Saturday, March 31, 2007

PyMOTW: Call for input

Tomorrow's post will cover the ConfigParser module. Beyond that, I have a few more weeks planned out, and am looking for suggestions for which modules to cover next.

If you were stranded on a desert island, which standard library module would you want, and why?

Updated 5/20/2007 with technorati tags.


Technorati Tags:
,


Testing pygments

This is a test post to see experiment with the code hightlighting output from pygments.org (as recommended by a couple of commentors on my previous post). Pygments produces HTML with CSS-based styling, so I have added a bunch of new styles to my blogger template. And I am including as a sample the same Python code posted earlier with the alternative syntax highlighting tool.

    def main(self, *m3ufilenames):

self.startRSS()
self.generateChannelInfo()

for line in fileinput.input(m3ufilenames):
mp3filename = line.strip()
if not mp3filename or mp3filename.startswith('#'):
continue
self.generateItem(mp3filename)

self.endRSS()

return 0


So, let me know what you think of the 2 methods, and which looks better.

Tuesday, March 27, 2007

Missing the point

This is a perfect example of being too close to a problem.

Some users of the FlipStart compact PC were having trouble pressing the tiny keys for Ctrl-Alt-Del to login or reboot Windows. Unfortunately, the hardware vendor decided that instead of fixing the operating system so it wasn't necessary to press Ctrl-Alt-Del on the tiny keyboard, they would just add a special Ctrl-Alt-Del button.

Go figure.

Sunday, March 25, 2007

PyMOTW: fileinput

To start this series, let's take a look at the fileinput module, a very useful module for creating command line programs for processing text files in a filter-ish manner. For example, the m3utorss app I recently wrote for my friend Patrick to convert some of his demo recordings into a podcastable format.

The inputs to the program are one or more m3u file listing the mp3 files to be distributed. The output is a single blob of XML that looks like an RSS feed (output is written to stdout, for simplicity). To process the input, I need to iterate over the list of filenames and:

  1. Open each file.
  2. Read each line of the file.
  3. Figure out if the line refers to an mp3 file.
  4. If it does, extract the information from the mp3 file needed for the RSS feed.
  5. Print the output.
I could have written all of that file handling out by hand. It isn't that complicated, and with some testing I'm sure I could even get the error handling right. But with the fileinput module, I don't need to worry about that. I just write something like:

def main(self, *m3ufilenames):
self.startRSS()
self.generateChannelInfo()

for line in fileinput.input(m3ufilenames):
mp3filename = line.strip()
if not mp3filename or mp3filename.startswith('#'):
continue
self.generateItem(mp3filename)

self.endRSS()
return 0


The relevant bit in that snippet is the for loop. The fileinput.input() function takes as argument a list of filenames to examine. If the list is empty, the module reads data from standard input. The function returns an iterator which returns individual lines from the text files being processed. So, all I have to do is loop over each line, skipping blanks and comments, to find the references to mp3 files.

In this example, I don't care what file or line number we are processing in the input. For other tools (grep-like searching, for example) you might. The fileinput module includes functions for accessing that information (filename(), filelineno(), lineno(), etc.). Check out the standard library documentation for fileinput for more details.

Updated 5/20/2007 with technorati tags.


Technorati Tags:
,


Converting Python source to HTML

For my PyMOTW series, I have found that I want to convert a lot of python source code to HTML. In a perfect world it would be easy for me to produce pretty XML/HTML and use CSS, but it is not obvious how to use CSS from Blogger. Instead, I am using a CLI app based on this ASPN recipe which produces HTML snippets that I can paste directly into a new blog post. The output HTML is more verbose than I wanted, but I like the fact that it has no external dependencies.

If you have any alternatives, I would appreciate hearing about them.

PyMOTW: Python Module of the Week

I am starting a new series of posts today that I am calling "Python Module of the Week" (PyMOTW). I have two goals for this:

  1. to work my way though the standard library and learn something about each module
  2. to get into the habit of posting to the blog more regularly
I will cheat a little, and start out with some modules that I already know something about. Hopefully that will give me enough of a head-start that I can keep up a fairly regular flow.

Subscribe to PyMOTW in your feed reader

Updated 5/20/2007 with technorati tags.


Technorati Tags:
,


Sunday, March 11, 2007

What is the matter with people?

Or, more specifically, New Mexico state representatives?

I don't know where to start. You can't legislate science? Pluto isn't ever "over" New Mexico?

Sunday, March 4, 2007

Distributing django applications

I had a report that version 1.2 of my codehosting package did not include all of the required files. It turns out I messed up the setup.py file and left out the templates, images, and CSS files. Oops.

In the process of trying to fix the setup file, I discovered that distutils does not include package data in sdist. Not a big deal, since I just created a MANIFEST.in file to get around it.

My next challenge (for this project) is how to write the templates in a way that would let anyone actually reuse them. For example, the project details page shows info about the most current release and a complete release history. It uses a 2 column layout for that, but the way I have it implemented the layout is defined in the base template for my site. I want to move that layout from the site base template down into the application base template, but I do not want to repeat myself if I can avoid it. Maybe I need to get over that and just repeat the layout instructions. Or refactor the site base template somehow. Obviously that needs more thought. I did find some useful advice in DosAndDontsForApplicationWriters, but have not implemented all of those suggestions.

In the mean time, release 1.4 of codehosting is more flexible than the previous releases and is probably closer to something useful for people other than me.

[Updated 28 Sept 2007 to correct typo in title]

Saturday, March 3, 2007

Things to Do

In no particular order:

  1. Cull my Google Reader subscriptions. 364 is too many.
  2. Finish reading Dreaming in Code.
  3. Add tagging support to codehosting.
  4. Verify all of the domains under my control with Google Web Master tools.
  5. Create a Trac plugin for code reviews based on the process we use at work.
  6. Change the monitor feeds on CastSampler.com so they do not include items without enclosures.
  7. Enhance BlogBackup to save enclosures and images linked from blog posts.
  8. Write a tool to convert an m3u file to an RSS/Atom feed for Patrick so he will set up a podcast of his demo recordings.
  9. Improve AppleScript support in Adium.
  10. Add support to Adium for notifications when a screen name appears in a chat message.