Controlling the passing of data

Sayth Renshaw flebber.crue at gmail.com
Fri Apr 29 09:17:44 EDT 2016


> because a set avoids duplicates. If you say "I want to document my 
> achievements for posterity" I would recommend that you print to a file 
> rather than append to a list and the original code could be changed to
> 
> with open("somefile") as f:
>     for achievement in my_achievements:
>         print(achievement.description, file=f)
> 
> 
> Back to my coding hint: Don't repeat yourself. If you move the pieces
> 
> >> > meetattrs = ('id', 'venue', 'date', 'rail', 'weather',
> >> > 'trackcondition')
> >> 
> >> >     meet = d('meeting')
> >> 
> >> >     meetdata = [[meet.eq(i).attr(x)
> >> >                  for x in meetattrs] for i in range(len(meet))]
> 
> into a function
> 
> def extract_attrs(nodes, attrs):
>     return [[nodes.eq(i).attr(name) for name in attrs]
>             for i in range(len(nodes))]
> 
> You can reuse it for clubs, races, etc.:
> 
> meetdata = extract_attrs(d("meeting"), meetattrs)
> racedata = extract_attrs(d("race"), raceattrs)
> 
> If you put the parts into a dict you can generalize even further:
> 
> tables = {
>    "meeting": ([], meetattrs),
>    "race": ([], raceattrs),
> }
> for name, (data, attrs) in tables.items():
>     data.extend(extract_attrs(d(name), attrs))
> 

I find that really cool. Reads well to, hadn't considered approaching it that way at all.

> So you want to go from a tree structure to a set of tables that preserves 
> the structure by adding foreign keys. You could try a slightly different 
> approach, something like
> 
> for meeting in meetings:
>     meeting_table.append(...meeting attrs...)
>     meeting_id = ...
>     for race in meeting:
>         race_table.append(meeting_id, ...meeting attrs...)
>         race_id = ...
>         for nomination in race:
>             nomination_table.append(race_id, ...nomination attrs...)
> 
> I don't know how to spell this in PyQuery -- with lxml you could do 
> something like
> 
> meeting_table = []
> race_table = []
> nomination_table = []
> tree = lxml.etree.parse(filename)
> for meeting in tree.xpath("/meeting"):
>     meeting_table.append([meeting.attrib[name] for name in meetattrs])
>     meeting_id = meeting.attrib["id"]
>     for race in meeting.xpath("./race"):
>         race_table.append(
>             [meeting_id] + [race.attrib[name] for name in raceattrs])
>         race_id = race.attrib["id"]
>         for nomination in race.xpath("./nomination"):
>             nomination_table.append(
>                 [race_id]
>                 + [nomination.attrib[name] for name in horseattrs])
> 
> Not as clean and not as general as I would hope -- basically I'm neglecting 
> my recommendation from above -- but if it works for you I might take a 
> second look later.

I need to play around with this just to understand it more, really like it. Might try and implement your advice from before and put it in a function.

Sayth



More information about the Python-list mailing list