Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web.
|
2008-04-01 A simple Python class to trace access to object attributesEvery now and then I run into a situation where I want to know what attributes on an object are getting accessed when. Fortunately it's pretty easy to knock together something to do this, in a variant on the mutating proxy pattern. Here is a sample implementation, as a
def _repon(obj, n):
return (not obj._t_il) or \
(n in obj._t_il)
class Tracer(object):
def __init__(self, real,
name = None, monitor = None):
self._t_ro = real
if name:
self._t_nm = name
else:
cl = real.__class__
self._t_nm = "<%s.%s>" % \
(cl.__module__, cl.__name__)
self._t_il = monitor
def __getattr__(self, n):
if _repon(self, n):
print "get %s.%s" % (self._t_nm, n)
return getattr(self._t_ro, n)
def __setattr__(self, n, v):
if n in ("_t_nm", "_t_ro", "_t_il"):
super(Tracer, self).__setattr__(n, v)
return
if _repon(self, n):
print "set %s.%s" % (self._t_nm, n)
setattr(self._t_ro, n, v)
def __delattr__(self, n):
if _repon(self, n):
print "del %s.%s" % (self._t_nm, n)
delattr(self._t_ro, n)
You use it by replacing the original object with There's a couple of limitations in this approach:
As a side note, the (Disclaimer: the indentation has been shrunk and the variable names shortened in the interests of not overflowing WanderingThoughts' margins. I do not write real code that is this condensed.)
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |