Debugging Django apps with Eclipse

August 30th, 2010 Alexander Posted in Agile, IT, Programming | No Comments »

Yes you can use Eclipse/PyDev as a graphical debugging tool with Python. The trick is to set up your Python environment correctly within Eclipse. When that is done you can harness the power of setting breakpoints and drawing out information of the debugger by stepping through your code.

The path

I use virtualenv when running my Django projects. This way I can set up different Django projects on my development machine and not have them clash when they have different dependencies. I.e. for reise.no I  set up virtualenv like this:

virtualenv /Users/alex/workspace/reise_noENV/

On the command line I go into this virtual environment by issuing

source /Users/alex/workspace/reise_noENV/bin/activate

In Eclipse you'll also need to set up python path to point to this environment.

python-interpreter

After setting up your python environment we can head on over to running tests in debug mode.

Creating a debug runner

Okay, running tests in commandline mode is as easy as doing "python manage.py test". So taking that into regard we'll create a debug runner for Eclipse that does this.

Choose "Run->Debug Configurations" and we'll set up a PyDev Django runner:

pydev-debug-runner

From this screenshot you'll see that we have created a PyDev Django configuration. I'll name it "reise_no test views". Now you can click the "Arguments" tab and fill in "Program arguments" with what you on the command line would enter after "python manage.py". So here we'll enter "test".

Running

You can now run this configuration either via the Debug button you can find on your right in the current dialog. Or you'll find it under the bug icon. Click the small arrow next to the bug and a dropdown list with all your debug configurations should appear.

So add a couple of breakpoints in your code by right-clicking in the margin of the line where you want to break, and run your test via the debug runner. When you hit a breakpoint Eclipse should now go into Debug View.

Debug view

debug-controls

In the Debug View you can now step through your code with the debugger controls. The two controls you'll use the most are

  • Step into (F5): Go one level deeper to see whats going on in there.
  • Step over (F6): Go to the next line.

You'll also have a list of the current variables that are set. Use these to figure out whats wrong with your code.

debug-vars

Okay, thats it. Now get rid of all those print statements you've been so shamefully using and start using the tools at your disposal :)

AddThis Social Bookmark Button

Creating unique IDs with NoSQL

May 12th, 2010 Alexander Posted in Programming | No Comments »

I wanted to have unique user friendly IDs for an application I'm working on. No long, hard to type in customer reference numbers. And using MongoDB I didn't have that nifty autoincrementing sql.

The way I solved it was using MongoDBs update command. That command takes an atomic modifier $inc. So you'll need to create a c collection with one document, and increase a value on that counter. Then I'll base64 encode the value to have a slightly more user friendly value. Especially when the sequence grows in size. This is all using Python.

Creating my document:

db.counter.insert({'count': 0})

And here's my unique id generator:

def unique_id():
    db = get_database()
    val = db.command('findandmodify', 'counter', update={'$inc': {'count':1}})['value']['count']
    b = base64.b64encode(str(val))
    result = b.replace('=', '')
    return result

You'll now end up with IDs looking like this:

MA
MQ
Mg
...
MTAwMA
MTAwMQ
MTAwMg
MTAwMw
AddThis Social Bookmark Button

Python value objects

May 11th, 2010 Alexander Posted in Programming | No Comments »

When your Python class is essentially a bag of values creating a class for them is a lot of writing - because what you essentially want is to automatically store these values with as little fuzz as possible. So instead you can use the Python 2.6 collections.namedtuple trick.

So instead of doing it one of the old ways:

class Foo:
    bar = None
    gazonk = None

    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            setattr(self, k, v)

You can now do it like this:

from collections import namedtuple
Foo = namedtuple('Room', 'bar gazonk')

You can now init your Foo class with either *args or **kwargs.

AddThis Social Bookmark Button

Testing Django apps with MongoDB

April 14th, 2010 Alexander Posted in Agile, Programming, Web development | 1 Comment »

I've fallen in love with #NoSQL. So I'm doing my storage with MongoDB for an application I'm working on. But since I'm also a testing zealot I had to figure out how to test my app properly, with fixtures and all.

There's already some good code out there that will help you develop your Django app using MongoDB - namely MongoKit and django-mongokit.

MongoKit is a great way to make a structure for your document. Complete with validation and all. And by combining it with django-mongokit you also get automatic test database create/drop.

models.py:

from django_mongokit.document import DjangoDocument
from django_mongokit import connection

class Country(DjangoDocument):
    structure = {
        'iso3': unicode,
        'iso2': unicode,
        'name': unicode,
    }

    required_fields = ['iso3', 'iso2', 'name']
    use_dot_notation = True

connection.register([Country])

tests.py

from django_mongokit import get_database 

