Main contents

Archive for the 'python' Category

Python with COM

February 5th, 2009

The Win32 extensions for Python have been a lifesaver for me many times.  I consider them one of the best ways of scripting the windows platform.  Unfortunately there’s not as much documentation as one could hope for.

A useful trick I discovered recently is the following.  Say you’re trying to use a COM interface such as Microsoft Message Queuing.  You can create a message queue object as follows:

import win32com, win32com.client
serverInfo = win32com.client.Dispatch("MSMQ.MSMQQueueInfo")

However, the next step requires constants from the messaging API such as MQ_SEND_ACCESS.  These can be made available to win32com.client.constants in two ways.  One involves running makepy, but a more robust alternative is as follows:

import win32com, win32com.client
win32com.client.gencache.EnsureDispatch("MSMQ.MSMQQueueInfo")
win32com.client.gencache.EnsureDispatch("MSMQ.MSMQMessage")
from win32com.client import constants
serverInfo = Dispatch("MSMQ.MSMQQueueInfo")
serverInfo.FormatName = <format name>
return serverInfo.Open(constants.MQ_SEND_ACCESS, constants.MQ_DENY_NONE)

The order is important.  Once EnsureDispatch has been called, win32com.client.constants can be imported and will have the correct values available.

Posted in Programming, python | No Comments »

Language Abstractions

September 1st, 2007

It’s been nice working at a company where everyone is a computational polyglot – you can debate the merits of LISP or the shortcomings of Smalltalk over coffee. Of course there are downsides – simply to get the build system to work requires getting Perl, C, Bash and Python to work together harmoniously.

However, working in a semantically rich language, like Python, or talking to people who know Lisp, which is probably the most versatile language in existence, really does through into sharp relief how semantically impoverished C is. One problem is that people frequently don’t make use of language abstractions, even where they exist. But it’s very nice to be able to introduce them, and benefit from the resulting simplification of the code base.

I discovered the new python ‘with’ statement, which in the absence of deterministic destructors is a pretty nice way of handling resource management. Writing lock-safe code is now as simple as this:


with Guard(lock):
    threadUnsafeOperation()

where Guard is as simple as

class Guard:
    def __init__(self,lock):
        self.lock=lock
    def __enter__(self):
        self.lock.acquire()
    def __exit__(self,exceptionType,exceptionValue,traceback):
        self.lock.release()

I’ve really been missing the constructor/destructor pairs from C++ in other languages, and this seems to fit the bill pretty nicely, especially where the pattern you’re looking for is ‘ScopeGuard’

Posted in Programming, python | No Comments »

Actually getting somewhere.

June 7th, 2007

Having put in all the work for a nice infrastructure for my current project, I think that it’s beginning to pay off.  Now I can make changes to my TurboGears app in JEdit, have the changes picked up by CherryPy’s auto-reload feature and test and debug in a very tight cycle.  Then when I’m happy with them I can push them upstream with a quick subversion check in, and a few seconds later my changes are running on the production machine.  All in all, I feel as if programming could actually become pleasant again.  And I must say that TurboGears is a nice way to develop GUI’s – describing the layout in XHTML is far nicer than all the grief I used to have to go back in the day when I was building desktop apps with MFC.

Posted in Programming, TurboGears, python | No Comments »

Portfolio

May 30th, 2007

Languages I code in:

Continuously for the last 8 years

  • C++
  • SQL
  • Python

Regularly for several years:

  • Java
  • Haskell
  • Javascript
  • Visual Basic
  • FoxPro (yuck!)

Operating Systems I use regularly

  • Windows (Since NT3.1/ Windows 95)
  • Linux (including Mandrake, Ubuntu, Red Hat)
  • FreeBSD (isn’t ports nice?)
  • Solaris
  • Mac OSX (can’t run a studio without it!)
  • Other interesting combinations like Cygwin on Windows, Parallels on Mac, VirtualPC etc.
  • I typically work in heterogeneous environments with multiple different systems having to work together.

