Logging global assignments

brianmce at gmail.com brianmce at gmail.com
Fri Mar 11 09:29:52 EST 2005


In 2.4 at least, it looks like execfile can take an arbirary mapping
object for globals, so something like this may work:

class LoggedDict(dict):
    def __setitem__(self, item, val):
        dict.__setitem__(self, item, val)
        # Do whatever logging is needed here
        print "Assignment made: %s = %r" % (item, val)

# When loading your config file use:
logged_globals = LoggedDict()
execfile(my_config_file, logged_globals)

Note that this covers all name binding (ie. function and class
definitions etc), not just assignments.

This might not work with earlier versions of python, since I think
these may restrict the globals argument to a real dictionary.




More information about the Python-list mailing list