[Tutor] time(duration) formats & basic time math query

Alan Gauld alan.gauld at freenet.co.uk
Thu Jul 6 13:07:58 CEST 2006


> So the input doc would be grepped for times and i could just  
> uncomment the line i need and get he format my boss wants at this  
> particular moment.

You shouldn't need to uncomment it if you can specify an 
argument that describes which format you want. Then have 
a bunch of formatting functions that have the same protocol

func(string) -> string

you can then assign your formatting function as an argument 
to the "grep" function:

def f(s): ....
def g(s): ....
def h(s): ...

if arg == 'hms':
    filter = h
id arg == 's'
    filter = g
etc

print grep(theFile, filter)


Or use a dictionary instead of the if/elif chain

filter = {'hms': h,
            's': g,
            etc...}

print grep(thefile, filter[arg])

Now to write the filter functions you still need to grok datetime 
but this might save any commenting out shenannigans... :-)

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list