[Tutor] better way to write this code

Peter Otten __peter__ at web.de
Fri Aug 24 14:11:20 CEST 2012


Norman Khine wrote:

> I have this code  (http://pastie.org/4575790) which pulls data from a list
> and then modifies some of the values such as the 'yield' entry, which has
> entries like:
> 
> 21
> 15
>  ≤ 1000
>  ≤ 20
> 2.2 - 30
> 
> so that they are cleaned up.

> can the code be improved further?

It will make the code a bit longer, but I'd still put the code for every 
column/key format into a separate function. Here's a sketch:

class Skip(Exception):
    pass

def as_is(item):
    return item

def fix_date(item):
    soup = BeautifulSoup(item)
    for a in soup.findAll('a'):
        return ''.join(a.findAll(text=True))
    raise Skip

def fix_fill(item):
    if 'Underground' in item:
        return "green"
    elif 'Atmospheric' in item:
        return 'red'
    raise Skip

fixers = [
    (0, "id", as_is),
    (2, "country", as_is),
    (3, "fill", fix_fill),
    (4, "date", fix_date),
    # ...
    (12, "name", as_is),
]

if __name__ == "__main__":
    INFILE = "table_content.json"
    OUTFILE = "detonations.json"

    with open(INFILE) as f:
        table_content = json.load(f)

    event_list = []
    for event in table_content:
        event_dict = {}
        for index, name, fixer in fixers:
            item = event[index]
            try:
                event_dict[name] = fixer(item)
            except Skip:
                pass
        event_list.append(event_dict)

    event_list.sort(key=operator.itemgetter('id'))

    with open(OUTFILE, 'w') as f:
        json.dump(event_list, f)

> also, the content has 2,153 items, what will be the correct way to have
> this in a separate file and import this within this file to work on it?

I think it would be better to store it in a data format. JSON seems the 
obvious choice.



More information about the Tutor mailing list