Archive for the ‘python’ Category

Creating an OS X Installer for Python modules and extensions

In the past, people have hassled me to make it easier for them to install my applications and libraries on OS X. I’m not very skilled with OS X, and searching for how to do this task took some time. I’ve made a script that does this in a modular way for Python. Specifically, this article describes the process of making a Python module or extension installer for OS X, similar to the Python installer for Windows.

Read the rest of this entry »

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Python Google Chart 0.2.1 released

I just released pygooglechart 0.2.1. It has several bug fixes and minor additions, but the major change is that it now supports QR codes. You can quite easily create a QR code with pygooglechart as you can see below.

from pygooglechart import QRChart

# Create a 125x125 QR code chart
chart = QRChart(125, 125)

# Add the text
chart.add_data('Hello, World!')

# "Level H" error correction with a 0 pixel margin
chart.set_ec('H', 0)

# Download
chart.download('qr-hello.png')

This will download a png from the URL:

http://chart.apis.google.com/chart?cht=qr&chs=125×125&chl=Hello%2C%20World%21&chld=H|0

The image downloaded looks like this:

QR codes are new to me. I’ve only seen a QR code on a bus stop advertisement once in Sydney but I’m sure more will pop up.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Wireless Heatmap

I wrote an application to generate a wireless (802.11) heat map based on signal strength. The reason for it was to find the best place/area (for my laptop) to be with the highest signal strength. Below is a screenshot of the application (with the ESSID and BSSID removed for security reasons).

This heat map has about 100 samples in it, which are shown as white dots. There is a balcony at the top of the image and the common office area on the right, which was drawn by hand (hence the waviness). As you can see it has pinpointed the location of the access point in the red area.

Read the rest of this entry »

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

pycallgraph 0.5.1 released

Last week I quietly released pycallgraph 0.5.1 with some outstanding bugs fixed and preparation for it to be a Debian package.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Create a fast C Python extension with Cython tutorial

Cython is an easy way to create Python extensions in “pseudo-Python” without having to deal with the verboseness of the Python API. It also does not rely on you knowing much of the C language. Cython is based on Pyrex which I have some experience with. This tutorial shows you how to convert a simple Python module to a Python extension.

Read the rest of this entry »

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

TracRecaptchaPlugin 0.1.0 - A CAPTCHA plugin for Trac

I decided to make a CAPTCHA plugin for Trac called TracRecaptchaPlugin, to stop spammers using the ticket system. There is an existing Trac plugin to fight spammers, but it still lets through many automated spammers.

The plugin uses the reCAPTCHA service and is serving me well so far. Here’s how it looks within the template:

TracRecaptchaPlugin Screenshot

This is my first publicly released Trac plugin so I’m unsure how it’ll perform out there, so feedback is welcome.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Python Google Chart 0.2.0

I released pygooglechart 0.2.0 this afternoon after several months of patches coming in to add features and fix bugs. The main addition to pygooglechart is automatic data scaling, which is turned on by default. You can also specify the scale range manually:

chart = SimpleLineChart(width, height, x_range=(0, 100), y_range=(0, 100))

Other features are the new API chart types and options. There are 3 new chart types: Map, Google-o-Meter and Radar. Here is an example of using the Map chart type:

import pygooglechart as gc

chart = gc.MapChart(440, 220)
chart.set_colours(('AAAAAA', '30A030', 'A0C030'))
chart.set_codes(['AU', 'US', 'CA', 'BR', 'NZ'])
chart.add_data([1, 0.5, 0.7, 0.3, 0.1])
chart.download('map.png')

Produces the URL:
http://chart.apis.google.com/chart?cht=t&chs=440×220&chd=s:9frSG&chco=AAAAAA,30A030,A0C030&chtm=world&chld=AUUSCABRNZ

And the image:

The Google-o-meter has a funny name. Here is an example of its usage:

import pygooglechart as gc

chart = gc.GoogleOMeterChart(440, 220, y_range=(0, 10))
chart.set_pie_labels(['Awesome'])
chart.add_data([8])
chart.download('gom.png')

It produces this URL:
http://chart.apis.google.com/chart?cht=gom&chs=440×220&chd=s:x&chl=Awesome

And the image:

Well, have fun. Please write a ticket for any bugs, patches or feature requests you have.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Mixing IPython and autoimp

IPython is great for typing up quick tests. Unfortunately it is annoying when you have to import modules just to use it for one or two commands. autoimp is a Python module where it automatically imports modules when you refer to them. It works well with IPython simply by adding “import_all autoimp” somewhere in your “~/.ipython/ipythonrc” file.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Release Update

