Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107855

Re: Controlling the passing of data

Newsgroups comp.lang.python
Date 2016-04-29 06:17 -0700
References (1 earlier) <nft3s8$3ku$1@ger.gmane.org> <mailman.189.1461850832.32212.python-list@python.org> <cf68e844-441c-4498-a730-0c7b08da1ee9@googlegroups.com> <nfvgcr$i4d$1@ger.gmane.org> <mailman.218.1461929203.32212.python-list@python.org>
Message-ID <03178a33-04cb-491e-8cfb-9e6545fa092d@googlegroups.com> (permalink)
Subject Re: Controlling the passing of data
From Sayth Renshaw <flebber.crue@gmail.com>

Show all headers | View raw


> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Controlling the passing of data Sayth Renshaw <flebber.crue@gmail.com> - 2016-04-28 05:02 -0700
  Re: Controlling the passing of data Peter Otten <__peter__@web.de> - 2016-04-28 15:40 +0200
    Re: Controlling the passing of data Sayth Renshaw <flebber.crue@gmail.com> - 2016-04-28 06:59 -0700
      RE: Controlling the passing of data Dan Strohl <D.Strohl@F5.com> - 2016-04-28 15:19 +0000
        Re: Controlling the passing of data Sayth Renshaw <flebber.crue@gmail.com> - 2016-04-28 18:49 -0700
      Re: Controlling the passing of data Peter Otten <__peter__@web.de> - 2016-04-29 13:26 +0200
        Re: Controlling the passing of data Sayth Renshaw <flebber.crue@gmail.com> - 2016-04-29 06:17 -0700
      Re: Controlling the passing of data Peter Otten <__peter__@web.de> - 2016-04-29 13:36 +0200
  RE: Controlling the passing of data Dan Strohl <D.Strohl@F5.com> - 2016-04-28 14:40 +0000
    Re: Controlling the passing of data Rustom Mody <rustompmody@gmail.com> - 2016-04-28 07:52 -0700

csiph-web