mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-22 01:54:09 +01:00
Cleaner implementation.
This commit is contained in:
parent
fc2751d04d
commit
e4f15ae1b3
44
src/utils.py
44
src/utils.py
@ -125,46 +125,36 @@ def timeElapsed(elapsed, short=False, leadingZeroes=False, years=True,
|
|||||||
etc. will be printed; the others determine what larger time periods should
|
etc. will be printed; the others determine what larger time periods should
|
||||||
be used.
|
be used.
|
||||||
"""
|
"""
|
||||||
|
ret = []
|
||||||
def format(s, i):
|
def format(s, i):
|
||||||
if short:
|
if short:
|
||||||
return '%s%s' % (i, s[0])
|
ret.append('%s%s' % (i, s[0]))
|
||||||
else:
|
else:
|
||||||
return nItems(s, i)
|
if leadingZeroes or ret or i:
|
||||||
|
ret.append(nItems(s, i))
|
||||||
elapsed = int(elapsed)
|
elapsed = int(elapsed)
|
||||||
assert years or weeks or days or \
|
assert years or weeks or days or \
|
||||||
hours or minutes or seconds, 'One flag must be True'
|
hours or minutes or seconds, 'One flag must be True'
|
||||||
ret = []
|
|
||||||
if years:
|
if years:
|
||||||
yrs, elapsed = elapsed // 31536000, elapsed % 31536000
|
(yrs, elapsed) = (elapsed // 31536000, elapsed % 31536000)
|
||||||
if leadingZeroes or yrs:
|
format('year', yrs)
|
||||||
if yrs:
|
|
||||||
leadingZeroes = True
|
|
||||||
ret.append(format('year', yrs))
|
|
||||||
if weeks:
|
if weeks:
|
||||||
wks, elapsed = elapsed // 604800, elapsed % 604800
|
(wks, elapsed) = (elapsed // 604800, elapsed % 604800)
|
||||||
if leadingZeroes or wks:
|
format('week', wks)
|
||||||
if wks:
|
|
||||||
leadingZeroes = True
|
|
||||||
ret.append(format('week', wks))
|
|
||||||
if days:
|
if days:
|
||||||
ds, elapsed = elapsed // 86400, elapsed % 86400
|
(ds, elapsed) = (elapsed // 86400, elapsed % 86400)
|
||||||
if leadingZeroes or ds:
|
format('day', ds)
|
||||||
if ds:
|
|
||||||
leadingZeroes = True
|
|
||||||
ret.append(format('day', ds))
|
|
||||||
if hours:
|
if hours:
|
||||||
hrs, elapsed = elapsed // 3600, elapsed % 3600
|
(hrs, elapsed) = (elapsed // 3600, elapsed % 3600)
|
||||||
if leadingZeroes or hrs:
|
format('hour', hrs)
|
||||||
if hrs:
|
|
||||||
leadingZeroes = True
|
|
||||||
ret.append(format('hour', hrs))
|
|
||||||
if minutes or seconds:
|
if minutes or seconds:
|
||||||
mins, secs = elapsed // 60, elapsed % 60
|
(mins, secs) = (elapsed // 60, elapsed % 60)
|
||||||
if leadingZeroes or mins:
|
if leadingZeroes or mins:
|
||||||
ret.append(format('minute', mins))
|
format('minute', mins)
|
||||||
if seconds:
|
if seconds:
|
||||||
ret.append(format('second', secs))
|
leadingZeroes = True
|
||||||
if len(ret) == 0:
|
format('second', secs)
|
||||||
|
if not ret:
|
||||||
raise ValueError, 'Time difference not great enough to be noted.'
|
raise ValueError, 'Time difference not great enough to be noted.'
|
||||||
if short:
|
if short:
|
||||||
return ' '.join(ret)
|
return ' '.join(ret)
|
||||||
|
@ -81,7 +81,8 @@ class UtilsTest(SupyTestCase):
|
|||||||
self.assertEqual('try', f('tries'))
|
self.assertEqual('try', f('tries'))
|
||||||
|
|
||||||
def testTimeElapsed(self):
|
def testTimeElapsed(self):
|
||||||
self.assertRaises(ValueError, utils.timeElapsed, 0, 0, seconds=False)
|
self.assertRaises(ValueError, utils.timeElapsed, 0,
|
||||||
|
leadingZeroes=False, seconds=False)
|
||||||
then = 0
|
then = 0
|
||||||
now = 0
|
now = 0
|
||||||
for now, expected in [(0, '0 seconds'),
|
for now, expected in [(0, '0 seconds'),
|
||||||
|
Loading…
Reference in New Issue
Block a user