This can be used as a great guide on writing pythonic code. Don't look at
the specific code that is being corrected but look at how the improvements
are being presented.

I would recommend someone who is learning python read this guide.

On Sun, Feb 15, 2009 at 12:17 AM, MRAB <goo...@mrabarnett.plus.com> wrote:

> aiwarrior wrote:
>
>> I've made this script and would like to have some input and share it
>> with the community.
>> I also have a page with some code i produce on my spare time.
>> http://pneves.net
>> Thanks
>>
>>  [snip]
>
>>
>> def from_rapidshare(url):
>>    '''Check if this is a rapidshare link'''
>>    return (url.startswith("rapidshare.com") or
>>        url.startswith("www.rapidshare.com") or
>>        url.startswith("http://rapidshare.com";) or
>>        url.startswith("http://www.rapidshare.com";))
>>
>>  You can shorten:
>
>    s.startswith(x) or s.startswith(y) or ...
>
> to:
>
>    s.startswith((x, y, ...))
>
> [snip]
>
>>    cfg = ConfigParser.SafeConfigParser()
>>    cfg.read(path)
>>    try:
>>        m_user = cfg.get("Acc_Details", "m_user")
>>        m_password = cfg.get("Acc_Details", "m_password")
>>        r_user = cfg.get("Acc_Details", "r_user")
>>        r_password = cfg.get("Acc_Details", "r_password")
>>    except ConfigParser.NoSectionError or ConfigParser.NoOptionError:
>>
>
> In order to catch multiple exceptions it should be:
>
>    except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
>
>         print "no section or No Option"
>>        print
>>        os.remove(path)
>>    return (m_user, m_password, r_user, r_password)
>>
>> def cookie_processor(cookie):
>>    cookie = cookie.split("; ")
>> ##    cookie = dict( [cookie[x].split("=") for x in xrange ( len
>> (cookie) ) ] )
>>
> Or:
> ##    cookie = dict(x.split("=") for x in cookie)
>
>  ##    if cookie['user'] != None:
>>
> When checking for None use "is" and "is not":
> ##    if cookie['user'] is not None:
>
>     return cookie
>> ##    else:
>> ##        print "Scheme has changed or authentication failes. Last
>> option most likely"
>> ##        sys.exit()
>>
> Raising an exception is better than exiting.
>
>  ##
>>
>>  [snip]
>
>> def mu_auth(login):
>>    r_user = login[0]
>>    r_password = login[1]
>>    cred = urllib.urlencode({"login": r_user, "password": r_password})
>>    try:
>>        req = urllib2.urlopen("http://www.megaupload.com";, cred)
>>        cookie_mu = cookie_processor( req.headers.get("set-cookie",
>> "") )
>>
>>    except:
>>
> Don't use an empty "except"; be explicit in which exception you want to
> catch .
>
> [snip]
>
>>
>> if sys.argv[1] == "-h" or sys.argv[1] == "–help":
>>
> Or:
>
>    if sys.argv[1] in ("-h", "–help"):
>
> [snip]
>
>  elif from_rapidshare(sys.argv[1]): #if argument is one or more urls
>>    for i in range(1, len(sys.argv)):
>>
> Or:
>
>    for arg in sys.argv[1 : ]:
>
> and so on.
>
>         if from_rapidshare(sys.argv[i]):
>>            urls.append(sys.argv[i])
>> else:
>>        print "This is not valid argument" , sys.argv[1]
>>        sys.exit()
>> urls = []
>>
>>  [snip]
> HTH
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.goldwatches.com/

http://www.jewelerslounge.com/
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to