Tech and travel

Category: Python

Python logging output file change

2009-08-12

I run unit tests in Python using the standard unittest module. The output should be logged to a file. For this Python has a standard logging library, which is somewhat similar to the logging library for Java. The problem is that there is no easy way to direct the logging to a new file for every test case. Here’s how I got around it. Firstly the setup method of my testcase object contains the following : def setUp(self): self.

Using map with a dictionary in Python

2009-07-21

When you have a dictionary in Python like this : mydict={'first':1, 'second':2, 'third':3} and you want to get the values into a list with a certain order you can do this : mylist=[mydict['first'], mydict['second'], mydict['third']] However, that can get a bit verbose if there are a lot of entries. For every entry you have to repeat ‘mydict’. An elegant way of solving this is using the map function : mylist=map(mydict.get,['first', 'second', 'third'])

Python generators

2008-07-03

Python generators are powerful, but perhaps a bit hard to understand. Luckily, a great tutorial on them can be found at http://www.dabeaz.com/generators/index.html .

Workout

2008-04-23

My Google App Engine application is a workout followup utility. You can enter your times or distances, and the results are shown in list. Graphs were added using the Google Chart API. The URL is http://workoutfollowup.appspot.com/ (note : you will have to login with your Google ID). One of the things added recently is the ability to download the results as a csv file. The following code snippet shows how to do this.

Google App Engine

2008-04-14

Python‘s creator, Guido Van Rossum, has been hinting at this for a while, and it’s finally here : Google App Engine. It’s a way to build and host web apps on the Google servers. I put a small app together, which is a very simple variation on the ‘The Django Form Validation Framework on Google App Engine‘ article. It let’s you login, and store some data on workouts. On to some code : import cgi import wsgiref.

Copyright (c) 2024 Michel Hollands