[Tutor] Help with modulus.

Alan Gauld alan.gauld at btinternet.com
Tue Nov 27 11:28:45 CET 2007


"Luis N" <globophobe at gmail.com> wrote

> I'd like the below to be a single line if possible.

Beware compression for the sake of it. However we can simplify
a bit using divmod()

> hours = metrics.totaltime/3600000
> minutes = (metrics.totaltime - 3600000*hours)/60000
> seconds = (metrics.totaltime - 3600000*hours - 60000*minutes)/1000

hours,minutes = divmod(metrics.totaltime/1000, 3600) # get rid of the 
ms here
minutes.seconds = divmod(minutes, 60)

> Would it be possible to simplify this with a generator expression 
> e.g.
>
> total_time = tuple((...))

Possibly, but the above is simple enough for my tastes! :-)

Alan G. 




More information about the Tutor mailing list