Archive for the ‘python’ Category

DSL in Python

Sunday, September 6th, 2009

Fernando Meyer has made a nice DSL in python. It’ll be interesting to see where it heads. He just modifies the file using python’s tokenize library instead of AST. He hasn’t implemented import hooks to bootstrap it yet, but that should be easy to do.

This is another technique that could be used to implement the AST magic I posted about previously.

AST Manipulation fun for when I have time

Sunday, November 2nd, 2008

Nice asserts

Currently python asserts are of the form

assert EXPRESSION, MESSAGE

It’d be really nice if an assert of the form

assert a < b < c

would display display a message equivalent to this (with a = 2, b = 1, c = 3)

AssertaionError: 2 < 1 < 3

I think I can do this for most Boolean expressions by doing an AST transform. So something like

assert a.foo() < g.bar() != f("fizzle")

Would be transformed into:

_1, _2, _3, = a.foo(), g.bar(), f("fizzle")
assert _1 < _2 != _3, "%s < %s != %s" % (_1, _2, _3)

This would happen right before methods/functions were run. Hopefully, by using AST it will be more portable to other python implementations (This is the primary reason cited by Guido for why adding this type of assertion to the CPython implementation wont be supported).

Mutation Testing

Mutation testing plugin for nose. Mutate Boolean expressions and constants akin to Pester, only using an AST transform instead of text substitution. This could potentially increase the types of mutations possible (shuffle orders of statements executed, change parameters, etc.)

Difficulties:

  • Determing what’s under test to change (perhaps use coverage/figleaf to figure out what to change?)
  • Keeping track and reporting mutation results to tester (should be relatively straightforward)
  • Doing this quickly — mutations usually take a long time. (Only mutate files specified by user?)

Genetic programming

AST tree manipulation can also be used to generate syntactically correct statements automatically. Assuming I find or create an acceptable cross-over algorithm (perhaps steal the one from pygp) AST will make this relatively straightforward.

Random stuff so I don’t forget

Wednesday, October 29th, 2008

Freemind management of mnemosyne cards is an awesome idea! Create and organize subject matter in FreeMind, then export (leaves probably) to Mnemosyne cards with containing branches as categories. The tree could also be exported to a LaTeX memoir document, so it’s three in one: Tree, narrative, and notes. OOooh, I really like this idea.

  • Freemind needs a: spellchecker, automagically updated file view and clone nodes.
  • Use python 2.6 ast module to make pretty asserts that use the assert keyword.
  • the ast module could also be used to implement something akin to the Eiffel “old” keyword. But I think a registry of expressions to evaluate before the method and are accessable by name e.g.
    • old(expr1=lambda s: s.attr, expr2=lambda s: s.method()) and accessable by old.expr1, old.expr2
  • would be cleaner to implement.
  • Cmockery is hugs and cuddles, now all I have to do is add it to our make file for OS, mwahahahah!
  • Pythoscope equivalent for C would be interesting, and probably useful. Although really hard to implement…Whoever did it would become really good at C, though.
  • Contracts for python need to handle functions as well as classes (perhaps through wrapper class with __call__ or some such?)
  • Also looking at automake and autotools, makefiles don’t look as brain melting as they used to.
  • Automatically generating Sequence diagrams based on unit test or code execution would be AWESOME, pyumlgraph already does it for class relationship diagrams (generally, at least)

Design By Contract in Python

Monday, October 20th, 2008

One possibility is to vaguely mimic the interface C4J uses for Java with class decorators and additional classes (possibly inside the defined class).

I’m not sure if it’s a good idea to separate the contracts from the implementing code…But it would add a lot of flexibility to be able to add contracts to existing classes (via wrapping).

Nose test cases

Thursday, August 21st, 2008

So, starting with the previous blog post, I’ve turned the verbal description f the test cases into python code. I also implemented the necessary additions to urls.py and views.py to get them to pass.

Here are my “functional tests” These use the django testing framework, which means it’s pretty close to what a real user would experience. This file needs to be updated after I get some more functionality lower down with unit tests.

functional_tests.py

from django.test import TestCase

class IndexFunctionalTests(TestCase):
    urls = 'linkranking.urls'

    def testIndexReturns200(self):
         """The response to a get should have a 200 status code."""
         response = self.client.get('/')
         self.assertEqual(response.status_code, 200)

test_views.py
This file contains all the real test cases, currently it tests templates, urls and views.

from django.http import HttpRequest, HttpResponse

from linkranking import views

