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.