In [ ]:
def say_secs(seconds):
names = 'sec min hour day week'.split()
modulos = [60, 60, 24, 7]
units, remainder = [], seconds
for mod in modulos:
remainder, unit = divmod(remainder, mod)
units.append(unit)
units.append(remainder)
plurals = [('' if count == 1 else 's') for count in units]
out = ' '.join(f'{count} {name}{p}'
for count, name, p
in (zip(units[::-1], names[::-1], plurals[::-1]))
if count)
return out or '0 secs'
if __name__ == '__main__':
for s in [0, 100, 66400, 172801, 987987]:
print(f'{s:6} seconds is: {say_secs(s)}')
In [ ]:
def say_secs(seconds):
names = 'sec min hour day week'.split()
plurals = [f'{n}s' for n in names]
modulos = [60, 60, 24, 7] # next unit is x times this for unit in names
return say_units(seconds, names, plurals, modulos)
def say_inches(inches, progression=False):
names = 'inch foot yard chain furlong mile'.split()
plurals = 'inches feet yards chains furlongs miles'.split()
modulos = [12, 3, 22, 10, 8]
return say_units(inches, names, plurals, modulos)
def say_units(amount, names, plurals, modulos):
units, remainder = [], amount
for mod in modulos:
remainder, unit = divmod(remainder, mod)
units.append(unit)
units.append(remainder)
named = [(singular if count == 1 else plural) for
count, singular, plural in zip(units, names, plurals)]
out = ' '.join(f'{count} {name}'
for count, name
in (zip(units[::-1], named[::-1]))
if count)
return out or f'0 {plurals[0]}'
if __name__ == '__main__':
print('\n###')
for s in [0, 100, 66400, 172801, 987987]:
print(f'{s:6} seconds is: {say_secs(s)}')
print('\n###')
say_inches(0, progression=True)
for i in [0, 100, 66400, 172801, 987987]:
print(f'{i:6} inches are: {say_inches(i)}')
In [3]:
class Say_unit():
def __init__(self, names, plurals, modulos):
self.names = names
self.plurals = plurals
self.modulos = modulos
def __call__(self, amount):
return self.say_units(amount)
def say_units(self, amount):
names, plurals, modulos = self.names, self.plurals, self.modulos
units, remainder = [], amount
for mod in modulos:
remainder, unit = divmod(remainder, mod)
units.append(unit)
units.append(remainder)
named = [(singular if count == 1 else plural) for
count, singular, plural in zip(units, names, plurals)]
out = ' '.join(f'{count} {name}'
for count, name
in (zip(units[::-1], named[::-1]))
if count)
return out or f'0 {plurals[0]}'
def print_progression(self):
for i, mod in enumerate(self.modulos):
print(' '* i + f'{mod} {self.plurals[i]} in 1 {self.names[i+1]}')
print()
def sample_unit_conversion(title, names, plurals, modulos):
print(f'\n### {title}')
unit_convertor = Say_unit(names, plurals, modulos)
unit_convertor.print_progression()
for units in [0, 100, 66400, 172801, 987987]:
print(f'{units:6} {plurals[0]} is: {unit_convertor(units)}')
if __name__ == '__main__':
title = 'Seconds of time'
names = 'sec min hour day week'.split()
plurals = [f'{n}s' for n in names]
modulos = [60, 60, 24, 7]
sample_unit_conversion(title, names, plurals, modulos)
title = 'Inches of length'
names = 'inch foot yard chain furlong mile'.split()
plurals = 'inches feet yards chains furlongs miles'.split()
modulos = [12, 3, 22, 10, 8]
sample_unit_conversion(title, names, plurals, modulos)
title = 'Fluid ounces of volume'
names = 'fluid ounce, gill, cup, pint, quart, gallon, peck'.split(', ')
plurals = [f'{n}s' for n in names]
modulos = [5, 2, 2, 2, 4, 2]
sample_unit_conversion(title, names, plurals, modulos)
In [ ]: