Category "Python"

The “Zen of Python” provides the basic philosophy of Python. From [PEP 20]:

> There should be one — and preferably only one — obvious way to do it.

I doubt there is a single area in Python that violates this more than making a simple HTTP request. Python provides at least six builtin libraries to do this: httplib, httplib2, urllib, urllib2, urllib3, and pycurl. There are [several] [reviews] comparing the various libraries.

But there is a third party library, [requests], that might be the “obvious way to do it” now:

import requests, json

url = ‘’
r = requests.get(url, auth=(‘mgalloy’, ‘my_password’))
print r.json()[‘updated_at’]

requests is installed as part of [Anaconda], which an easy way to get all the core scientific programming packages for Python.

[PEP 20]: “PEP 20”
[requests]: “Requests: HTTP for Humans”
[Anaconda]: “Anaconda Scientific Python Distribution”
[several]: “Communicating with RESTful APIs in Python”
[reviews]: “The state of HTTP’s libraries in Python”

Numba is a Python package that uses the LLVM compiler to compile Python code to native code. [Numba 0.13], released a few weeks ago, offers support for automatically creating CUDA kernels from Python code.

I created a [notebook][^1] ([HTML]) to show off the demo code.

[Numba 0.13]: “Numba 0.13”
[HTML]: “Numba with CUDA IPython HTML”
[notebook]: “HTML output of Numba with CUDA IPython notebook”

[^1]: I’m not sure which is cooler, IPython notebooks or Numba.

In *[Modern IDL]*, I give a short demo of using [pyIDL] to use IDL from within a Python session. I have had problems with installing pyIDL lately, but didn’t have an alternative until I found [pIDLy] recently. It `easy_install`s nicely and has an even nicer interface:

>>> import pidly
>>> idl = pidly.IDL()
>>> idl(‘x = total([1, 1], /int)’)
>>> print idl.ev(‘x’)
>>> print idl.ev(‘x ^ 2’)
>>> idl.reform(range(4), 2, 2)
array([[0, 1],
[2, 3]])
>>> idl.histogram(range(4), binsize=3, L64=True)
array([3, 1], dtype=int64)
>>>‘plot’, range(10), range(10), xstyle=True, ystyle=True)
>>> idl.interact()
IDL> print, x
IDL> exit
>>> idl.close()

I don’t see any versioned releases since 2008, but there are commits to the GitHub repo in the last few months so I believe the project is still alive. In any case, it does what I need right now and I can install it. Updating book to use pIDLy…

[Modern IDL]: “Modern IDL”
[PyIDL]: “pyIDL Sourceforge page”
[pIDLy]: “pIDLy GitHub page”

[example-main]: “IDL Data Point: The merits of an example main program”
[doctest]: “Python doctest module”

The IDL Data Point recently had an [article][example-main] about writing a simple main-level program at the bottom of each file which would give an example or test of the routine(s) in the files. I like this idea and have been doing it for quite a while, but one of the annoyances of this approach is that I also typically want the code to be included in the documentation for the routine, so I end up copy-and-pasting the code into the examples section of the docs (and, of course, reformatting it for the doc syntax). Also, the main-level program just is a place to put some code, I have to do all the work if I actually want to write multiple pass/fail tests.

Continue reading “Doc testing thoughts.”

[This history of programming languages]( is hilarious:

> 1957 – John Backus and IBM create FORTRAN. There’s nothing funny
> about IBM or FORTRAN. It is a syntax error to write FORTRAN while
> not wearing a blue tie.


> 1972 – Dennis Ritchie invents a powerful gun that shoots both
> forward and backward simultaneously. Not satisfied with the number
> of deaths and permanent maimings from that invention he invents C
> and Unix.

I wonder what IDL’s entry would look like?

Link via [BoingBoing](

[Matplotlib]( is a popular 2-dimensional plotting library used with Python. While Matplotlib is fairly powerful, I have always had trouble figuring out how to do what I needed from the documentation. I have found the online Matplotlib [documentation]( adequate as a reference, but not very good for getting started (especially since Matplotlib uses some terms in what I would call a “non-standard” way).

*Matplotlib for Python Developers* by Sandro Tosi is a welcome addition to my bookshelf. It focuses on embedding Matplotlib in applications in GTK+, Qt 4, wxWidgets, and even various web frameworks such as Pylons and Django. But the fundamental elements of the scientific Python tool chain, such as NumPy and IPython, as well as a thorough tutorial of Matplotlib itself are discussed. Both the `pylab`/`pyplot` procedural interface (useful for interactive plotting) and object-oriented interface (useful for fine-tuning and more advanced usage) are covered. Over 2000 lines of example code are downloadable from the book site.

Seeing as I don’t use Matplotlib regularly, I’m sure I will using this book frequently to brush up on Matplotlib basics. And this book would have saved me a lot of time when I was writing my only major Python project, a PyQt 4 application which embedded interactive Matplotlib graphics. While this book doesn’t cover every detail of Matplotlib (the online docs are good for that), it will get you started no matter how you are using it.

The publisher has made a sample chapter on embedding Matplotlib in Qt4 [available for download]( More information, such as a full [table of contents](, is also available on the [publisher’s site](

*Full disclosure: I received a review copy of this book.*

[Proceedings of the 8th Python in Science Conference ]( are now available. I would love to have something like this for IDL.

Link via [Gaël Varoquaux](

[IDLSave]( is a Python module to read IDL save files from Python. IDLSave converts IDL types to Python types: arrays are converted to NumPy arrays, structures are converted to NumPy record types. NumPy is required.

(Mentioned by Brian Larsen in [this]( `comp.lang.idl-pvwave` thread about migrating from IDL to Python.)

Guido van Rossum, creator of Python, recently showed up for a [Py4science]( meeting in the San Francisco Bay Area ((, [slides]( There are several accounts of the discussion by attendees: [Guido]( himself; [Matthew Brett](, of Neuroimaging in Python (NiPy); [Jarrod Millman](, of NumPy, NiPy, and SciPy; and [Fernando Perez](, of IPython. It looks like people are starting to see the benefits of porting to Python 3.

Win a free Wii in the [Opticks Open Source Programming Contest]( by writing an Opticks extension in C++, Python, or IDL. I’m not familiar with [Opticks](, but it looks like an ENVI competitor:

> Opticks is an expandable remote sensing and imagery analysis software platform that is free and open source.

« newer postsolder posts »