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 = 'https://api.github.com/repos/mgalloy/mglib'
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]: https://www.python.org/dev/peps/pep-0020/ "PEP 20"
[requests]: http://docs.python-requests.org/en/latest/ "Requests: HTTP for Humans"
[Anaconda]: https://store.continuum.io/cshop/anaconda/ "Anaconda Scientific Python Distribution"
[several]: http://isbullsh.it/2012/06/Rest-api-in-python/ "Communicating with RESTful APIs in Python"
[reviews]: https://lumberjaph.net/HTTP_requests_with_python/ "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]: http://continuum.io/blog/numba-0.13 "Numba 0.13"
[HTML]: http://michaelgalloy.com/wp-content/uploads/2014/04/Numba-with-CUDA.html "Numba with CUDA IPython HTML"
[notebook]: http://michaelgalloy.com/wp-content/uploads/2014/04/Numba-with-CUDA.ipynb "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')
2
>>> print idl.ev('x ^ 2')
4
>>> idl.reform(range(4), 2, 2)
array([[0, 1],
[2, 3]])
>>> idl.histogram(range(4), binsize=3, L64=True)
array([3, 1], dtype=int64)
>>> idl.pro('plot', range(10), range(10), xstyle=True, ystyle=True)
>>> idl.interact()
IDL> print, x
4
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]: http://modernidl.idldev.com "Modern IDL"
[PyIDL]: http://sourceforge.net/projects/pyidl/ "pyIDL Sourceforge page"
[pIDLy]: https://github.com/anthonyjsmith/pIDLy "pIDLy GitHub page"

[example-main]: http://idldatapoint.com/2012/05/03/the-merits-of-an-example-main-program "IDL Data Point: The merits of an example main program"
[doctest]: http://docs.python.org/library/doctest.html "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](http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html) 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.

and

> 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](http://www.boingboing.net/2010/05/10/funny-history-of-pro.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+boingboing%2FiBag+%28Boing+Boing%29).

[Matplotlib](http://matplotlib.sourceforge.net/index.html) 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](http://matplotlib.sourceforge.net/contents.html) 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](http://www.packtpub.com/files/7900-matplotlib-for-python-developers-sample-chapter-6-embedding-matplotlib-in-qt-4.pdf). More information, such as a full [table of contents](http://www.packtpub.com/article/matplotlib-python-development-table-of-contents), is also available on the [publisher's site](http://www.packtpub.com/matplotlib-python-development/).

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

[Proceedings of the 8th Python in Science Conference ](http://conference.scipy.org/proceedings/SciPy2009/) are now available. I would love to have something like this for IDL.

Link via [Gaël Varoquaux](http://gael-varoquaux.info/blog/?p=123).

[IDLSave](http://idlsave.sourceforge.net/) 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](http://groups.google.com/group/comp.lang.idl-pvwave/browse_thread/thread/13660b4e5ff8fd2e#) `comp.lang.idl-pvwave` thread about migrating from IDL to Python.)

Guido van Rossum, creator of Python, recently showed up for a [Py4science](https://cirl.berkeley.edu/view/Py4Science) meeting in the San Francisco Bay Area ((http://www.archive.org/details/ucb_py4science_2009_11_04_Guido_van_Rossum), [slides](http://fperez.org/py4science/2009_guido_ucb/index.html)). There are several accounts of the discussion by attendees: [Guido](http://neopythonic.blogspot.com/2009/11/python-in-scientific-world.html) himself; [Matthew Brett](http://nipyworld.blogspot.com/2009/11/guido-van-rossum-talks-about-python-3.html), of Neuroimaging in Python (NiPy); [Jarrod Millman](http://jarrodmillman.blogspot.com/2009/11/visit-from-guido-van-rossum.html), of NumPy, NiPy, and SciPy; and [Fernando Perez](http://fdoperez.blogspot.com/2009/11/guido-van-rossum-at-uc-berkeleys.html), 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](http://opticks.org/confluence/display/opticksSDK/Opticks+Open+Source+Programming+Contest) by writing an Opticks extension in C++, Python, or IDL. I'm not familiar with [Opticks](http://opticks.org/confluence/display/opticks/Welcome+To+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 »