Incessant Expressions

Writing for writing’s sake

Incessant Expressions header image 2

A follow up to snakes and pomegranates

June 23rd, 2007 by James Westfall

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.

Tags: 1 Comment

Leave A Comment

1 response so far ↓