Popular recipes by Martin Blais http://code.activestate.com/recipes/users/1643324/2007-10-09T20:22:37-07:00ActiveState Code RecipesAST Pretty Printer (Python)
2007-10-09T20:22:37-07:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/533146-ast-pretty-printer/
<p style="color: grey">
Python
recipe 533146
by <a href="/recipes/users/1643324/">Martin Blais</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
Revision 2.
</p>
<p>A function that outputs a human-readable version of a Python AST.</p>
AST Pretty Printer (Python)
2007-10-09T11:42:44-07:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/533145-ast-pretty-printer/
<p style="color: grey">
Python
recipe 533145
by <a href="/recipes/users/1643324/">Martin Blais</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
</p>
<p>A function that outputs a human-readable version of a Python AST.</p>
True Lieberman-style delegation in Python. (Python)
2007-05-14T11:14:33-07:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/519639-true-lieberman-style-delegation-in-python/
<p style="color: grey">
Python
recipe 519639
by <a href="/recipes/users/1643324/">Martin Blais</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Proxies are usually implemented as objects that forward method calls to a
"target" object. This approach has a major problem: forwarding makes the target
object the receiver of the method call; this means that calls originating from
the body of a method in the target will not go through the proxy (and thus their
behavior cannot be modified by the proxy).</p>
<p>For example, suppose we want a proxy to an instance of Target (shown below)
that is "safe", i.e., does not do anything bad like firing missiles. We can
just define a class that forwards calls to the safe methods, namely
send_flowers() and hang_out(). This class can have its own version of
fire_missiles() that does nothing. Now consider what happens when we call
the proxy object's innocent-looking hang_out() method. The call is forwarded
to the target object, which in turn calls the target object's (not the
proxy's) fire_missiles() method, and BOOM! (The proxy's version of
fire_missiles() is not called because forwarding has made the target object
the receiver of the new method call.)</p>
<p>By using delegation, one can implement proxies without the drawbacks of the
method-forwarding approach. This recipe shows how Python's __getattr__
method can be used to implement the kind of delegation present in
prototype-based languages like Self and Javascript, and how delegation can
be used to implement better proxies.</p>
Remove FTP directory walk() equivalent. (Python)
2006-12-18T11:31:55-08:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/499334-remove-ftp-directory-walk-equivalent/
<p style="color: grey">
Python
recipe 499334
by <a href="/recipes/users/1643324/">Martin Blais</a>
.
</p>
<p>A routine much like os.walk() that iterates over the directories and files of a remote FTP storage.</p>
Builtin i18n _() function in a multi-threaded environment. (Python)
2006-01-02T10:57:10-08:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/465756-builtin-i18n-_-function-in-a-multi-threaded-enviro/
<p style="color: grey">
Python
recipe 465756
by <a href="/recipes/users/1643324/">Martin Blais</a>
(<a href="/recipes/tags/web/">web</a>).
</p>
<p>Injecting _() in the __builtin__ module in order to inject global functions _() and N_() is common in applications which need i18n. This is a variation on the theme that does this in a multi-threaded environment, using threading.local from Python-2.4.</p>
Generic proxy object with before/after method hooks. (Python)
2005-02-07T22:52:49-08:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/366254-generic-proxy-object-with-beforeafter-method-hooks/
<p style="color: grey">
Python
recipe 366254
by <a href="/recipes/users/1643324/">Martin Blais</a>
.
</p>
<p>A proxy object that delegates method calls to an instance, but that also calls hooks for that method on the proxy, or for all methods. This can be used to implement logging of all method calls and values on an instance.</p>