Putting Things Together

A third-party vendor I (Rick Hightower) worked with once had a problem reading the IDL files we were trying to integrate with its product. Specifically it didn't like identifiers beginning with ::, as in ::com::mycompany::Employee, preferring com::mycompany::Employee. My job was to convert the IDL files to their liking, which I did using regular expressions. The code I wrote to do this is just four lines long (actually six but four with a little cheating).

Here's the code to read in the file and write all the replacement text:

f=open(filename)
fstr=re.compile(r'\s((::)(\w+))\b').sub(r' \3', f.read())
f.close(); f=open(filename, "w")
f.write(fstr); f.close()

Here's the complete fixIDL() function:

 import re def fixIDL(filename): ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.