Skip to content

Commit

Permalink
[Units]Bugfix, Negative temperatures and idiotproofing... sort of.
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrope committed Jun 20, 2013
1 parent d940965 commit 8c16c2c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions units.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
Licensed under the Eiffel Forum License 2.
"""
from willie.module import command, commands, example
from willie.module import command, commands, example, NOLIMIT
import re

find_temp = re.compile('([0-9]*\.?[0-9]*)[ °]*(K|C|F)', re.IGNORECASE)
find_temp = re.compile('(-?[0-9]*\.?[0-9]*)[ °]*(K|C|F)', re.IGNORECASE)
find_length = re.compile('([0-9]*\.?[0-9]*)[ ]*(mile[s]?|mi|inch|in|foot|feet|ft|yard[s]?|yd|(?:centi|kilo|)meter[s]?|[kc]?m)', re.IGNORECASE)

def f_to_c(temp):
Expand All @@ -31,7 +31,11 @@ def temperature(bot, trigger):
"""
Convert temperatures
"""
source = find_temp.match(trigger.group(2)).groups()
try:
source = find_temp.match(trigger.group(2)).groups()
except AttributeError:
bot.reply("That's not a valid temperature.")
return NOLIMIT
unit = source[1].upper()
numeric = float(source[0])
celsius = 0
Expand All @@ -52,15 +56,19 @@ def distance(bot, trigger):
"""
Convert distances
"""
source = find_length.match(trigger.group(2)).groups()
try:
source = find_length.match(trigger.group(2)).groups()
except AttributeError:
bot.reply("That's not a valid length unit.")
return NOLIMIT
unit = source[1].lower()
numeric = float(source[0])
meter = 0
if unit in ("meters","meter", "m"):
meter = numeric
elif unit in ("kilometers", "kilometer", "km"):
meter = numeric * 1000
elif unit == ("miles", "mile", "mi"):
elif unit in ("miles", "mile", "mi"):
meter = numeric / 0.00062137
elif unit in ("inch", "in"):
meter = numeric / 39.370
Expand Down

0 comments on commit 8c16c2c

Please sign in to comment.