iterating in reverse

Nick Vargish nav at adams.patriot.net
Sat Jan 18 05:47:55 CET 2003


Paul Rubin <phr-n2003b at NOSPAMnightsong.com> writes:

> How about something like (untested):
> 
>     def convert(nbytes):
>        suffixes = ['K', 'M', 'G', 'T', 'P', 'E']
>        s = ''
>        while nbytes >= 1024:
>           nbytes /= 1024.0
>           s = suffixes.pop(0)
> 
>        return '%.3f%c'% (nbytes, s)

It's supposed to look very spreadsheet-like, so I can't compress like
that.

It's a neat technique, though. I did work on a front-end for an mp3
repository, and I converted the sum of seconds to get the number of
days of continuous audio on the server. At the peak we had over 50
days of mus^H^H^Hsound on the beast. So, from your method:

def convertseconds(ticks):
    suffixes = [ ('seconds', 60.0), ('minutes', 60.0), ('hours', 24.0), \
                 ('days', 365.24), ('years', 1) ]
    for suf, quant in suffixes:
        if ticks / quant < 1.0:
            break
        ticks /= quant
    return '%.2f %s' % (ticks, suf)

Wow, that's cool. Much better than the way I did it last time. Test
results were plausible. (I could double check, but, well... :^)

Nick        

-- 
# sigmask.py  ||  version 0.2  ||  2003-01-07  ||  Feed this to your Python.
print reduce(lambda x,y:x+chr(ord(y)-1),'Ojdl!Wbshjti!=obwAqbusjpu/ofu?','')




More information about the Python-list mailing list