Controlling the passing of data

Dan Strohl D.Strohl at F5.com
Thu Apr 28 11:19:09 EDT 2016


If I am reading this correctly... you have something like (you will have to excuse my lack of knowledge about what kinds of information these actually are):

<race id=1>
    <nomination>1234</nomination>
    <meeting>first</meeting>
</race>
<race id=2>
    <nomination>5678</nomination>
    <meeting>second</meeting>
</race>


And you want something like:
    nominations = [(1,1234), (2,5678)]
    meetings = [(1,'first'),(2,'second')]

if that is correct, my suggestion is to do something like (this is psudeo code, I didn't look up the exact calls to use):

nomination_list = []
meeting_list = []

for race_element in xml_file('race'):
    id = race_element.get_attr('id')
    for nomination_element in race_element('nomination'):
    	nomination = nomination_element.get_text()
               nomination_list.append((id, nomination))

    for meeting_element in race_element('meeting'):
    	meeting = meeting_element.get_text()
               meeting_list.append((id, meeting))




> -----Original Message-----
> From: Python-list [mailto:python-list-bounces+d.strohl=f5.com at python.org]
> On Behalf Of Sayth Renshaw
> Sent: Thursday, April 28, 2016 7:00 AM
> To: python-list at python.org
> Subject: Re: Controlling the passing of data
> 
> 
> >
> > Your actual problem is drowned in too much source code. Can you
> > restate it in English, optionally with a few small snippets of Python?
> >
> > It is not even clear what the code you provide should accomplish once
> > it's running as desired.
> >
> > To give at least one code-related advice: You have a few repetitions
> > of the following structure
> >
> > > 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))]
> >
> > You should move the pieces into a function that works for meetings,
> > clubs, races, and so on. Finally (If I am repeating myself so be it):
> > the occurence of range(len(something)) in your code is a strong
> > indication that you are not using Python the way Guido intended it.
> > Iterate over the `something` directly whenever possible.
> 
> Hi Peter
> 
> > meetattrs = ('id', 'venue', 'date', 'rail', 'weather',
> > 'trackcondition')
> 
> is created to define a list of attr in the XML rather than referencing each attr
> individually I create a list and pass it into
> 
>  >     meetdata = [[meet.eq(i).attr(x)
> > >                  for x in meetattrs] for i in range(len(meet))]
> 
> This list comprehension reads the XML attr by attr using meet = d('meeting')
> as the call to pyquery to locate the class in the XML and identify the attr's.
> 
> I do apologise for the lack of output, I asked a question about parsing that I
> always seem to get wrong over think and then find the solution simpler than
> I thought.
> 
> The output is 4 tables of the class and selected attributes eg meetattrs = ('id',
> 'venue', 'date', 'rail', 'weather', 'trackcondition') from the meeting class of the
> XML.
> 
> In order to give flexibility and keep the relational nature they have defined in
> the table I found when exporting the nominations section via pandas to csv
> that i had no way to determine which id belonged to each race that is there
> was no race_id in the nominations to relate back to the race, and also no
> meeting id in the raceid to relate it back to the meeting.
> 
> 
> So I wanted to traverse all the classes Meeting, Race and Nomination and
> insert the id of the class into its direct children only and since there were
> many races a meeting and many nomnations a race I need to ensure that it is
> the direct children only.
> 
> It was otherwise working as parsed output in code supplied using to push to
> pandas and use its csv write capability.
> 
> So I inserted
> 
>     for race_el in d('race'):
>         race = pq(race_el)
>         race_id = race.attr('id')
> 
>     for nom_el in race.items('nomination'):
>         res.append((pq(nom_el).attr('raceid', race_id)))
> 
> which traverses and inserts the race_id into the child nominations. However,
> my boggles is how to pass this to the list comprehension that was working
> without changing the data from XML or creating another intermediate step
> and variable. Just to parse it as it was but with the extra included race_id.
> 
> 
> Thanks
> 
> Sayth
> --
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list