Database systems I’ve used

  • MS SQL Server
  • MySQL
  • Postgresql
  • Oracle
  • Access
  • FoxPro
  • sqlite

Source Control Systems I know

  • Bitkeeper
  • Perforce
  • Subversion
  • Microsoft Visual Source Safe (hardly counts, I know)
  • darcs

Types of development I do

  • Web based AJAX applications, using TurboGears or ModPython
  • Win32 applications, using MFC, WTL, or the Win32 api.
  • Low-level C++ libraries: eg. TCP/IP communications and medical imaging.
  • Cross platform GUIs using toolkits like GTK.
  • Anything to do with databases, especially fun legacy environments with multiple disparate data sources that all need glued together!
  • Medical Imaging – I’ve developed a powerful C++ and Python implementation of the DICOM standard that’s being used at a number of institutions, and I maintain the associated support website : http://dicomlib.swri.ca
  • Creating systems and workflow to efficiently deliver new services.
  • Analysing, simplifying and improving legacy systems.

People I work for

  • Major Banks
  • Oil companies
  • Non-profits
  • Research hospitals
  • Consulting firms
  • Friends
  • Software development companies
  • Event managers
  • Photographers
  • Anyone else…

What is my philosophy of systems development?

“Il semble que la perfection soit atteinte non quand il n’y a plus rien à ajouter, mais quand il n’y a plus rien à retrancher.”

(Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.)

Antoine de Saint-Exupéry

Posted in Programming, c++, python, work | No Comments »

TurboGears on WebFaction

May 30th, 2007

Created a new site for a client on WebFaction, using TurboGears. A few small bumps here and there, but mostly a smooth and fairly painless process. Went from not having a site to having a fully functional TurboGears site with postgres backend, subversion and trac implementation, in about a day. (Including homeschooling all morning and rigging a photoshoot in the evening!)

The site doesn’t do a whole lot yet, but I have a hunch that setting up the infrastructure will have been a significant part of the job. I think that WebFaction could be a very useful resource – they can get you up and running with a site complete with Rails/TurboGears/Django/Framework of your choice quite efficiently.

Posted in Programming, python, work | No Comments »

Turbogears

May 16th, 2007

These last few days I’ve been experimenting with TurboGears, the Python answer to Ruby on Rails. This has been one of those situations where you write something yourself for the learning experience, and then throw it away and use someone else’s tool. TurboGears comprise of a full stack of tools for writing interactive web applications, including:

  • database layer
  • templating language
  • javascript library
  • url-python mapping system.

Now in the past, I’ve probably implemented most of the above myself, or pulled together existing systems such as Cheetah, prototype, mod_python with my own rudimentary url-to-python function mapping system. But, as I said, having done it once to get my head around the concepts, it’s much nicer to then take someone’s off-the-shelf system and get on with the job. I like the TurboGears philosophy, which is about taking existing toolkits and making them play nicely together, rather than re-invent the wheel. Apparently it’s also easy to swap in other components, for example replacing the default templating language (kid) with another, such as Cheetah.

Hopefully it will become another useful instrument in my ever-growing toolbox.

Posted in python | No Comments »

DICOMLIB

May 16th, 2007

If, by a bizarre coincidence, you are looking for a top-quality, open source DICOM implementation in C++, along with bindings for Python, then you could do far worse than peruse the most excellent dicomlib website. I created this site last month, to support the library of the same name that I wrote during my time at Sunnybrook Health Sciences Centre. I’ve been learning that developing good software is more than just a technological challenge; it’s a social one as well. Pulling together contributors and users, figuring out what people actually want, managing timelines, synchronizing competing needs and simply getting the word out that the software exists are all important tasks. While I like nothing better than getting stuck into the nitty-gritty of debugging a complex multi-threading problem, it’s been a new experience for me to tackle the community-building side of software development.

Posted in DICOM, Programming, c++, python | 1 Comment »