class TestIndexViewLayout():
    """Test the index view so that it returns an HttpResponse object that
    contains:
    * Submit Link hyperlink
    * A tag cloud
    * A list of the top Bayesian ranked links from the past week
    * Links labeled day, month, year, all time
    * A login/create account hyperlink
    """

    def setup(self):
        """Test the index view with an empty HTTP request object."""
        self.response = views.index(HttpRequest())

    def testShouldReturnAnHttpResponse(self):
        """View should return HttpResponse object."""
        assert isinstance(self.response, HttpResponse)

    def testShouldHaveStatusCode200(self):
        """View should return a status code of 200."""
        assert self.response.status_code == 200

    def testResponseShouldNotBeEmpty(self):
        """The response should not be empty."""
        assert self.response.content != ''

    def testShouldHaveSubmitHyperlink(self):
        """The response should have a submit hyperlink."""
        assert '<a href="/submit-link">Submit Link</a>' \
                in self.response.content

    def testShouldHaveTagCloud(self):
        """The response should have a tag cloud."""
        assert '<div id="tag-cloud">' in self.response.content

    def testShouldHaveListOfTopLinks(self):
        """The response should contain a list of the top links from the past
        week."""
        assert '<ul id="top-links">' in self.response.content

    def testShouldHaveLoginLink(self):
        """The response should have a login/create account link."""
        assert '<a href="/login">Login/Create Account</a>' \
                in self.response.content

    def testShouldNotHaveSettingsLink(self):
        """Anonymous users should not see a settings link."""
        assert 'settings' not in self.response.content

    def testShouldHaveTimeNavigationLinks(self):
        """The webpage should have links for month, day, year, all time and the
        words "week" for navigatiion."""
        assert '<a href="/today">today</a>' in self.response.content
        assert 'week' in self.response.content
        assert '<a href="/month">month</a>' in self.response.content
        assert '<a href="/year">year</a>' in self.response.content

So, I’ve generated a lot of very simple test cases to make sure that the correct pieces of the HTML output are there. I’m not sure if this is a good strategy (we’ll see in the future). Luckily these tests are high level enough that they shouldn’t change much when the underlining functionality changes.

One thing I’ve been debating is using BeautifulSoup to look up the tags in the output HTML instead of just poking at it to see if it directly contains strings. Using beautiful soup would definitely be more robust, but I don’t know if it’s necessary.

Doing TDD in Django

Monday, August 18th, 2008

So, as part of this link ranking website project, I’m trying to do TDD In Django, I’ve got a nice development setup with virtualenv with django installed locally in my development folder. My apps are added to sys.path through the PYTHONPATH environment variable and I set the DJANGO_SETTINGS_MODULE variable depending on what I’m developing at the moment. I also used easy_install to get the latest version of nose, my favorite test runner.

So, the first thing I do is write a simple smoke test to make sure that my application is setup correctly:

from django.test import TestCase

class IndexFunctionalTests(TestCase):

    def testIndexReturns200(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)

This requires the DJANGO_SETTINGS_MODULE environment variable to be set. I copied what django-tagging does, and have a tiny project settings.py specified within the tests folder. I also set up the urls.py file in the linkranking directory and reference it in tests.settings file.

This psuedo project will need to be expanded once I start developing templates. And I’m still running into problem #8358. I really should try this patch to see if it fixes it.

So far using nose with Django is pretty straightforward, if I use the Django TestCase object for all the database and client related stuff.

Other Todos

Tuesday, July 31st, 2007
  • Write a product() function for py3k if it isn’t already written
    • Done, patch rejected (product function not wanted as a builtin)
  • Install roundup for personal bug tracking
    • Generally use mind maps now for idea tracking, don’t really have a todo list system.
  • Investigating business object type things that are open source

(updated December 1st, 2008)

Blender Game engine tutorial

Sunday, July 15th, 2007

Alrighty, so it’s time to learn how to use the blender game engine. First we’re going to use the links here as a starting point.

We’ll also be doing some programming with livewire, since 2D is much easier than 3D, to start out with.

First, lets look to see what blender knows about objects in the game:

Game Engine Documentation

Now, here’s an example of what we can do with what blender knows:

Blender Scripting tutorial

So, we can access the properties of any object in the blender game engine, along with the sensors and actuators. The script is run in the game main loop automatically by blender, so we don’t have to worry about that.

Programming in livewires

To start off with, we need to installl pygame and livewires on your windows machine.

Lets start out working in the interactive interpreter provided by IDLE. So lets do a quick demo of what you can do.

from livewires import *
begin_graphics()
circle(200, 200, 50)

This has drown a circle at those coordinate within the window. This PDF contains other information that you may need to do stuff with livewire and graphics. Other PDFs on the livewires library are available here.

First we’ll just get used to drawing with livewires, then we’ll be able to make a game that takes user input.

Wiki Creole

Tuesday, July 10th, 2007

Ok, so one of my pet interests is wikis. Currently I don’t think there is any python parser for Wiki Creole, a generalized syntax for wikis. So I think I’m going to create a parser for Wiki Creole in python, using test driven development (TDD). It’ll first consist of only one function, that outputs valid XHTML. After that I might add support for custom tags, etc. Then I’ll re-implement in javascript, and then world domination :-D. It’ll also give me a chance to do some heavy lifting in javascript, does anyone know of a good javascript unit testing library?

Firebug is a lifesaver, but at the moment I can’t keep track of all of DOM and javascript in my head at once.

Ryan