Saturday, September 29, 2007

PlotKit and MochiKit

I needed to draw some graphs on a web page dynamically from data from a command line application so I took a look at Plotkit which, apart from having some very nice looking output, has quick methods to draw a graph from the contents of a HTML table on a page (DatasetFromTable - perfect for what I want) or the EasyPlot method that draws a chart in one line of code (or two if you count the HTML).

Poking around in there, I took another look at Mochikit which is a requirement for Plotkit. Mochikit's motto is making "javascript suck less" which it succeeds at very well. It provides the $() found in prototype and has a simple, clean set of libraries for such things as DOM handling, event handling and asynchronous event handling. It also has the javascript equivalent of Python's itertools and a logging module that can send messages to the console in Firebug.

Firebug is a Firefox extension that with the ability to inspect CSS, HTML and Javascript and a visual debugger makes web development simpler than ever.

Monday, August 20, 2007

Tips and Tricks: strings and formats

What if you want to output string data in a formatted fashion? You could use a format string in the same way that you would if writing PHP, C, Perl etc.. where %s is replaced by a string, %f is a float etc then you could end up with something like:
print "%10s is a filename"%stringvar

which would print the contents right-justified in a 10 character field. What if you wanted left-justified?
Well, then you would replace %10s with %-10s. Obvious, huh?

You could take advantage of some of the useful methods that Python strings own such as:
  • rjust - presents a number or string right-justified in a field of specified width (there is an ljust as well)
  • centre - presents the number or string centered in a filed of the specified field width
  • zfill - to present a number or string of a specified width padded out with zeroes as needed
and use them like this:
print "%s is a filename"%stringvar.rjust(10)

Of course, this is only useful if you are writing to a plain text file or a terminal. What if you are writing an HTML output? You could use named parameters like this..

print "%(noddy)s is a filename of %(max_len)d characters"%(max_len, noddy)

Notice the parameters are the wrong way round in the format specifier compared to the parameter list?
This can make constructing long format strings simpler. You can take this a step further by providing a dictionary instead of a list of parameters, like this

my_dict ={ 'name':'/var/log/message', 'code':1}
print "Examining %(name)s returned code %(code)i" %my_dict

Pyglet

Pyglet is a cross-platform windowing and multimedia library for Python.. uses OpenGL and has a clean API.

I'll take a look and write it up.

Saturday, July 28, 2007

Indexing the Sky

Indexing the Sky
SETI may be the Search for ExtraTerrestrial Indexes.

Rectangle classes in python

This will be useful for mapping applications.. assuming it works well. I keep hoping to find time to build something with Google maps and Python people data. Maybe this will help.

Sunday, July 15, 2007

If it looks like a Duck, it may be a Parrot..

Last Saturday (July 15), the DFW Pythoneers gathered together to hear wise words from Patrick Michaud. "Lo", he said, "it is not an ex-parrot (though it may be pining for the fjords.." and we listened in solemnity.

PM gave us another great talk and explained why Perl 6 is important/interesting/intriguing to Python people and how we can get pizza paid for by the Perl foundation while looking at Python.

Parrot is the VM for the upcoming Perl6. It also happens to be capable of 'running' Python (and a bunch of other languages, some of which you will wish you had never heard of if I told you about them so I won't. You're welcome) and Patrick gave us a run-down on how the Python sourcefile in all of its beauty gets transformed into bytecode for Parrot. It's less than simple so you can go ook it up yourself at parrotcode.org.

After the meeting, I checked out the latest Parrot code and tried the test suite. Yay, it worked (good start) and then I tried the command line prompt for the Python part of Parrot, pynie. It also worked but things got a little bumpy after that:
  • longs appear to be broken but aren't
  • floats are broken, along with imaginary numbers
  • list, arrays and dictionaries are less than working
It's a work in progress and I am not sure how to fix the BNF grammar that's written in Perl6 Regex syntax so sent Patrick an email and called it a night.

Sunday, July 8, 2007

Django Flat Pages are cool

Turn your ever-growing linear website into a Django website with all that leading edge buzzword goodness!

