Python parser script layout
I'm writing a simple Python parser, where I loop over each line in a file, and prosess it further if the right conditions are me开发者_高级运维t. My short start:
def identify(hh_line):
if(re.match(regex.new_round, hh_line)):
m = re.match(regex.new_round, hh_line)
# insert into psql
...
if(re.match...
..and I was wondering what's the best way (practice) to approach this task, since this is the first time I write Python.
Thanks! =)
First of all, it's redundant to run the match twice - instead, run it, store the result, and branch off of that:
m = re.match(regex.new_round, hh_line)
if m:
# ...
Next, if you have a bunch of regex -> processing combinations, you might instead make a dict of regex -> function mappings, and then just iterate over it:
def process_a(data):
# ...
def process_b(data):
# ...
regex_to_process = {
'regex_a': process_a,
'regex_b': process_b,
}
for hh_line in <file object>:
for regex,process in regex_to_process.iteritems():
m = re.match(regex, hh_line)
if m:
process(hh_line)
精彩评论