On 10 Dec, 19:11, Stargaming <[EMAIL PROTECTED]> wrote: > On Mon, 10 Dec 2007 16:10:16 +0200, Nikos Vergas wrote: > > [snip] > > >> Problem: In the dynamic language of your choice, write a short program > >> that will: > >> 1. define a list of the following user ids 42346, 77290, 729 (you can > >> hardcode these, but it should > >> still work with more or less ids) > >> 2. retrieve an xml document related to each user at this url "http:// > >> api.etsy.com/feeds/xml_user_details.php?id=" > >> 3. retrieve the data contained in the city element from each xml > >> document > >> 4. keep a running total of how many users are found in each city 5. > >> display the total count of users living in each city > [snip] > > > i wanted to make it a one liner, but i had to import modules :( > > > import sys, xml, urllib > > > dummy = [sys.stdout.write(city + ': ' + str(num) + '\n') for city, num > > in set([[(a, o.count(a)) for a in p] for o, p in [2*tuple([[city for > > city in > > ((xml.dom.minidom.parseString(urllib.urlopen('http://api.etsy.com/feeds/ > > xml_user_details.php?id=' > > > + str(id)).read()).getElementsByTagName('city')[0].childNodes + [(lambda > > t: (setattr(t, 'data', 'no city'), > > t))(xml.dom.minidom.Text())[1]])[0].data.lower().replace(' ', ' ') for > > id in [71234, 71234, 71234, 71234, 71234, 71234, 42792])]])]][0])] > > I suggest `__import__` in such cases. > > Even though I do not qualify for the job, I came up with this (<wink>) > code (modified list values for demonstration, mixed together from > previous post and original task): > > print '\n'.join('%s: %d'%(x,len(list(y))) for x,y in __import__ > ('itertools').groupby(sorted(__import__('xml').dom.minidom.parse > (__import__('urllib').urlopen('http://api.etsy.com/feeds/ > xml_user_details.php?id=%d'%i)).getElementsByTagName('city') > [0].lastChild.data.title() for i in (71234, 729, 42346, 77290, 729, > 729)))) > > I still find this rather readable, though, and there is no bad side- > effect magic! :-) > > Output should be: > > | Chicago: 3 > | Fort Lauderdale: 1 > | Jersey City And South Florida: 1 > | New York: 1 > > Cheers,
A simpleton's version: #!/usr/local/bin/python import urllib from elementtree import ElementTree as et userids = [71234,729,42346,77290,729,729] url = 'http://api.etsy.com/feeds/xml_user_details.php?id=' if __name__ == "__main__": city = {} for userid in userids: feed = urllib.urlopen(url+str(userid)) tree = et.parse(feed) for elem in tree.getiterator('city'): if not city.has_key(elem.text):city[elem.text] = 1 else: city[elem.text] += 1 for k,v in city.items(): if not k == None:print k,':\t',v Output: Fort Lauderdale : 1 new york : 1 Jersey City and South Florida : 1 Chicago : 3 Cheers -- http://mail.python.org/mailman/listinfo/python-list