I'm making a unit converting function for Python for fun.
This is my code, so far:
def UnitConverter(number,*units):
if units == ("feet","inches"):
return number*12
elif units == ("ft","yd"):
return number/3
You probably get the idea of how I'm having this work.
Because I'm obsessive about elegance, good code practice, and overall flow, I want to know what you coders think about this in general, in addition to my main question: How can I efficiently check for a list of permutations in an if statement?
Example: Is there an efficient way to make this work?
def UnitConverter(number,*units):
if units == (("feet" or "foot" or "ft."),("inches" or "in" or "in.")):
return number*12
elif units == ("ft","yd"):
return number/3
If not, is there a way to restructure my program such that someone can enter the three arguments number, unit1, unit2 in a way that on the coding end, I can effectively include all alternate spellings of each unit (feet,foot,ft,etc)?
I really value everyone's opinion.
Thanks!
3 Answers 3
I would pick a standard unit of length, let's say m. Then I would have a single dictionary that gives a factor for each other unit, and convert:
conversion_factors = {
'foot': 0.3048, # Google search '1 foot in m'
'yard': 0.9144,
# etc
}
def unit_convert(number, from_unit='m', to_unit='m'):
m = number * conversion_factor[from_unit]
return m / conversion_factor[to_unit]
For the synonyms (feet, ft, etc) you could make a second dictionary and lookup the canonical name in that first:
conversion_factors = { ... } # as above
synonyms = {
'feet': 'foot',
'ft': 'foot',
...
}
def unit_convert(number, from_unit='m', to_unit='m'):
from_unit = synonyms.get(from_unit, from_unit)
to_unit = synonyms.get(to_unit, to_unit)
# etc
...or just put them in the conversion_factors dictionary multiple times:
conversion_factors = {
'foot': 0.3048, # Google search '1 foot in m'
'feet': 0.3048,
'ft': 0.3048,
'yard': 0.9144,
# etc
}
1 Comment
'feet': 0.3048,.Use sets.
foot_units = {"ft.", "feet", "foot"}
Then you can check ownership in the sets.
if(units[0] in foot_units):
...
Beyond this, make a conversion_factor dictionary that goes to a common conversion element. You can then coerce to your final after.
inches -> feet -> yards
inches -> feet -> feet
RemcoGerlich has a good solution for that step.
1 Comment
Perhaps something like the following, using the in operator which checks for containment:
def UnitConverter(number,*units):
feet = {'feet', 'foot', 'ft.'}
inches = {'inches', 'in', 'in.'}
yards = {'yard', 'yd', 'yd.'}
if units[0] in feet and units[1] in inches:
return number*12
elif units[0] in feet and units[1] in yards:
return number/3
Comments
Explore related questions
See similar questions with these tags.
*unitsand then repeatedly implicitly forcing it to be two values, instead of just accepting, say,fromunit, tounitas two separate parameters?fromunitto some canonical unit, then convert from the canonical unit to thetounit. Then you only need N*2ifconditions instead of N**2.from_unitandto_unitunits.libthat can be used to build, e.g., the dictionary used in RemcoGerlich's answer.