Skip to content

Instantly share code, notes, and snippets.

@vilterp
Created February 6, 2009 01:25
Show Gist options
  • Save vilterp/59153 to your computer and use it in GitHub Desktop.
Save vilterp/59153 to your computer and use it in GitHub Desktop.
converts from one unit to another
conversions = [
# length
('inches','feet',lambda x: x/12.0),
('feet','inches',lambda x: x*12),
('meters','feet',lambda x: x*3.2808399),
('feet','meters',lambda x: x*0.3048),
('inches','centimeters',lambda x: x*2.54),
('meters','centimeters',lambda x: x*100),
('centimeters','meters',lambda x: x/100),
# stoichiometry
('moles','formula units',lambda x: x/6.022e23),
('formula units','moles',lambda x: x*6.022e23),
# pressure
('atmospheres','millimeters of mercury',lambda x: x*760),
('millimeters of mercury','atmospheres',lambda x: x/760),
('atmospheres','pascals',lambda x: x*101.3),
('pascals','atmospheres',lambda x: x/101.3),
('pascals','kilopascals',lambda x: x/1000),
('kilopascals','pascals',lambda x: x*1000),
('torr','millimeters of mercury',lambda x: x),
('millimeters of mercury','torr',lambda x: x),
]
abbreviations = {
'inches': ['in'],
'centimeters': ['cm'],
'feet': ['ft'],
'meters': ['m'],
'moles': ['mol'],
'formula units': ['molecules'],
'atmospheres': ['atm'],
'millimeters of mercury': ['mm hg'],
'pascals': ['Pa'],
'kilopascals': ['kPa'],
'torr': ['torr']
}
def get_unit(abbreviation):
if abbreviation in abbreviations.keys():
return abbreviation
else:
for abbrev in abbreviations.keys():
if abbreviation in abbreviations[abbrev]:
return abbrev
raise NoAbbreviationException(abbreviation)
def get_abbreviation(unit):
try:
return abbreviations[unit][0]
except KeyError:
raise NoUnitException(unit)
class NoAbbreviationException(Exception):
pass
class NoUnitException(Exception):
pass
def conversions_from(unit):
return [conversion for conversion in conversions if conversion[0] == unit]
def convert(amount, from_unit, to_unit, history=[]):
if len(history) == 0: history = [from_unit]
for conversion in conversions_from(from_unit):
if conversion[1] == to_unit:
return conversion[2](amount), history
else:
if conversion[1] not in history:
answer, history = convert(conversion[2](amount),conversion[1],to_unit,history)
if answer is not False:
history.append(conversion[1])
return answer, history
return False, history
if __name__ == '__main__':
try:
while 1:
amt = float(raw_input('amount: '))
try:
from_unit = get_unit(raw_input('from unit: '))
except NoAbbreviationException, e:
print 'dont\'t know what', e, 'is an abbreviation for'
continue
try:
to_unit = get_unit(raw_input('to unit: '))
except NoAbbreviationException, e:
print 'dont\'t know what', e, 'is an abbreviation for'
continue
answer, history = convert(amt,from_unit,to_unit)
if answer is not False:
if len(history) == 1:
print answer
else:
print '%s > %s > %f' % (' > '.join(history),to_unit,answer)
else:
print 'sorry, don\'t know enough conversion factors to do that.'
except (KeyboardInterrupt, EOFError):
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment