"""
"s" is a string containing a Python statement. "objstrlist" is a list of strings where each string is a python expression. An example is watch('x = 2 * [[]]', ['x[0]', 'x[1]'] """
import string

# "s" is a string containing a Python statement. "objstrlist" is a list of
# strings where each string is a python expression. An example is
#      watch('x = 2 * [[]]', ['x[0]', 'x[1]']
def watch(s, objstrlist):
    exec s in globals(), {}
    print 'After executing "%s"' % s
    for objstr in objstrlist:
        v = eval(objstr)
        t = str(type(v))[7:-2]
        print '%10s : value %10s, id %9i, type %-10s' % \
             (objstr, v, id(v), t)

# Executes the statement, or list of statements, s, then prints information
# about all the local variables that were created.
def watchall(s):
    if type(s) == type([]):
        lines = s
    else:
        lines = string.split(s, '\n')

    prog = string.join(lines, '\n')

    # It appears that some python programs need to end in a newline.
    if prog[-1] != '\n':
        prog = prog + '\n'

    loc = {}
    exec prog in globals(), loc

    print 'After executing'
    for line in lines:
        print '-> ', line

    for objstr in loc.keys():
        v = loc[objstr]
        t = str(type(v))[7:-2]
        print '%10s : value %10s, id %9i, type %-10s' % \
             (objstr, v, id(v), t)

# Same as watchall but prints information about elements of lists.
def watchall1(s):
    if type(s) == type([]):
        lines = s
    else:
        lines = string.split(s, '\n')

    prog = string.join(lines, '\n')
    # It appears that some python programs need to end in a newline.

    if prog[-1] != '\n':
        prog = prog + '\n'

    loc = {}
    exec prog in globals(), loc

    print 'After executing'
    for line in lines:
        print '-> ', line

    for objstr in loc.keys():
        v = loc[objstr]
        t = str(type(v))[7:-2]
        print '%10s : value %10s, id %9i, type %-10s' % \
             (objstr, v, id(v), t)
        if type(v) == type([]):
            for i in range(len(v)):
                t = str(type(v[i]))[7:-2]
                print '%7s[%i] : value %10s, id %9i, type %-10s' % \
                    (objstr, i, v[i], id(v[i]), t)

if __name__ == '__main__':
    prog = [
       "for i in range(3):",
       "    print i"
    ]
    watchall(prog)
#
Make your own free website on Tripod.com