Archive for programming

Review of ‘Learning Python’ by Mark Lutz

My review of Lutz’s Learning Python has been published over at The Associated Content. Between this blog, BAND NAMES NOT BRAND NAMES (new review posted today!), Helium, and Associated Content, I always forget to write something for each one! I wonder how people can manage and write for so many sites at a time unless they have a decent amount of back material already prepared.

A follow up to snakes and pomegranates

In my previous post, I was having some issues with a regex substitution in Python:


matchdate = re.compile(r'?P200[0-9]{5}’)
matchdate.sub(’CCYYMMDD’, ‘date’, count = 1)

However, I managed to get this working earlier in this week with a little refactoring (I also recoded the entire function it was enclosed in):


pattdate = re.compile(r'(?P200[0-9]{7})')
fi = file.read()
newdate = pattdate.sub(thedate, fi)

My problem was that while I was compiling the regex properly, I wasn’t matching it correctly. When run the program would produce something similar with my print function:


The old date was CCYYMMDDRR, the new date is date

And that was repeated 10 times for the first 10 bytes of the file. This was simply due to the fact that my `for` statement was incorrect so I was iterating over the first line of the file and processing stopped at the first \n. So I recoded the entire chdate() function to dump the `for` statement and just do an inline replacement a la sed. I also managed to get the output working for the the most part. I had declared `output = file(‘test.com.db’, ‘w’)` for my output file however, I wasn’t using output.write() correctly. I merely changed this to the following:


fi = file.read()
fo = open(sys.argv[1]+'.2', 'w')
print >>fo

Which, by reading, makes a lot more sense than pulling a global variable from somewhere and it functions the same albeit slightly faster from what I’ve seen. I can now do my inline date changes on zone files by merely typing `python chdate.py test.com.db 2007062301` and the script does the rest. Now I’ve got to figure out the most efficient way to walk a directory and gather certain criteria into a list or dictionary for further processing.

Snakes and pomegranates

This week, I’m nearly 300 pages deep in Mark Lutz’s Learning Python, Second Edition and it’s still an easy to read book surprisingly. I know that technical books that I’ve read can be easy to read, or even entertaining, for the first few chapters but once you get into the real “meat” of the subject matter, it tends to dry up. I’ve started chapter 15 today and it’s now getting into the more advanced and concrete everyday use cases you’ll see: modules, module packages. I’m currently using it to rewrite a script I use at work for mass SPF record insertion in DNS zone files. I’ve skipped ahead some in the book to get an understanding of the re (regular expression) module of Python but unfortunately, Lutz doesn’t really spend a whole lot of time on it in this book or Programming Python (his other acclaimed O’Reilly book on Python). It’s really a shame because the module’s syntax is rather complex and can really take some getting used to if all you’re used to using are PCRE-types (Perl Compatible Regular Expressions) and sed. But it’s extremely nice being able to create groups like the following for code re-use:


matchdate = re.compile(r'?P200[0-9]{5}')
matchdate.sub('CCYYMMDD', 'date', count = 1)

Now I can use `date` to match anything from 20010101 to 20091231 which can obviously be very helpful if I’m busy doing inline edits on multiple files that can have multiple dates in them in that syntax. However, my only problem now is the fact that I can read zone files into a list (an array in other languages) and change the date inline but I can’t seem to take those values and write them back into a file. In bash, it’s easy:


sed 's/200[0-9][0-9][0-9][0-9][0-9]/CCYYMMDD/' < file1 > file2

But when I try this with production code, I either end up with an empty file2 or I get an exception raised. Do I need to pickle the files?

Also, I’ve really grown to love pomegranate juice. I buy a bottle of POM(c) about once every two weeks since I use it as a supplement in my orange juice in the morning. I also have this great pomegranate tea that also has hibiscus and pepper in it. Yum!