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.

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.


  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_installs 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…

The IDL Data Point recently had an article 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.

For example, Python’s doctest module provides a solution to these problems for Python. In your docs, you give a command and its expected output:

>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]

Then, in the main-level program, you say something like (I’ve omitted some Python details for simplicity here):


When you do the equivalent of running the main-level program, the docs are checked for “>>>” lines which are executed and compared to the output shown in the docs.

It seems like there are several solutions that could be implemented in IDLdoc or mgunit to solve this problem.

  1. Create a doctest routine for IDL. This would mean to create a routine like the dockets.testmod() routine in doctest that runs all the lines in the documentation that begin with “IDL>” and compares the results to the output shown below the “IDL>” line.

Advantages: Can be used to easily run tests and compare results to provided output. Test commands and output are included in documentation, but are not present twice in the file.

Disadvantages: None?

  1. Conversely, create an IDLdoc include directive, like ..include:: [main-level] that includes the contents of the main-level of the current file. This could be used in the examples section of the docs to include the main-level program.

Advantages: Could also be used to include other files which might be useful in other situations.

Disadvantages: For files with multiple routines in a file, the entire main-level program would be included at the location, even if the main-level program addressed multiple of the routines. Doesn’t help with testing.

  1. Allow IDLdoc to do the testing by creating a tests section that would be executed when IDLdoc is run.

Advantages: Can be used to easily run tests and compare results to provided output. Test results appear in the IDLdoc output.

Disadvantages: Need to run IDLdoc to run the tests.

Once nice thing here is that several of these strategies could be implemented. Any ideas, suggestions on what you would like to see? Creating an MG_DOCTEST routine seems like the strongest solution right now, but am I missing something?

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 example

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 (video, 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 »