7
Python code metrics
Recently at work I’ve been pushing to start tracking some metrics on our python code base. There are a few tools out there like pygenie, pychecker and pylint. These seem to be the leading code metric tools for python at the moment. Where I work we have eagerly adopted pylint in our daily use of python.
Pylint seems to be the most useful for the day to day developer. It’s fast and flexible, you can run it on the file you’re working in,a group of files or the entire project.
There are some problems with all of these tools when it comes to frameworks. I’ve been using django alot lately and with the way that it has it’s settings.py file integrated it kind of tricks these lint tools. So you may see errors saying something like this…
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
There are a couple django-lint checkers it appears.
django-lint is a project that appears to have died. I’ve tried to use this a little bit, but I haven’t really had much success.
It appears however that the project has moved or branched http://chris-lamb.co.uk/projects/django-lint.
I haven’t tried this version but I imagine it would be better since it was last updated in March 2010.
You can also set the DJANGO_SETTINGS_MODULE environment variable like this…
$ export DJANGO_SETTINGS_MODULE=mysite.settings $ django-admin.py runserver
So here’s my story:
I have some surprising elements that came out of looking for lint tools for python or any other programming language that you’re working in.
One, was that my team latched on to the pylint scoring mechanism instantly and it became a game of who could get a perfect score over all the code they were working in. This is good for a few reasons, and I challenge those nay sayers out there.
Warning: POINT ahead!
Ok yes, maybe “your” coding style isn’t accomodated in pylint…you know what! It doesn’t matter. The POINT is that everyone is meeting a STANDARD.
That’s the point! I don’t care if no one can get a perfect score unless all their variables start with “i” (which I assume is Apple’s policy). The power that comes with everyone driving, pushing and accelerating in the same direction is far far far more valuable than “your” coding style.
4
Python GeoIP (python-geoip) cities tutorial
Using geolocation is something that people are doing a lot lately. You may have noticed twitter.com in FireFox showing a little bar at the top asking if it’s alright to share your location. This is so that when you tweet you can have your location show up there. That way you can keep track of where you tweet from. There are many uses for this kind of information and these days there’s a lot of free things out there to help you get started with your geolocation project.
Installing GeoIP
Requirements/Dependancies:
- Python 2.4+
- python-geoip
- libc6
- libgeoip1
More on dependencies here http://ns2.canonical.com/es/karmic/python-geoip. But don’t worry about these since most of them get installed by the package anyway.
A couple of basic principles before we get started.
- Geolocation is gathered from an IP address.
- There has to be a database that connects the IP address to a geographical location
I’m going to be using Python here because frankly it’s powerful, easy and has awesome libraries for geolocation. Which brings me to the GeoIP library! I’m using Ubuntu 9.10 so most of these libraries will just take an apt-get to install.
Then you can install python-geoip with
sudo apt-get install python-geoip
Or you can get the source from http://geolite.maxmind.com/download/geoip/api/python/
Now that you have this installed you can test it with the following code put in the python terminal.
>>> import GeoIP
>>> gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
>>> print gi.country_code_by_addr("203.195.93.0")
I got that from MaxMind’s tutorial http://www.maxmind.com/app/python. At this point you have the ability to track IPs down to the country level. What you probably really want is to go down to the city level.
Geolocation – Cities
If you call some of the other functions on the GeoIP class like record_by_addr() you’lld get an error like this
“Invalid database type GeoIP Country Edition, expected GeoIP City Edition, Rev 1″
5
PHP5-CLI versus Python CLI
I just thought this was kind of interesting. Out of the box CLI comparison of PHP and Python type errors
Python
>>> test = 123
>>> test2 = "ewfwef"
>>> test / test2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'int' and 'str'
PHP
$test = 123;
$test2 = "werwe";
echo $test/$test2;
Warning: Division by zero in Command line code on line 1
It’s nice that python tells you what is really wrong here, you can’t divide an integer by a string! PHP on the other hand implies that $test2 is equivalent to 0.
It should be noted that python is doing a trace and PHP is not. Doing a trace on the error in PHP would give more information but since PHP is very weak with types, you would probably have to notice the error on your own.
10
# Function/Method Decorators
One of the reasons I like Python is because it’s simple but powerful. All the tools are there for whatever task needs to be done. I’ve found __getattr__ to be a very useful tool for creating sets of standardized class methods without having to explicitly declare them. Another useful tool is function decorators. Read on to get a quick idea of how to make your own class decorators.
9
# __getattr__ Deserves Some Respect
One of Python’s built-in functions is __getattr__, which turns out to be quite useful but at the same time slightly dangerous. It is especially helpful for creating complex classes without using inheritance. For a brief tutorial, read on.
25
# Python Dictionary Coolness
Lately I’ve been working with dictionaries, specifically as a device for storing settings. I ran into a little annoyance and thought of programming a little routine to take care of it. Come to find out, Python already has such a routine built into the dictionary class. Read on to find out more.
24
# OpenGL in Python
Somebody has worked out an OpenGL wrapper for Python. The installation steps are here, but I found them a bit non-trivial. Here’s what it will take to get going.

