Added convert and units command.

This commit is contained in:
Jeremy Fincher 2003-10-02 23:19:38 +00:00
parent 41bacaba13
commit ff6fdf02f3
2 changed files with 56 additions and 0 deletions

View File

@ -41,6 +41,8 @@ import math
import cmath
from itertools import imap
import unum.units
import utils
import privmsgs
import callbacks
@ -189,8 +191,51 @@ class Math(callbacks.Privmsg):
s = ', '.join(imap(self._complexToString, imap(complex, stack)))
irc.reply(msg, 'Stack: [%s]' % s)
_convertEnv = {'__builtins__': new.module('__builtins__')}
for (k, v) in unum.units.__dict__.iteritems():
if isinstance(v, unum.Unum):
_convertEnv[k.lower()] = v
def convert(self, irc, msg, args):
"""<number> <units> to <other units>
Converts the first number of <units> to the <other units>. Valid units
expressions include the standard Python math operators applied to valid
units.
"""
(n, unit1, to, unit2) = privmsgs.getArgs(args, needed=4)
if to != 'to':
raise callbacks.ArgumentError
try:
n = float(n)
except ValueError:
irc.error(msg, '%s is not a valid number.' % n)
return
try:
u1 = eval(unit1.lower(), self._convertEnv, self._convertEnv)
except:
irc.error(msg, '%s is not a valid units expression.' % units1)
return
try:
u2 = eval(unit2.lower(), self._convertEnv, self._convertEnv)
except:
irc.error(msg, '%s is not a valid units expression.' % units2)
return
try:
irc.reply(msg, str((n*u1).as(u2)))
except Exception, e:
irc.error(msg, str(e))
def units(self, irc, msg, args):
"""takes no arguments
Returns all the valid units.
"""
L = self._convertEnv.keys()
L.remove('__builtins__')
L.sort()
irc.reply(msg, utils.commaAndify(L))
Class = Math
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -49,6 +49,17 @@ class MathTestCase(PluginTestCase, PluginDocumentation):
self.assertResponse('rpn 1 dup', 'Stack: [1, 1]')
self.assertResponse('rpn 2 3 4 + -', str(2-7))
def testConvert(self):
self.assertResponse('convert 1 m to cm', '100.0 [cm]')
self.assertResponse('convert 1 M to cm', '100.0 [cm]')
self.assertResponse('convert 1 m to CM', '100.0 [cm]')
self.assertResponse('convert 1 m to cM', '100.0 [cm]')
self.assertError('convert 1 m to kpa')
self.assertNotRegexp('convert 1 m to kpa', 'UNUM_ERROR')
def testUnits(self):
self.assertNotError('units')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: