Programming Idiomatic Code

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Mon Jul 2 21:27:50 EDT 2007


On Jul 2, 5:22 pm, "Nathan Harmston" <ratchetg... at googlemail.com>
wrote:
> Hi,
>
> I m sorry but I m bored at work (and no ones looking so I can write
> some Python) and following a job advertisement post,I decided to write
> the code to do its for the one entitled Ninjas or something like that.
> I was wondering what could be done to my following code to make it
> more idiomatic...or whether it was idiomatic and to be honest what
> idiomatic really means. All comments greatly appreciated and welcomed.
>
> Thanks in advance
>
> Nathan
>
> import urllib2,sys
> from elementtree.ElementTree import parse
>
> base_url = "http://api.etsy.com/feeds/xml_user_details.php?id="
>
> def read_id_file(filename):
>     """ reads a file and generates a list of ids from it"""
>     ids = [ ]
>     try:
>         id_file = open(filename, "r")
>         for l in id_file:
>             ids.append( l.strip("\n") )
>         id_file.close()
>     except e:
>         print e
>         os._exit(99)
>     return ids
>




The expression in the except clause should
evaluate to an object that "matches" the exception.

For example,

import sys

try:
    idfile = open(filename, "r")
except IOError, e
    print >> sys.stderr, str(e)
    # or look into the warnings module
    # etc.


--
Hope this helps,
Steven




More information about the Python-list mailing list