37 entries tagged python

Picky Picky Game: Tidying up the Python code

Having got the first working version of the Picky Picky Game, I have naturally now pulled it apart again. I decided that now it is in a state where it makes sense to try to package up a version for Adrian to try installing, I better think about getting the module and package names right, since it will be harder to change them later.

I have reorganized my Python classes in to their own package pdc (designed to prevent name collisions with WWW-oriented packages by other people). I also changed some of the file names—so that ‘import httputils’ becomes ‘from pdc import www’.

There is now a proper unit-test suite for the www module (which has functions like urlDecode, urlResolve, and xmlencode). This is easier to do for this odule than the others, which tend to involve creating scads of HTML text which will be hard to check for errors. For the URL-manipulating functions, the unit tests turned out to be invaluable—there are a lot of corner cases that I only sorted out because I had tests for all of them.

Windows Unicode versus Python's Unicode

During an attempt to better organize a collection of T-SQL scripts I came across a strange behaviour when converting Windows ANSI (8-bit character data) to Unicode (16-bit character data) which appears, surprisingly, to involve the ancient MSDOS convention of using Ctrl-Z (character 0x1A) to end files.

What do you mean, Ctrl-Z?

Processing XML with Python Mac OS X

I’ve been amusing myself by concocting an RSS reader using XSLT to do the processing. XSLT can even handle the downloading of the RSS files, but this does not allow for caching or aggregating—so I thought I would knock something together in Python.

Installing pyXML

Now with BMP support

To make it easier to create pictures for the Picky Picky Game, I have added code to automatically convert Microsoft Windows Bitmap (.bmp) files into PNGs.

This is straightforward because I already was using PIL to check the dimensions of the images. Converting to PNG (if the format is one PIL knows) is pretty simple:

permittedTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/gif']
...
if not imt in permittedTypes:
    buf = StringIO.StringIO()
    im.save(buf, 'PNG')
    data = buf.getvalue()
    logger.log(slog.WARNING, 'Converted your image from %s to image/png' % imt,
	       'This may have lead to a slight loss of image quality.')
    imt = 'image/png'
    buf.close()

The above goes in the sequence of checks on uploaded images (after the check for width × height, but before the check for number of bytes). I think I spent longer creating a BMP image to test it on than I did writing the new code!

The advantage of BMP support is that, if you have Microsoft Windows, then you definitely have Microsoft Paint installed. So long as you know about Start menu → Programs → Accessories → Paint, and the Image → Attributes menu item, you can create panels for Picky Picky Game.

Generating the printed comic with ReportLab

My Percy Street comic strip is published on-line at 100 dpi, using HTML to arrange the panels on the page. I draw the pages at 300 dpi, which should be just high enough resolution to print on paper. The printed version uses PDF rather than HTML to lay out the page. (The page has to be laid out because I am drawing each panel as a separate image.) Read more

ASP’s Response.Write disaster

Were you foolish enough to be creating a web application with ASP, then you will be used to using the Write method of the Response object to stream HTML to the web browser of your client. Today I got caught out by a serious limitation of the implementation of Response.Write. (And this is not the first time it has leapt out and bit me, either.) Read more

It's a bit like Markdown

I have decided to change the way my home site works. Up until now I have been writing entries by creating a quasi-XML file containing the HTML text; I am changing the format to be quasi-RFC-822: a text file with a short header section at the top. The text is translated to HTML via the usual hacked-together nest of regexps. Read more

Migrating my website workspace, part 1

My website is maintained by a rather complex amalgamation of software, accreted over generations. Having migrated it from my old desktop lickity to my new(ish) PowerBook Ariel, I now want to migrate it again to my new server Tranq (a Tranquil PC T2); this will allow me to use cron to keep some parts up-to-date automatically. Read more

Death to Java! Death! Death! Death!

At work I keep my work-log and my work plan in one big XML document (actually one per client, for the sake of limiting the enormity of the files). This single document is then sliced and diced through various XSL transforms to create different views: for example, when you look at the entry for one task, you see all the notes describing that task and the work I did on it, and when you look at the log for a particular day, you see the notes for that day regardless of which task they belong to. Read more

TurboGears and OpenID

I thought I might have a go at familiarizing myself with TurboGears, one of the new Python web frameworks, by writing a new and improved clone of the Picky Picky Game. Step one was verifying that I can upload pictures easily. Step two was getting an identity system put together. Creating a registration page and log-in form and so on is the obvious and dull solution, so I thought I’d have a go at exploiting OpenID instead. Read more

First Adventures with CouchDb

I’ve just run my first client program against the CouchDb server. It doesn’t do much yet, but it shows that I’ve managed to install and build the thing successfully. Read more

First Cut of PyCouchDb

I have been experimenting with CouchDb by creating the beginnings of a Python API for it. It is not really ready for prime time yet, but in best free-software tradition I am publishing early and will add to it when I have time. Read more

Dictionary Literals in C#

C# Does not have a nice way to represent a dictionary as a single value: you can only create an empty dictionary and add entries one by one. Which is annoyingly verbose if you are used to a more reasonable programming language, Read more

Minecraft Texture Maker

I have started working on a little utility program for assembling Minecraft texture packs. Its working title is Minecraft Texture Maker. With it I an do mundane tasks like assembling the files in a directory in to the ZIP archive that gets published, and also create new texture packs by remixing existing ones. I have a vague ambition to elaborate it in to a web app for remixing packs, but it is useful already. Read more

Behold! As I Mock Time!

I believe in Test-Driven Development but had somehow had never gotten around to using mock objects until a few months ago. They’re super-useful when testing classes that write to files or query remote databases or what-have-you, or when the rest of your system is big and hairy and setting up tests takes ridiculously more work than the test itself. Read more

Attic Pluck: Alleged Tarot 2002

It has taken only ten years but we now have SVG support as good as it was ten years ago using Adobe’s SVG Viewer plug-in in Netscape 4. I have decided it is about time I resurrected my 2002 project, the Alleged Tarot. Read more

The Imaginary Cats of St Ouses

I have started building a toy web application to give myself something to try out some of these new-fangled JavaScript frameworks like React and Redux. But first I needed a fake data server to supply the data for it to serve. Read more

Plato, a Python module for plotting plates

As part of my new hobby to build a computer keyboard, I have to draw the instructions for the cutting of the plate the switches are held in (see previous post for more on what the plate is). Rather than learn how to install and use a CAD system to do this, I have written a Python module to do it for me. Read more

Porting my Blog to Python 3

My last Mac OS update replaced Python, which as usual broke the virtualenvs I use and when building a new virtualenv it seemed like as good a time as any to upgrade to Python 3 and Django 2.0. This lead to a lot of ‘adventures’ as I discovered how well Python 3 is or isn’t supported in my setup. Read more

Ooble: Work in Progress

New side project: I am working on a microblogging app called Ooble. The aim is to be able to replace my use of Tumblr for linking to web pages I find interesting and making short notes. Read more