I have been helping a friend with a website and it, like Topsy, just growed. It went from being manageably small (not too many pages) to large enough to be a problem (too many pages). Think it doesn't sound so bad? Imagine having to fix the copyright date on each page. That's not a good example because I was careful to use a standard layout.

Obviously the solution is to use templates but I didn't want to edit the content and add the template on my machine and upload everything so I wanted server-side templating (as opposed to rendering the pages client-side and uploading them, like they do for python.org).

My preference is to use Python for, well, just about any reason and then it hit me! Use Django! Or aanother framework, after all there are 80+ at the last count. But use Django! It's sexy! The voices in my head finally convinced me and I sketched my requirements:

I wanted
  1. really straight-forward templating
  2. WYSIWYG editing in the browser so the client can fix the content (if they want)
  3. access control (so bad people don't do bad things)
  4. a well-explained system with a user-base
  5. ease of implementation and support (this should have been nearer the top of the list)
  6. Simple urls, somehow mirroring existing URLs. Don't want to edit lots of pages.
The voices seemed to be right! Use Django! But how to do number 2? Using flatpages we can get a plain text version of WSIWYG but that's not I had in mind. I cast about and found TinyMCE (not hard to find, really, it's everywhere and for good reason) which is a small javascript-based editor that can beconfigured to be about as compicated as you want. Followed a recipe on the Django website and, presto chango! Nice looking WSIWYG!

After I fixed the problem where TinyMCE decided to rewrite URLs (wanted to leave that to Apache to solve number 6), it was all uphill to the finish line. Apache rewrites most URLs to send them to django, except for the fixed asset stuff (like CSS, images, forms for download etc).

What I may do, for a giggle, is have Django write a copy of the rendered page to the filesystem and server all content as static. Hm.. maybe not. What might be good is to version the pages - add a post-save so that we keep some amount of history.

Monday, June 11, 2007

Issue Number 2

I discovered cssutils tonight.. I thought there must be a parser for CSS in the Python universe somewhere.. and it's called cssutils, available from the Cheeseshop.

I installed it (using the magic of easy_install) and then broke it using the css file from HelixPlayer (the open-source portion of RealPlayer) so I raised an Issue.

Issue No. 2 as it turns out.

Thursday, June 7, 2007

Unit testing.. why wouldn't you?

Unit testing is a good thing, right? Especially if it is simple to set up, simple to run and simple to read the results. How do we do it in Python? Simple!
Here's a quick example using some pieces from the unit test file I checked in along with the python bitset:


import unittest
from pybitset import Bitset # import class or module under test

class bitset_testcase(unittest.TestCase):
def setUp(self): # testcase setup
self.bits = Bitset(10)

def testSize(self): # some method to test
# use assert to compare the expected and
# actual results
assert len(self.bits.bitstring)==self.bits.size(),
'Incorrect Size'

def testRepr(self): # some other method
assert self.bits.size() == len(self.bits.__repr__()),
'Repr size incorrect'


# .. if you need to test exceptions then write some code to
# provoke an exception and catch the exception and add an else
# clause to deal with exception failure

def testIndex(self):
try:
self.bits[40]=1
except IndexError:
pass
else:
self.fail("Out of range index expected exception")

# add the check for main so you can run from the command line
if __name__ == "__main__":
unittest.main()

Presto! Try it out, it's easy.

If you don't like my notes, go and read this

Python bitset checked in (along with unittests)

I have just checked in the python version of bitset (and unittests!) into the boost_python project directory. Now I need to spend a little time making the API for the python version agree with the C++ version. Some discrepancies around init/constructor, nothing major.

I think I have enough functionality to start on benchmarks.

The python bitset uses a list internally to store bits as, can you guess, zero or one. The first crack used the string representation of a bit rather than a numeric which was not a big problem until I noticed that all operations except init and repr needed to convert the value somewhere.

One gotcha to note: the zero-th bit is rightmost, as it would be if you were writing out a number rather than the string layout where the zeroth bit is on the left.