Reverse argument order

Steve Holden sholden at holdenweb.com
Fri Oct 19 16:36:33 EDT 2001


"David Brady" <daves_spam_dodging_account at yahoo.com> wrote in message
news:mailman.1003519340.3380.python-list at python.org...
> 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?
>
This seem a little less sucky?

>>> def hms(*args):
...      args = list(args)
...      while len(args) < 3:
...           args.insert(0, 0)
...      print "args are:", args
...
>>>
>>> hms(0)
args are: [0, 0, 0]
>>> hms(12)
args are: [0, 0, 12]
>>> hms(12, 11)
args are: [0, 12, 11]
>>> hms(1,2,3)
args are: [1, 2, 3]
>>>

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list