Tuesday, January 27, 2015

Notes on distributing cython code

One of the conveniences of Python is the package system which allows you to install your program and any dependencies smoothly. The package system works very well when the code is pure Python, but can run into trouble when code written in cython or c is part of the program.

I will illustrate some mis-steps I made while writing a install script for an example program that is a mixture of Python and Cython. I've put the code up on github and each step is a commit tag. You can follow along by setting up a virtual environment using virtualenvwrapper:

mkvirtualenv cy-test

And then trying to install the appropriate tag, e.g:

git clone git@github.com:kghose/cython-example.git
cd cython-example
git checkout ex2


The module installs without errors, but because of me not indicating the paths of the cython files properly (I omit the kgcyex directory in the path) the cython files do not compile. You will note this because there are no compilation messages during the install, though the failure is otherwise silent
kghose$ kgcyex
Traceback (most recent call last):
  File "/Users/kghose/.venvs/blog/bin/kgcyex", line 9, in <module>
    load_entry_point('kgcyex==1.0.0', 'console_scripts', 'kgcyex')()
  File "/Users/kghose/.venvs/blog/lib/python2.7/site-packages/pkg_resources.py", line 356, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/Users/kghose/.venvs/blog/lib/python2.7/site-packages/pkg_resources.py", line 2431, in load_entry_point
    return ep.load()
  File "/Users/kghose/.venvs/blog/lib/python2.7/site-packages/pkg_resources.py", line 2147, in load
  File "/Users/kghose/.venvs/blog/lib/python2.7/site-packages/kgcyex/main.py", line 2, in <module>
    import kgcyex.cy1 as cy1
ImportError: No module named cy1


I correctly write out the full paths of the cython modules, and everything installs and runs fine.
kghose$ kgcyex
foo from kgcyex.mod1
foo from kgcyex.cy1
foo from kgcyex.lib.mod2
foo from kgcyex.lib.cy2


Suppose the other user does not have Cython? The cython documentation suggests that we distribute the generated c code with the source. There is some debate as to whether this is "proper" since the .c files are actually generated from the .pyx files and in principle we should only really be distributing files which can not be auto-generated from the "real" source. For now, we put pragmatism over principle. Note that the setup.py changes a bit
If you read the setup.py you will note that I have used a check to test if the user has Cython or not. This check then tells setup to either use the .pyx files or the .c files. This is standard stuff recommended by the Cython folks. Look carefully at the setup.py where I add the extensions.
extensions = [Extension("cy1", ["kgcyex/cy1"+ext]), Extension("cy2", ["kgcyex/lib/cy2"+ext])]
Things compile properly because I've remembered to indicate the peoper path to the .pyx (or .c) files. When we run setup.py we can see the modules being compiled. But what the #$%@! when we go to run the code it again complains that it can find the compiled modules! In real life this error caused me to lose about an hour :(
My error was that though I had correctly indicated the path to the source (the second parameter forExtension) I had not given the proper dotted path for the modules themselves. If you look undersite-packages of your installation you will note that there are two compiled modules cy1.so andcy2.so directly under site-packages rather than in their proper places under kgcyex andkgcyex/lib. The correct form of this line is ...


extensions = [Extension("kgcyex.cy1", ["kgcyex/cy1"+ext]), Extension("kgcyex.lib.cy2", ["kgcyex/lib/cy2"+ext])]

Friday, January 23, 2015

Running bash functions in parallel

I was blown away when I learned this. From this thread on stackoverflow it turns out that by simply adding an ampersand to a line containing a function call you can send it to run in the background!

function foo {
  echo $1
  sleep $1

for i in `seq 1 10`; do
  foo $i &

I always thought that this was restricted to programs/scripts you can call from the command line!

Sunday, January 4, 2015

Electricity choice in Massachusetts

I've lived in Massachusetts for some years now and I've noticed that my electric bill is split into two parts: Delivery services and Supply services. I always thought that that was some itemizing detail, like the forty items I used to have on my phone bill and I ignored it. This month's bill was higher than expected and I took a closer look.

After a little inspection I noted that the Supply services rate was higher than before. It said "Basic Fixed Service". After some messing around on the National Grid Website, I came to this page. The important information there is this:
National Grid separates your bill into two services: supply and delivery. Supply Services is the portion of your electric service for which you can shop for your electricity supply from a supplier other than National Grid. These suppliers, often referred to as competitive suppliers, can be companies that produce or generate electricity or are brokers that buy electricity in the wholesale market and sell it to residents and businesses. National Grid is a delivery company, which means we will deliver electricity to you regardless of your choice of supplier. We encourage you to shop and compare the prices of competitive suppliers. Find out more about choosing your supply of electricity from a competitive supplier by visiting our Energy Choice area.
Wow. It goes on to say that by default you are signed on to a National Grid brokered plan where they buy electricity at wholesale rates and sell it to you for no profit and with some administrative costs added.

I went to the list of energy suppliers and browsed many of the companies. It does not take much time, and I would encourage you to do the same. It was interesting to me that most of these companies were offering rates lower than what I have from National Grid, which I was not expecting, if National Grid was a near monopoly buyer of electricity.

Some of the companies looked shady - the website had no upfront way to find out the electric supply cost, and they were offering incentives like gift cards and so on. The companies I favored were those that had a nice, easy interface for signing up and a clearly marked price per kWh.

Some of the companies did not serve Massachusetts, so I was surprised that a Mass customer was linked to them, but National Grid does serve many areas, so perhaps this is a country-wide list.

Some companies offer choices of getting electricity from renewable sources, which, if you have the budget for it, seems a good way to go. One company was offering renewable at about 20% more than regular, which isn't so bad if your electric-bills are about $100 a month.