Accessing OSX's Keychain From The Command Line

I’ve been complaining about OSX’s keychain and it’s lack of command line support for a while, and today I decided to fix that. I did a bit of googling and found that there is an app called security, that comes with it, and is very full-featured, but is a bit of a pain to handle. Here’s a quick and dirty bash function encapsulating a chunky one-liner to grab the password of your choice and put it on the clipboard. Obviously alter it to add your keychain - I only use one regularly so I just set it to that manually. ...

March 29, 2010

The Great Passenger Debacle

It would appear that, if you have Passenger running on Apache, and you put a Rails app on it, then you may find that the whole stack dies with a 500 error with absolutely no explanation and no stack trace or log to speak of it. I had this today and banged my head against it for hours before I found the reason: Rails had a database.yml telling it to connect to the local MySQL server, but could not, in fact, get access, because I had not created the username. ...

February 18, 2010

Twisted Deferreds: When and Where

The project I work on makes extensive use of Twisted’s Deferred job system, and while it’s pretty cool, it is atrociously documented (Seriously, guys, variables named ’thingy’?) and it’s pretty hard to tell when to use what. This is my attempt to pick it apart a little and lay this out - please correct me if I’m wrong. Use defer.deferToThread if you have a slow-running function and you want to turn it into a Deferred. ...

February 8, 2010

Extending Existing Classes in Rails

If you want to make a new class in Rails that isn’t a model or controller, you’ll probably put it into the lib directory. If you name it sensibly, then Rails will automagically find it when you reference it in your code. That’s cool, but if you extend an existing class, Rails will find the existing class, and as such won’t go looking for yours. Therefore you’ll need to require your shiny new alterations explicitly in environment.rb. ...

January 28, 2010

Python 'from' statements and reloading

Here’s little gotcha I just ran into: if you import something using ‘from’, it will not be re-imported when you reload the enclosing module. This means that if you have classes that contain some kind of class-wide state, and you reload the module it’s in, anywhere you have imported it with a from statement will end up having a different copy of the item. ...

November 23, 2009

Web.py + Google App Engine

I tried to write a little Hello World app for the app engine this evening and ran up against a sneaky problem. I used some quick tutorial code and came up with an app that looked like this: And it ran ok as a standalone program, but under dev_appserver.py it would hang, with no errors, and consume all of my memory and CPU time. Sucks. ...

October 5, 2009

Using setuptools with Jython 2.5

Thanks to the people on jython-users for this one. On newer versions of Jython, you can use easy_install to grab python packages easily. Your shiny jython-based easy_install will be available at $JYTHON_HOME/bin/easy_install. Brilliant.

October 1, 2009

Pretty Printing in Textmate

Here’s a quick and dirty snippet for pretty printing python dicts in Textmate. It’s very simple and won’t work in many cases due to the use of JSON, but it’ll work for dicts of primitives, as long as you aren’t using unicode/raw strings. Also, JSON doesn’t like single quotes, so I convert them blindly to double quotes, and this could cause massive issues. In short, your mileage may vary.

August 18, 2009

UNIX timestamps from strings

If you have a string, let’s say, “2009-02-19 02:18:00”, and you need a unix timestamp, the dateutils library is super useful. You can grab it with sudo easy_install python-dateutil and then you can run your strings through it like so: dateutil.parser.parse(‘2009-02-19 02:18:00’).strftime(’%s’) Why isn’t ‘%s’ anywhere to be found in the strftime docs? That would make this somewhat easier to figure out. ...

August 10, 2009

Sorting dicts of dicts

So, say you’ve got these dicts of files and metadata that, for whatever reason, you need in a dict. What if you need to sort that list while maintaining key/value pairs? (I know that this example is contrived, but I actually have this problem in something I’m working on) Itemgetter will do you just fine if you don’t need the key/value pairs to be maintained, but if you need the pairs intact, a lambda will do the same job for you. ...

June 26, 2009