Skip to content

Commit

Permalink
[units] add length units
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Alfassa committed Jun 5, 2013
1 parent f92c065 commit 2894885
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion units.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
Licensed under the Eiffel Forum License 2.
"""
from willie.module import command, example
from willie.module import command, commands, example
import re

find_temp = re.compile('([0-9]*\.?[0-9]*)[ °]*(K|C|F)', re.IGNORECASE)
find_length = re.compile('([0-9]*\.?[0-9]*)[ ]*(mile|m|meter|km|cm|kilometer|inch|in|ft|foot|feet)', re.IGNORECASE)

def f_to_c(temp):
return (float(temp) - 32) * 5/9
Expand Down Expand Up @@ -43,3 +44,35 @@ def temperature(bot, trigger):
kelvin = c_to_k(celsius)
fahrenheit = c_to_f(celsius)
bot.reply("%s°C = %s°F = %s°K" % (celsius, fahrenheit, kelvin))

@commands('length', 'distance')
@example('.distance 3km')
def distance(bot, trigger):
"""
Convert distances
"""
source = find_length.match(trigger.group(2)).groups()
unit = source[1].lower()
numeric = float(source[0])
meter = 0
if unit == "m" or unit == "meter":
meter = numeric
elif unit == "kilometer" or unit == "km":
meter = numeric * 1000
elif unit == "mile":
meter = numeric / 0.00062137
elif unit == "inch" or unit == "in":
meter = numeric / 39.370
elif unit == "cm":
meter = numeric / 100
elif unit == "ft" or unit == "foot" or unit == "feet":
meter = numeric / 3.2808

inch = meter * 39.370
mile = meter * 0.00062137
if meter >= 1000:
bot.reply("%skm = %smile" % (meter/1000, mile))
elif meter < 1:
bot.reply("%scm = %sinch" % (meter*100, inch))
else:
bot.reply("%sm = %sinch" % (meter, inch))

0 comments on commit 2894885

Please sign in to comment.