From f92c065ec18c00631b0298a41959c0c963b93868 Mon Sep 17 00:00:00 2001 From: Elad Alfassa Date: Wed, 5 Jun 2013 14:08:32 +0300 Subject: [PATCH] [units] Add new unit conversion module Only does temperatures right now. TODO: Length, volume, mass --- units.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 units.py diff --git a/units.py b/units.py new file mode 100644 index 0000000000..1423571401 --- /dev/null +++ b/units.py @@ -0,0 +1,45 @@ +# -*- coding: utf8 -*- +""" +units.py - Unit conversion module for Willie +Copyright © 2013, Elad Alfassa, +Licensed under the Eiffel Forum License 2. + +""" +from willie.module import command, example +import re + +find_temp = re.compile('([0-9]*\.?[0-9]*)[ °]*(K|C|F)', re.IGNORECASE) + +def f_to_c(temp): + return (float(temp) - 32) * 5/9 + +def c_to_k(temp): + return temp + 273.15 + +def c_to_f(temp): + return (9.0/5.0 * temp + 32) + +def k_to_c(temp): + return temp - 273.15 + + +@command('temp') +@example('.temp 100F') +def temperature(bot, trigger): + """ + Convert temperatures + """ + source = find_temp.match(trigger.group(2)).groups() + unit = source[1].upper() + numeric = float(source[0]) + celsius = 0 + if unit == 'C': + celsius = numeric + elif unit == 'F': + celsius = f_to_c(numeric) + elif unit == 'K': + celsius = k_to_c(numeric) + + kelvin = c_to_k(celsius) + fahrenheit = c_to_f(celsius) + bot.reply("%s°C = %s°F = %s°K" % (celsius, fahrenheit, kelvin))