Re-thinking my if-thens - a software engineering question

metaperl metaperl at gmail.com
Wed Jan 24 14:51:27 EST 2007


Ok, I have a module called textgen.py. The point of this module is to
generate a csv file from an array of dictionaries. As I iterate through
each dictionary, I "massage" the dictionary values before writing them
out to csv. Now, for one dictionary entry, I have the following code:

            if dict_key == 'PCN':
		fields = dict_val.split("/")
                mo = re.match( '(\w{2})(\d{2})(\d{2})' , fields[1] )
		if mo:
		    dict_val = "%s/%s%s/%s" % (fields[0], mo.group(1), mo.group(3),
fields[2][1:])
		else:
		    dict_val = dict_val

Ok, so now here is what has happened. This code was based on the
assumption that dict_val would have 2 forward slashes in it. It turns
out that I need to follow a different process of massaging when no
slashes are present. A naive solution would be something like:

if dict_key == 'PCN':
                    fields = dict_val.split("/")
                    if fields == 3:
                        dict_val = pcn_three(fields) # where pcn_three
is the code above
                else:
                    # new logic

But I am wondering if I should abstract the flow of control into a
class or something.

Ideas welcome.




More information about the Python-list mailing list