PyEphem (from the Greek word ephemeris) is the way to calculate the positions of all sorts of astronomical bodies in Python. Continue reading ‘Calculate sunrise and sunset with PyEphem’
Monthly Archive for April, 2008
Update: After some folks requested it in the comments, I wrote another post, A minimal Sphinx setup for autodocumenting Python modules. You might want to check this out if you’re specifically interested in automatically documenting your code with Sphinx.
I’ve been doing quite a bit of code documentation lately, and I decided to try and figure out the best tool to use. I found it. It’s called Sphinx, and you can see what the documentation looks like by checking out the documentation for Python itself (v. 2.6 and 3.0).
Here’s how to get started using Sphinx. Continue reading ‘Use Sphinx for documentation’
I’ve been generating many figures, and I want to be able to find them again and browse them easily. Organizing them on disk just isn’t cutting it. My solution for now is to use a local TiddlyWiki as the glue for my figures, since I can embed figures in tiddlers (the microcontent entries that are the bread and butter of TiddlyWikis), and tag and search those entries. Bonus: I can zip everything up and send TiddlyWiki + images to my advisor so he can browse and search them as well.
Try this Python script, addtiddler.py, to insert tiddlers into an existing TiddlyWiki. You can optionally specify an image name (relative to the output file, see the documentation in the source code) to be embedded. You can use this script from the command line using options, or import it into another script.
I tried to add lots of comments so you can modify it for your own needs. Let me know if you find bugs so I can fix them.
The sort() method of list objects in Python is quite flexible. By default, it sorts on the first thing in each item of the list, which is exactly what you would expect. For example, a list of strings is sorted by the first letter of each string. What if you wanted to sort by the second letter of each string? Or sort a list of people’s names by last name? Continue reading ‘Advanced sorting: sorting by key’
So you’ve come up with a couple of ways to solve a problem in code. But how do you decide which way is the best? One criterion to decide on is to use the one that makes the most sense to you. Another criterion is to use the version that is fastest. Here’s how to quickly determine which way is fastest using the interactive interpreter IPython. Continue reading ‘Test the speed of your code interactively in IPython’
Here are a couple of ways of sorting one list by another list in Python. The first uses plain ol’ Python, and the others use NumPy.
In each case imagine we want to sort a list of peoples names by their ages. Continue reading ‘Sort one list by another list’
Use the excellent xlrd package, which works on any platform. That means you can read Excel files from Python in Linux! Example usage:
Open the workbook
import xlrd
wb = xlrd.open_workbook('myworkbook.xls')
Check the sheet names
wb.sheet_names()
Get the first sheet either by index or by name
sh = wb.sheet_by_index(0) sh = wb.sheet_by_name(u'Sheet1')
Iterate through rows, returning each as a list that you can index:
for rownum in range(sh.nrows):
print sh.row_values(rownum)
If you just want the first column:
first_column = sh.col_values(0)
Index individual cells:
cell_A1 = sh.cell(0,0).value cell_C4 = sh.cell(rowx=3,colx=2).value
(Note Python indices start at zero but Excel starts at one)
Turns out the put_cell() method isn’t supported, so ignore the following section (Thanks for the heads up, John!)
Put something in the cell:
row = 0 col = 0 ctype = 1 # see below value = 'asdf' xf = 0 # extended formatting (use 0 to use default) sh.put_cell(row, col, ctype, value, xf) sh.cell(0,0) # text:u'asdf' sh.cell(0,0).value # 'asdf'
Possible ctypes: 0 = empty, 1 = string, 2 = number, 3 = date, 4 = boolean, 5 = error
The default engine in MySQL, MyISAM, does not support foreign keys. You need foreign keys to use the SQLAlchemy Python package effectively.
In order to use foreign keys, you need to convert your tables to the InnoDB engine:
ALTER TABLE mytable ENGINE = INNODB;
To add a foreign key to mytable where the unique keys are coming from othertable, use this:
ALTER TABLE mytable ADD FOREIGN KEY (otherID) REFERENCES othertable (otherID);
If you run that same line several times, several identical foreign keys will be created, which will confuse SQLAlchemy. In that case you need to delete the keys. To do so, you need their name. To see the name, use
SHOW CREATE TABLE mytable;
The foreign key’s label will be something that ends in something similar to fk_1 or fk_2. Using that label, you can then delete the foreign key:
ALTER TABLE mytable DROP FOREIGN KEY mytable_dbfk_2;