Just a quick update. I have recently made some Python releases.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

BetterPrint

I find readability very important when reading data. It saves time and energy. To me, Python’s pprint module is not easy to read in a lot of cases so I made my own. This is how pprint looks like at the moment:

BetterPrint is based on a lot of the source code, which looks about 10 years old. It makes more sense to me to have braces aligned by column rather than jammed together inline. BetterPrint also introduces colour to increase readability even more. As you can see from the following screen shot:

BetterPrint is compatible with Python’s pprint module, so you can simply “import betterprint” aliased as pprint like so:

try:
    import betterprint as pprint
except ImportError:
    import pprint

If you have setuptools installed simply run “easy_install betterprint”, or visit the web page for more details and installation instructions.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

SQLAlchemy Cheat Sheet

I’ve made a small quick reference guide to SQLAlchemy if anyone is interested. I’ll update it as often as I have to look up something in the official documentation.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Releases and Projects

For the lack of updates, here is an update:

  • Just released pycallgraph 0.3.1 which fixes a few bugs.
  • This week hopefully releasing a pre-alpha of c80 which is an AJAX IRC client.
  • Development of VirtuShare, a transparent peer-to-peer networking service, is still in progress.
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

pyraknet 0.1.4

I have released pyraknet 0.1.4! For those who don’t know… it lets you easily apply multi-player networking to your python game pretty easily. Enjoy!

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Python Call Graph 0.2.0

Within 24 hours of releasing version 0.1.0, I’ve released 0.2.0 of pycallgraph. It’s just so fun to play with Graphviz…

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Python Call Graph

I have just released pycallgraph which can create call graphs for Python programs. Quite useful for working out how Python programs work and to see if you have any problems in your program flow.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

pyraknet 0.1.3

I’ve been releasing new versions of pyraknet like a mad man. pyraknet 0.1.3 has been released with the major change being a new Windows binary for Python 2.5. Other things include documentation, tests and one new method.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

pyraknet 0.1.2

I have released the multiplayer network library pyraknet 0.1.2 with a few more features. I’ll be updating it with some more features soon.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Django time zone problem

This is a Django bug specific to Windows and it took me half a day to figure out because I trusted the datetime module! Basically if you set your TIME_ZONE in the settings.py file like so:

TIME_ZONE = 'Australia/Sydney'

The datetime and time modules will always return the UTC date.

The strange thing is that if you use “python manage.py shell” you get the bug but if you just use “python” you don’t get the bug.

Correct time:

c:\project>python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2006, 9, 28, 12, 11, 38, 109000)

Django time:

c:\project>python manage.py shell --plain
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
(InteractiveConsole)
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2006, 9, 28, 3, 12, 16, 468000)

Possible solutions:

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Creating a Django cron job

I couldn’t find a way to directly call a Python function in a Django application view from the command line. It doesn’t seem like it is a common thing to do from my Google search attempts. In this example I have a function defined to download a few web sites once a day using a custom made Django model. The project is called mytestproject and the application is called mytestapp. Here is the views.py file:

import urllib2
from models import WebSite
def daily_job():
    for site in WebSite.objects.all():
        page_html = urllib2.urlopen(site.url).read()
        do_something_with_page_html(page_html)

To run this function from the command line an optimist would create a python script that looks like this:

#!/usr/bin/env python
from mytestapp.views import daily_job
daily_job()

Running this will give you an exception about your DJANGO_SETTINGS_MODULE environment variable not being defined:

EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

Lets change the script a little to conform with Django’s demands.

#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from mytestapp.views import daily_job
daily_job()

Please note that according to the documentation, DJANGO_SETTINGS_MODULE should be ‘mytestproject.settings’ instead of just ’settings’. Using the project name in DJANGO_SETTINGS_MODULE will cause troubles in our situation because Django does some tricky things to your path before importing the settings module. For our needs it isn’t necessary to do this.

Of course you can make this script a little more generic so you can run an arbitrary script from your cron job if you feel the need to:

import sys
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

module_name = sys.argv[1]
function_name = ' '.join(sys.argv[2:])

exec('import %s' % module_name)
exec('%s.%s' % (module_name, function_name))

I’ll call this script run.py. To run my daily_job function:

python run.py mytestapp.views daily_job()

And your modnight cron job entry should look something like:

0 0 * * * python /path/to/mytestproject/run.py mytestapp.views daily_job()

And there you have it. I hope this will save you some time!

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon

Game programming compeition - PyWeek 3

Slowchop Studios have registered in PyWeek 3. The competition runs for a week and starts on the 3rd of September.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Reddit
  • StumbleUpon