Need help optimizing first script

Bengt Richter bokr at oz.net
Fri Jun 20 09:45:30 CEST 2003


On 19 Jun 2003 06:03:22 -0700, popup391 at yahoo.com (Frederic Lafleche) wrote:

>I put this script together mainly from bits and pieces I picked up in
>this newsgroup and one or two books lying around. What it does is map
>a given NT network drive and copy a file to it if some conditions are
>met.  As this is my first script, I'm looking for ways to improve code
>in any possible way, whether it's overall design, style, grammar,
>logic, readability, performance, validation, anything.
>
>#copyacs.py
>import os, shutil, sys, time, win32file
>from win32wnet import WNetAddConnection2, error
>
># Functions
>
># Generate timestamp
># OUTPUT: string in yyymmddhhmm format
>def determineFN():
    """Use time module's builtin formatting to return current time as yyyymmddhhmm"""
    return time.strftime('%Y%m%d%H%M')

No one seems to have mentioned strftime yet ;-)

>    # Get current time
>    now = time.time()
>    # Parse elements
>    (Iyear, Imonth, Iday, Ihour, Iminute) = (time.localtime(now)[:5])
>    year = str(Iyear)
>    month = str(Imonth)
>    day = str(Iday)
>    hour = str(Ihour)
>    minute = str(Iminute)
>      
>    # Padding
>    if len(month) == 1:
>        month = '0' + month
>    if len(day) == 1:
>        day = '0' + day
>    if len(hour) == 1:
>        hour = '0' + hour
>    if len(minute) == 1:
>        minute = '0' + minute
>
>    return year + month + day + hour + minute

There's other stuff to comment on, but I'm lazy right now ;-)
It's fun to (re)invent stuff, but for most common things, someone's
been there before by now, and it will often show up in the builtins
or a module, so if you are using  module, it pays to read the docs ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list