from django.test import TestCase
from models import *
class ModelsTest(TestCase):
db = get_database()
def test_should_save_country(self): country = self.db.countries.Country() country.iso2 = u'GB' country.iso3 = u'GBR' country.name = u'UNITED KINGDOM' country.save() self.assertTrue(self.db.countries.find())

But the fun doesn't stop there. You can also easily add some fixtures by creating a python file to hold some dicts for you:

countries.py:

countries = [
{"iso2" : "AF", "iso3" : "AFG", "name" : "AFGHANISTAN" },
{"iso2" : "AL", "iso3" : "ALB", "name" : "ALBANIA" },
]
And add this to tests.py:
from countries import countries

class ModelsTest(TestCase):
    def setUp(self):
        db = get_database()
        db.countries.insert(countries)

    def tearDown(self):
        db = get_database()
        db.drop_collection('countries')
Your countries collection will now be populated every time TestCase is created, and dropped when the tests are done running.
AddThis Social Bookmark Button

Backpacking Brighton

July 18th, 2009 Alexander Posted in Travel | No Comments »

I'm in Brighton at the moment, and therefore made a map of some of the nice galleries and places to see there.

View Backpacking Brighton in a larger map

AddThis Social Bookmark Button

Greenhopper acquired by Atlassian

June 2nd, 2009 Alexander Posted in Agile | No Comments »

Greenhopper, an agile project management plugin for JIRA has been acquired by Atlassian.

This is a great plugin that creates broad agile capabilities to JIRA - which means that agile teams can use JIRA not only as a bug tracker, but also as a planning board for their sprints. It got some great reporting tools complete with burndown charts.

We implemented Greenhopper for the teams at NHST Media Group, and it really helped the execution of our agile processes

AddThis Social Bookmark Button

Aspects with Spring and Maven for getting rid of singletons

January 24th, 2009 Alexander Posted in Programming | 1 Comment »

We wanted to update some old hibernate DAOs we had laying around which were implemented using singletons.  Instead of using singletons we wanted to wire these up using the Spring context. This was seemed to be an easy refactoring task. However, diving into the code we came to realize that we had several taglibs developed (extending from SimpleTagSupport) that was using these DAOs. And since you can't have constructors for these tags to dependency inject our DAOs we had to look elsewhere.

Read the rest of this entry »

AddThis Social Bookmark Button

rechargenews.com released

January 16th, 2009 Alexander Posted in Web development | No Comments »

rechargelogo

I've lately been spending my working hours getting rechargenews.com up and running. And now its here! The web release was friday 9th, and the first printed newspaper came out today.

All the design was delivered to us from apt, but we implemented the design and controllers inhouse. And I'm almost a bit proud to say that this has become one of the most visually and user friendly news sites on the web.

Rechargenews will deliver news and editorials on renewable energy - an important issue in today. And it is my hope that it will influence people to make choiches that will lead to a habitable planet in the future.

Using scrum on this project has made us able to deliver on time, and with an acceptable feature set. More will surely come in the future, but the important bits are already there.

This release marked the end of my commitment for this project at this time, and I will be moving my attention to the new work that needs to be done to dn.no - the leading financial news site in Norway.

AddThis Social Bookmark Button

Remove bottlenecks and optimize the web with Javascript

December 16th, 2008 Alexander Posted in Web development | 2 Comments »

speed boat

There are times when you want to embed 3rd party elements into your web pages. If you have a high traffic site which has time critical output, you might come to realize that your 3rd party vendor doesn't have the same uptime as yourself, resulting in delayed GET statements that seem to bog down your site.

Here you are left with a couple of choices.

  • Ditching the supplied elements all together, usually a very unpopular decision.
  • Caching it - which might be a problem with licensing and time critical elements.
  • Use JavaScript to fetch those elements - as explained in this article.

The goal here is to give a usable page to your visitors as quickly as possible.

Read the rest of this entry »

AddThis Social Bookmark Button

The FRA law may make the internet a more secure place.

September 28th, 2008 Alexander Posted in Social Media | No Comments »

I live in Norway. And with the advent of the new FRA law in Sweden a lot of shouting about political intervention and pressure has come forth in the local media. Big companies in Norway are asking if there can be ways to route traffic around Sweden to their destination. Complaints have been lodged to the EU. And philosophical discussions have surfaced that this is a way to treat everyone as a criminal until proven innocent - turning the entire legal system on its head.

However, I won't discuss this, but another aspect which hasn't been discussed much. This case gives the public insight into how the protocols of the internet work: Internet traffic crosses borders. This is a fact. Even if sender and recipient is in the same country traffic can cross national borders. This is how this technology is built, and is even considered one of the strengths of the internet. As one node goes down traffic will still find a way through somehow.

Read the rest of this entry »

AddThis Social Bookmark Button