Thursday, 24 December 2009

Spare batteries for IronPython

As we all know, Python comes with batteries included in the form of a rich standard library; and, on top of this, there are many awesome and liberally-licensed packages just an easy_install away.

IronPython, of course, includes *most* of the CPython standard library, but if you're a heavy user you might have noticed a few minor holes: in the course of my work on Ironclad, I certainly have. Happily for you I can vaguely remember what I did in the course of bodging them closed with cow manure and chewing gum; here then, for your edification and delectation, is my personal recipe for a delicious reduced-hassle IronPython install, with access to the best and brightest offered by CPython, on win32.

  • Install IronPython 2.6.

  • Download Jeff Hardy's zlib for IronPython and copy IronPython.Zlib.dll into IronPython's DLLs subdirectory (create it if it doesn't exist).

  • Download Jeff Hardy's subprocess.py for IronPython and copy it into IronPython's site-packages subdirectory.

  • Download Ironclad, and copy the ironclad package into IronPython's site-packages subdirectory. Yeah, maybe I'll sort out an installer one day, but don't hold your breath.

  • Install CPython 2.6.

  • Add CPython's Dlls subdirectory to your IRONPYTHONPATH environment variable.

  • Copy csv.py, gzip.py, and the sqlite3 directory from CPython's Lib subdirectory to IronPython's site-packages subdirectory.

  • Copy xml/sax/expatreader.py from CPython's Lib subdirectory to the corresponding location in IronPython's Lib subdirectory.

  • Download FePy's pyexpat.py, copy it to IronPython's Lib/xml/parsers subdirectory, and rename it to expat.py.

  • Download and install NumPy 1.3.0 and SciPy 0.7.1 for CPython, and copy them from CPython's site-packages subdirectory to IronPython's.


...and you're done. Start your ipy sessions with a snappy 'import ironclad', and enjoy.

Incidentally, you could just add CPython's site-packages to your IRONPYTHONPATH, and then you wouldn't have to copy extra packages over; the reason I don't do that is because having matplotlib on your path currently breaks scipy under ironclad -- can't remember exactly why -- and it's nice to have matplotlib installed for CPython.

Oh, and let me know if I've made any mistakes above: I just hacked this post together from slightly aged notes, and I'm too lazy to tear down and rebuild my environment to check that every detail is perfect.

Monday, 7 December 2009

Another snippet

I realised you could do this about a year ago, but I only just found an opportunity to use it.

def merge_dicts(d1, d2):
return dict(d1, **d2)

Satisfying :).

Saturday, 5 December 2009

Python return dictifier

I wrote this the other week, and thought it was cute enough to share:


def _dictify(keys, result):
if len(keys) == 1:
return { keys[0]: result }
return dict(zip(keys, result))

def return_dict(keys):
keys = keys.split()
def decorator(f):
def g(*_, **__):
return _dictify(keys, f(*_, **__))
return g
return decorator

Use it as follows:

>>> @return_dict('foo bar baz')
... def f():
... return 1, 2, 3
...
>>> f()
{'baz': 3, 'foo': 1, 'bar': 2}

Enjoy!