A skeleton template for new python projects
Saturday, March 06 2010
Whenever I start a new programming project, I find myself implementing the same initial functionality over and over: config files, command line options, some form of debug/logging output and so on. I hesitate to call this 'boilerplate' code, but I can't really think of a better word to describe it. Faced with this, I've come up with a template that I can build on that gives me some skeleton code to implement features I nearly always require. It's not very big, but saves time when starting a new project.
Most of my projects start with some wacky idea that just pops into my head (usually with the assistance of a higher than normal level of caffeine), then out through my fingers as quickly as possible to get something that kinda sorta works before I forget, or worse - get pulled off on some other task (my free time is very limited). Before starting the skeleton code, I would be half way through, suddenly realizing that it would be really useful to have somewhere to store configuration options right now, and waste time implementing that code instead of what I'm really doing. Using python with its large amount of built-in libraries reduces, but doesn't eliminate the problem.
I suspect a template of this sort is a somewhat personal thing, but if anybody would find such a thing useful, the git repository is git://git.mivok.net/pytemplate.git/ (browse).
These are the things I implemented/included in the template:
- Setting up of logging (used for debuggin and informational output) using the logging module. This does some basic setup with timestamps, outputting to stdout, and then I can just do logging.info("something") or logging.debug("debug only output") and things work out how I expect.
- Configuration file parsing using ConfigParser. There is code to search for several common config file locations (~/.prognamerc, /etc/prognamerc), and to load a default configuration from data/defaults.
- ConfigParser isn't used directly. There is a separate config.py module that adds default variants of the various get methods. For example, getintdefault, which will fetch an integer from the config file, falling back to a default value if the option doesn't exist rather than raising an exception.
- Command line option processing with some default options specifying whether to turn debugging output on and to specify an alternative config file location. New options can be quickly added, and the two options I add in every project are already in place.
- A project layout based on that mentioned at http://sayspy.blogspot.com/2010/03/various-ways-of-distributing-python.html and a predone setup.py script, allowing simple distribution of the app.
- A use_template.sh script that goes through and replaces 'progname' with the actual name of the project. Edit this script, fill in the values, and run it once. Then it can be deleted, with the template code already filled in.
This is being updated as I change how I do things. For instance the repository layout was changed very recently. If you find yourself starting multiple projects and implementing similar features each time, try building your own template. Even if it only contains a license file, skeleton readme file, and something to process command line arguments, it is work that you don't have to do each time.