transparent proxy object
Beni Cherniavsky
cben at techunix.technion.ac.il
Sat Mar 1 16:29:38 EST 2003
On 2003-02-27, Alex A. Naanou wrote:
> Irmen de Jong <irmen at -NOSPAM-REMOVETHIS-xs4all.nl> wrote:
> ...
> > What about the code below?
> > I snipped it from Pyro and stripped it a bit.
> > Didn't do any comparison with your code, although yours
> > looks a lot more complex than this one...
> >
> > Irmen de Jong
> >
>
> The code you posted works quite fine, while we stay within a one level
> attribute reference (i.e. "Class.Attribute" not
> "Class.Attribute_L1.Attribute_L2"... etc.), the goal was to intercept
> references of any depth....
>
I've just finished (more or less) debugging a module which creates
almost completely transparent tracing proxy objects. It's intended
for debugging; effeciency is probably very bad! Grab it from:
http://www.technion.ac.il/~cben/python/traceproxy.py
It's not documented properly yet (and some comments are out of date)
and has some "rough edges" (read: bugs) but it's quite usable. The
nicest thing was that since it became half-working, I was able to use
it to debug itself :-). Some fun things with it:
>>> from traceproxy import *
>>> trace(1) * 2 + 3
1 * 2 -> 2
(1 * 2) + 3 -> 5
5
>>> map(trace, xrange(4)).sort()
cmp(1, 0) -> 1
cmp(2, 1) -> 1
cmp(3, 2) -> 1
>>> trace(dict).fromkeys(trace('abc'))
<type 'dict'>.fromkeys -> <built-in method fromkeys of type object at 0x8101500>
abc[0] -> 'a'
hash((abc[0])) -> -468864544
abc[1] -> 'b'
hash((abc[1])) -> -340864157
abc[2] -> 'c'
hash((abc[2])) -> -212863774
abc[3] ==> exceptions.IndexError : string index out of range
(<type 'dict'>.fromkeys)('abc') -> {'a': None, 'c': None, 'b': None}
{'a': None, 'c': None, 'b': None}
--
Beni Cherniavsky <cben at tx.technion.ac.il>
True elegance has negative overhead so it isn't subject to tradeoffs.
More information about the Python-list
mailing list