Python awesomeness that sneaks up on you


I recently discovered two useful modules in Python’s library.

At work, a process needed to input and parse .csv files. Csv files are challenging because no specification exists, although there are de facto rules — which .csv-generating applications sometimes violate. I proceeded to write a specialized parser. It handled only our .csv files (which are used by a system configuration process), but still, its regular expressions were things of beauty. I felt pretty good about it when I was done.

And then…I discovered that Python comes with a csv module! Who knew? Amazing! I ripped out my custom code (including my beautiful Regex strings, sigh) and replaced it with code using csv. It works great, and now there’s fewer lines of code to support!

Another discovery was about temporary files. Some back-end tasks needed temporary files, and so I dutifully defined TEMPORARY_FILE objects and implemented the algorithms around them.

And then, my colleague, Ryan Roemer, mentioned the tempfile module. Sweetness! No more defining “task_foo.txt” filenames. Very nice!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.