I finished my first semi-useful program today. I probably wrote 800 lines of code, during the run, debug, repeat process just trying to unwrap my mind from the Perl Way and get used to Python’s Way.
The final thrust today was figuring out how to use regular expressions to pluck things from a configuration file. It’s something I can do in Perl in my sleep, with one hand on a cup of coffee. For example, this:
while (|STDIN|) {
if (/^([SV])_(\w+)=(\w+)/) {
($blah, $di, $blahblah) = ($1,$2,$3);
}
}
does what I want, took a minute to write, but, arguably, looks I let the coffee spill on the keyboard. (43things won’t let me use the lessthan/greater than signs, hence the pipes) The python n00b equivalent took a while to work out the invocation of the re functions.
I miss curly braces as visual delimiters where a function ends, especially as I’ve been burned by the incorrect indentation, but being encouraged to write smallish functions like this:
foo = re.compile('^([SV])_(\w+)=(\w+)',re.VERBOSE)
while 1:
line = f.readline()
if not line:
break
match = foo.search(line)
if match:
blah, di, blahblah = match.groups()
def get_config_file(x):
for i in a_list_i_made_up:
if os.environ.has_key(i):
if exists(join(os.environ[i], x)):
return join(os.environ[i], x)
exit
seems like a long-term win.
