Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Dan Strohl Newsgroups: comp.lang.python Subject: RE: Controlling the passing of data Date: Thu, 28 Apr 2016 15:19:09 +0000 Lines: 142 Message-ID: References: <5c94882cb24c441bad13fd122851bd38@seaexchmbx03.olympus.F5Net.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de k6NJKy/ynHJefZnj5O0Xgw1nw/YCHTf3nxX8q4zWMJVw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'guido': 0.05; 'attributes': 0.07; 'desired.': 0.07; 'inserts': 0.07; 'meeting,': 0.07; 'meeting.': 0.07; 'csv': 0.09; 'english,': 0.09; 'indication': 0.09; 'inserted': 0.09; 'iterate': 0.09; 'output,': 0.09; 'parsed': 0.09; 'xml.': 0.09; 'python': 0.10; 'output': 0.13; 'thursday,': 0.13; 'intermediate': 0.15; 'message-----': 0.15; '1234': 0.16; '2016': 0.16; 'advice:': 0.16; 'attr': 0.16; 'capability.': 0.16; 'occurence': 0.16; 'optionally': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'repetitions': 0.16; 'skip:[ 50': 0.16; 'snippets': 0.16; 'thought.': 0.16; 'traverse': 0.16; 'variable.': 0.16; 'flexibility': 0.18; 'meetings': 0.18; 'python?': 0.18; 'to:name:python-list@python.org': 0.20; 'to:2**1': 0.21; 'controlling': 0.22; 'parse': 0.22; 'parsing': 0.22; 'simpler': 0.22; 'pass': 0.22; 'code,': 0.23; 'code.': 0.23; 'defined': 0.23; '(you': 0.23; 'insert': 0.23; 'passing': 0.23; 'tables': 0.23; 'second': 0.24; '(this': 0.24; 'xml': 0.24; 'header:In-Reply-To:1': 0.24; 'define': 0.27; 'least': 0.27; 'question': 0.27; 'pieces': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'actual': 0.28; 'correct,': 0.29; 'nominations': 0.29; 'race,': 0.29; 'received:192.168.10': 0.29; 'referencing': 0.29; 'relational': 0.29; 'asked': 0.29; 'classes': 0.30; 'url:mailman': 0.30; 'meeting': 0.30; 'creating': 0.30; 'code': 0.30; 'push': 0.30; 'another': 0.32; 'included': 0.32; 'table': 0.32; 'class': 0.33; 'problem': 0.33; 'source': 0.33; 'url:python': 0.33; 'skip:- 10': 0.34; 'url:listinfo': 0.34; 'changing': 0.34; 'structure': 0.34; 'running': 0.34; 'list': 0.34; 'sent:': 0.35; 'clear': 0.35; 'knowledge': 0.35; 'something': 0.35; 'subject:': 0.35; 'step': 0.36; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'created': 0.36; 'possible.': 0.36; 'email addr:python.org': 0.36; 'child': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'suggestion': 0.37; 'charset:us-ascii': 0.37; 'wanted': 0.37; 'seem': 0.37; 'wrong': 0.38; 'data': 0.39; 'subject:the': 0.39; "didn't": 0.39; 'from:': 0.39; 'received:192': 0.39; 'rather': 0.39; 'skip:x 10': 0.40; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'your': 0.60; 'determine': 0.61; 'provide': 0.61; 'identify': 0.61; 'back': 0.62; 'skip:n 10': 0.62; 'information': 0.63; 'python-list': 0.66; 'email name:python-list': 0.67; 'direct': 0.68; 'finally': 0.70; 'race': 0.72; 'lack': 0.76; '7:00': 0.84; 'received:192.168.15': 0.84; 'relate': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=f5.com; i=@f5.com; q=dns/txt; s=seattle; t=1461856752; x=1493392752; h=from:to:subject:date:message-id:references:in-reply-to: content-transfer-encoding:mime-version; bh=Zw+RaH4JQWmNZkQt0bptupgwjwXYDCeCsS61D275O9s=; b=KLnTI0lO6on9MlvJeI3A/WuoG4UdqvmRnlhIpDb7tsdXakE4uCEP/zmF VoOEUFhOvBeLodt595cYyDSTJJXumhhA91Z5QHd63PRsAD6ZLVjIF781I a4wMiKPP+qpZTQR+f52O/NT4Pr2g7v4wkEd9pKDdOT6yveLaEs3Lo0bv4 I=; X-IronPort-AV: E=Sophos;i="5.24,547,1454976000"; d="scan'208";a="215363177" Thread-Topic: Controlling the passing of data Thread-Index: AQHRoUZuWhIhx+A/60mF+gywqyncuZ+fZP9ugAAFc6GAABIHoA== In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [192.168.15.239] X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <5c94882cb24c441bad13fd122851bd38@seaexchmbx03.olympus.F5Net.com> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:107799 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): 1234 first 5678 second And you want something like: nominations =3D [(1,1234), (2,5678)] meetings =3D [(1,'first'),(2,'second')] if that is correct, my suggestion is to do something like (this is psudeo c= ode, I didn't look up the exact calls to use): nomination_list =3D [] meeting_list =3D [] for race_element in xml_file('race'): id =3D race_element.get_attr('id') for nomination_element in race_element('nomination'): nomination =3D nomination_element.get_text() nomination_list.append((id, nomination)) for meeting_element in race_element('meeting'): meeting =3D meeting_element.get_text() meeting_list.append((id, meeting)) > -----Original Message----- > From: Python-list [mailto:python-list-bounces+d.strohl=3Df5.com@python.or= g] > On Behalf Of Sayth Renshaw > Sent: Thursday, April 28, 2016 7:00 AM > To: python-list@python.org > Subject: Re: Controlling the passing of data >=20 >=20 > > > > 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 =3D ('id', 'venue', 'date', 'rail', 'weather', > > > 'trackcondition') > > > > > meet =3D d('meeting') > > > > > meetdata =3D [[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. >=20 > Hi Peter >=20 > > meetattrs =3D ('id', 'venue', 'date', 'rail', 'weather', > > 'trackcondition') >=20 > is created to define a list of attr in the XML rather than referencing ea= ch attr > individually I create a list and pass it into >=20 > > meetdata =3D [[meet.eq(i).attr(x) > > > for x in meetattrs] for i in range(len(meet))] >=20 > This list comprehension reads the XML attr by attr using meet =3D d('meet= ing') > as the call to pyquery to locate the class in the XML and identify the at= tr's. >=20 > I do apologise for the lack of output, I asked a question about parsing t= hat I > always seem to get wrong over think and then find the solution simpler th= an > I thought. >=20 > The output is 4 tables of the class and selected attributes eg meetattrs = =3D ('id', > 'venue', 'date', 'rail', 'weather', 'trackcondition') from the meeting cl= ass of the > XML. >=20 > In order to give flexibility and keep the relational nature they have def= ined in > the table I found when exporting the nominations section via pandas to cs= v > that i had no way to determine which id belonged to each race that is the= re > 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. >=20 >=20 > 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. >=20 > It was otherwise working as parsed output in code supplied using to push = to > pandas and use its csv write capability. >=20 > So I inserted >=20 > for race_el in d('race'): > race =3D pq(race_el) > race_id =3D race.attr('id') >=20 > for nom_el in race.items('nomination'): > res.append((pq(nom_el).attr('raceid', race_id))) >=20 > which traverses and inserts the race_id into the child nominations. Howev= er, > 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. >=20 >=20 > Thanks >=20 > Sayth > -- > https://mail.python.org/mailman/listinfo/python-list