Hi Stefan,<br><br>Thanks for the code review :) Only just noticed this.<br><br><div class="gmail_quote">On Wed, May 25, 2011 at 3:10 PM, Stefan Behnel <span dir="ltr"><<a href="mailto:stefan_ml@behnel.de">stefan_ml@behnel.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Sithembewena Lloyd Dube, <a href="tel:25.05.2011%2014" value="+12505201114" target="_blank">25.05.2011 14</a>:40:<div class="im">
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks for all your suggestions. I read up on gzip and urllib and also<br>
learned in the process that I could use urllib2 as its the latest form of<br>
that library.<br>
<br>
Herewith my solution: I don't know how elegant it is, but it works just<br>
fine.<br>
<br>
def get_contests():<br>
url = '<br>
<a href="http://xml.matchbook.com/xmlfeed/feed?sport-id=&vendor=TEST&sport-name=&short-name=Po" target="_blank">http://xml.matchbook.com/xmlfeed/feed?sport-id=&vendor=TEST&sport-name=&short-name=Po</a><br>
'<br>
req = urllib2.Request(url)<br>
req.add_header('accept-encoding','gzip/deflate')<br>
opener = urllib2.build_opener()<br>
response = opener.open(req)<br>
</blockquote>
<br></div>
This is ok.<div class="im"><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
compressed_data = response.read()<br>
compressed_stream = StringIO.StringIO(compressed_data)<br>
gzipper = gzip.GzipFile(fileobj=compressed_stream)<br>
data = gzipper.read()<br>
</blockquote>
<br></div>
This should be simplifiable to<br>
<br>
uncompressed_stream = gzip.GzipFile(fileobj=response)<div class="im"><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
current_path = os.path.realpath(MEDIA_ROOT + '/xml-files/d.xml')<br>
data_file = open(current_path, 'w')<br>
data_file.write(data)<br>
data_file.close()<br>
xml_data = ET.parse(open(current_path, 'r'))<br>
</blockquote>
<br></div>
And this subsequently becomes<br>
<br>
xml_data = ET.parse(uncompressed_stream)<div class="im"><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
contest_list = []<br>
for contest_parent_node in xml_data.getiterator('contest'):<br>
</blockquote>
<br></div>
Take a look at ET.iterparse().<div class="im"><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
contest = Contest()<br>
for contest_child_node in contest_parent_node:<br>
if (contest_child_node.tag == "name" and<br>
contest_child_node.text is not None and contest_child_node.text != ""):<br>
<a href="http://contest.name" target="_blank">contest.name</a> = contest_child_node.text<br>
if (contest_child_node.tag == "league" and<br>
contest_child_node.text is not None and contest_child_node.text != ""):<br>
contest.league = contest_child_node.text<br>
if (contest_child_node.tag == "acro" and<br>
contest_child_node.text is not None and contest_child_node.text != ""):<br>
contest.acro = contest_child_node.text<br>
if (contest_child_node.tag == "time" and<br>
contest_child_node.text is not None and contest_child_node.text != ""):<br>
contest.time = contest_child_node.text<br>
if (contest_child_node.tag == "home" and<br>
contest_child_node.text is not None and contest_child_node.text != ""):<br>
contest.home = contest_child_node.text<br>
if (contest_child_node.tag == "away" and<br>
contest_child_node.text is not None and contest_child_node.text != ""):<br>
contest.away = contest_child_node.text<br>
</blockquote>
<br></div>
This is screaming for a simplification, such as<br>
<br>
for child in contest_parent_node:<br>
if child.tag in ('name', 'league', ...): # etc.<br>
if child.text:<br>
setattr(context, child.tag, child.text)<div><div></div><div class="h5"><br>
<br>
<br>
Stefan<br>
<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Regards,<br>Sithembewena Lloyd Dube<br>