Reverse argument order

David Brady daves_spam_dodging_account at yahoo.com
Fri Oct 19 15:21:58 EDT 2001


Hello,

I am writing an app that will (hopefully) expose
Python as a scripting/macro language to users, some of
whom know little or nothing about programming.  A
previous version of our software had a script parser
that would handle times like so:

until 5        ; wait 5 seconds
until 1:05     ; wait 65 seconds
until 1:02:05  ; wait 1 hour, 2 minutes, 5 seconds

etc.

The thing to note is that it's sort of a positional
notation that is backwards from most programming
languages.  In Python, we're going to force the users
to put the hours, minutes and seconds as separate
arguments, but we'd like to allow them to omit the
higher-order arguments and have them default to zero.

Is it possible to do something like this in Python, to
reverse the argument order?   I came up with the
following, but I wonder if I'm going about it the hard
way.

def SetTime( *args ):
    h = 0
    m = 0
    s = 0
    if len(args) == 3:
        h = args[0]
        m = args[1]
        s = args[2]
    elif len(args) == 2:
        m = args[0]
        s = args[1]
    else:
        s = args[0]

    # set the time


As I write this, another option presents itself, which
isn't *quite* so bad:

def _SetTimeReverse( s, m=0, h=0 ):
    # set the time

def SetTime( *args ):
	b = list(args)
	b.reverse()
	_SetTimeReverse(*b)

...but it's still kind of hacky.

Is there a better way?

Thank you,

-dB
--
David Brady
daves_spam_dodging_account at yahoo.com
I'm feeling very surreal today... or *AM* I?

__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com




More information about the Python-list mailing list