Emulating ActionScript's "with" statement in Python
If you haven't had much to do with ActionScript or Visual Basic, right now you might be thinking, as I did, "huh?" Basically the with statement in both of these languages allows one to access the attributes of an object as if they were in the local scope.
I know, I know. Why would you want to do this? I have no idea, but I thought it was cool that it was even possible in Python. However, there are some fairly major limitations:
(...)
Walking a py3k AST
The compiler module has been removed from py3k. Fortunately there's a replacement in the _ast module introduced in Python 2.5. Unfortunately, while the compiler module has a useful __repr__esentation for its AST objects...
>>> compiler.parse('x = 10')
Module(None, Stmt([Assign([AssName('x', 'OP_ASSIGN')], Const(10))]))
This is not the case for the _ast module, so here's a function that will dump _ast.AST objects as a dict:
(...)
Type masquerading using dynamic base classes
Python allows us to construct new classes on-the-fly with type(). By (ab)using this feature we can construct a class that preserves its own interface while assuming all of the behaviour of an existing object. Useful for wrapping compound types such as dictionaries, lists, sets, etc. without having to proxy __getitem__, __iter__ and so on, or write a custom __getattr__.
One example of when this could be useful is a JSON-specific HTTP client object that assumes the type of the JSON data, while maintaining response-specific attributes such as headers and status:
(...)
CLY 0.9 released
CLY is a project I've been working on for a while now, and I've finally gotten around to releasing it.
It's basically a CLI parser and grammar constructor that lets you easily add command-line interfaces to your applications:
echo.py:
from cly import *
def echo(text):
print text
grammar = Grammar(
echo=Node(help='Echo text')(
text=Variable(help='Text to echo', pattern=r'.+')(
(...)
It seems that all I blog about is workingenv.
This time it's a snippet of code that adds a "sandbox" command to distutils,
which automatically creates a workingenv from the setuptools extras_require,
install_requires and dependency_links options listed in your setup()
call. It only supports *nix systems for now, but could easily be extended to
(...)
Finally, a new release of Dev Todo. All bugs I'm aware of are fixed, and Dev Todo now builds cleanly on compilers from this century.
Check out the changelog for details.
April SyPy Presentation on PyCon
This months SyPy meeting attracted a larger number of people than usual, around 35 or so (compared to the usual 5 or 6).
I gave a talk about my trip to PyCon, which was received fairly well I think (I judge it a success by not being booed off the stage). S5 version is here.
Andrew Bennets gave a pretty interesting talk on Bazaar, although a lot of it was an introduction to distributed VCSes in general. One interesting aspect of Bazaar in particular, is the plugin system.
Activating a workingenv from Python
It can, under some circumstances, be useful to be able to activate a workingenv from Python. Here's a quick function to achieve that:
import sys
import os
def activate_workingenv(root):
"""Make modules in a self-contained workingenv available."""
# Add ./bin directory to path.
bin_dir = os.path.join(root, './bin')
try:
os.environ['PATH'] = os.path.pathsep.join([bin_dir, os.environ['PATH']])
(...)
Automatically activating workingenv.py environments on directory change
workingenv.py is a very useful tool for Python development. Quoting from its home page:
This tool creates an environment that is isolated from the rest of the Python installation, eliminating site-packages and any other source of modules, so that only the modules (and versions) you install into the environment will be available. This allows for isolated and controlled environments, as well as reproduceability.
(...)
Dallas, Texas - PyCon!
I've arrived in Dallas for PyCon. I'm very keen, yes indeed.
A few Trac hackers will be attending, and we're getting together for a BoF and a sprint. I can't wait to finally put faces to the names of those on trac-dev :)
I'm still not 100% decided on what talks to go to. My experience at LCA made me painfully aware that the subject matter is only a small part of what makes a talk interesting, the delivery is just as important. I'm trying to keep that in mind when selecting talks for PyCon.
(...)
Now that 0.2 is finally out the door, what next?
Firstly, making query term negation work in the default indexer. Not having this is suboptimal. My initial thought on how to solve this will be to create a set class with support for lazily evaluated complements.
Next up, more extensive unit tests. Specifically, I want to test the UTF support of all the indexer adapters.
After many months of development, pyndexter 0.2 has been released. I'm much happier with the overall design of 0.2 than 0.1, although there are still some major features I'd like to add before I'm completely happy.