Dugan Chen's Homepage

Various Things


Building SDL2 programs with CMake and PkgConfig

Here’s what I feel is currently the easiest and safest way to build SDL2 programs. First, you should never be writing Makefiles by hand. You should be generating them. And for generating Makefiles, CMake is more or less the current standard. How does CMake detect where a library and its header files are installed? Well, […]

Reading Environment Variables in Python. With Metaclasses.

I work in the animation industry. Here, the standard practice is to use environment variables to control the runtime behavior of software. It is common enough, in fact, that powerful open source projects such as Rez have been started to manage it. The idea is that before launching each application, you set the environment variables […]

A Humble Dialog implementation in PyQt

The following is an implementation of the Humble Dialog Box pattern (no relation to the Humble Bundle) in PyQt. Honestly, no further explanation should be necessary. The paper opens with a screenshot of the dialog box being used as an example. I have a full implementation of that dialog box’s user interface. The rest is […]

Unit Testing PyQt Applications With Nose and Mock

I spent a couple of evenings getting familar with the Mock mocking framework for Python. Turns out it works very well for testing PyQt code. Let’s say you have the following program, which I just contrived up. It’s very contrived, because it ignores obvious and simpler implementations such as using the QWebView‘s setUrl method. Nevertheless, […]

Implementing Pythonic mixins with class decorators.

There was virtually no discussion following the posting of this PEP, meaning that everyone agreed it should be accepted. Here is the correct and Pythonic way to do mixins in Python. You don’t. You use class decorators to do the same thing. Note that class decorators were added to Python as of 2.6. I suspect […]

A Point Free Function Composition Operator For Python

Here’s a quick implementation of a point free function composition operator for Python: The composable class works as a function decorator: The following test prints out “dcba”, which is what you would expect if you start with “d”, append “c” to it, append “b” to it, and then append “a” to it: One use case […]

Rendering Django forms for the Twitter Bootstrap

There seem to be dozens of projects to integrate Django with the Twitter Bootstrap. All of them seem to be large enough to need permanent homes on GitHub. If all you need to do is render a form the way Bootstrap expects, however, then the process is easy. The total amount of code needed is […]

Django ModelChoiceFields as autocomplete lists

Every Django user knows about its ModelChoiceFields. Pass them to a form template, and they render as HTML Select elements via which a user may choose an ORM moodel. Use them for form binding, and they’ll find you, from the form POST data, the ORM model that the user chose. But what if you don’t […]

Test Driven Development with PyQt Maya Plugins

Do you want to use PyQt to develop your Maya plugins? Of course you do; the alternative is Mel. Do you want to develop these plugins via the process known as test driven development (or a variation, as shown below)? Again, of course you do. Any other process would be inefficient. Well guess what. You […]

Elegantly Adding HTML5 Validation Attributes to Django Form Fields

The current version of Django, 1.4, does not use HTML5’s Web Forms 2.0 attributes. Later versions probably will, but here’s an elegant way to add them in the meantime. What you probably don’t want to do is this: That rolls back your reasons for choosing Python and Django. It turns out, however, that that creating […]


« Previous PageNext Page »