On Dec 10, 11:07 pm, Stargaming <[EMAIL PROTECTED]> wrote: > On Mon, 10 Dec 2007 19:27:43 -0800, George Sakkis wrote: > > On Dec 10, 2:11 pm, 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 > > > Alas, it's not: > > > AttributeError: 'module' object has no attribute 'dom' > > > Here's a working version, optimized for char length (one line, 241 > > chars): > > > import urllib as U,elementtree.ElementTree as > > E;c=[E.parse(U.urlopen('http://api.etsy.com/feeds/xml_user_details.php? > > id=%d'%u)).findtext('//city')for u in > > 71234,729,42346,77290,729,729];print'\n'.join('%s: %s'% > > (i,c.count(i))for i in set(c)) > > > George > > Heh, yes. I did the same error as the participant before me -- test it in > a premodified environment. A fix is easy, __import__ 'xml.dom.minidom' > instead of 'xml'. :-)
Closer, but still wrong; for some weird reason, __import__ for modules in packages returns the top level package by default; you have to use the 'fromlist' argument: >>> __import__('xml.dom.minidom') is __import__('xml') True >>> __import__('xml.dom.minidom', fromlist=True) <module 'xml.dom.minidom' from '/usr/local/lib/python2.5/xml/dom/ minidom.pyc'> George -- http://mail.python.org/mailman/listinfo/python-list