#!/usr/bin/env python2

"""
grph.py

LICENSING: PUBLIC DOMAIN

Peace... -George
"""

#_____________________________________________________________________________
# Imports
#
from math import sin, cos, tan
import os
import sys

#_____________________________________________________________________________
# Functions
#
def fprintf(fstream, fmt, *args):
    """
    Emulate C's fprintf() function.
    """
    fmt %= args
    fstream.write(fmt)
    return len(fmt)

def plot(x_expression, xmin, xmax, ymin, ymax):
    """
    plot(str, int, int, int, int) -> str

    Return a string representation of a graph of x_expression.

    BUGS:
    - The graph is inverted.
    - Zero Divison Error when expressions like `1/x` are passed as
      x_expression.
    """
    return '\n'.join(
            ''.join((
        lambda x,y: 'o' if round(eval(x_expression)) == y else (
                    ' ' if y != 0 and x != 0 else (
                    '|' if y != 0 and x == 0 else (
                    '-' if y == 0 and x != 0 else
                    '+' ))))
                (x,y)
        for x in xrange(xmax, xmin-1, -1))
        for y in xrange(ymax, ymin-1, -1))

#_____________________________________________________________________________
# Main function
# 
def main():
    x_expression = raw_input("f(x) = ")
    fprintf(sys.stdout, "\nGraph of f(x) = %s\n%s\n", 
            x_expression,
            plot(x_expression, -10, 10, -10, 10))
    return 0

if __name__ == '__main__':
    main()

