PEP 303: Extend divmod() for Multiple Divisors

Andrew Dalke adalke at mindspring.com
Tue Dec 31 17:32:39 EST 2002


Martin v. Löwis wrote:
> The use case for this feature will yield values in some (irregular) 
> positioning system (e.g. days, hours, minutes, seconds). It is common to 
> list such values with the most significant digit (days) first, and it is 
> (IMO) intuitive if the bases are listed in the same order (i.e. 24, 60, 
> 60).


Looking at my local installation, I found three places
with chained divmod calls.

random.py
         a, x = divmod(a, 30268)
         a, y = divmod(a, 30306)
         a, z = divmod(a, 30322)

whrandom.py
             t, x = divmod(t, 256)
             t, y = divmod(t, 256)
             t, z = divmod(t, 256)


ZEO/simul.py
     mm, ss = divmod(secs, 60)
     hh, mm = divmod(mm, 60)


What surprised me is that mxDateTime does *not* use chained
divmods.  That is, the program which I expect would need this
feature the most, doesn't need it.

Instead, it has

     year = int((1.0 * absdate) / 365.2425)
       (plus some twiddling to get it right)
      ...
     inttime = int(abstime)
     hour = inttime / 3600
     minute = (inttime % 3600) / 60
     second = abstime - 1.0 * (hour*3600 + minute*60)

None of my code uses divmod -- I usually forgot about it
and go for the /% solution too.  ;)

					Andrew
					dalke at dalkescientific.com




More information about the Python-list mailing list