Math: Allow 'factorial()' in icalc.

This commit is contained in:
Valentin Lorentz 2012-05-07 17:52:02 +02:00
parent 695078edeb
commit 1f60a9487c
2 changed files with 5 additions and 2 deletions

View File

@ -106,7 +106,8 @@ class Math(callbacks.Plugin):
_mathEnv['abs'] = abs
_mathEnv['max'] = max
_mathEnv['min'] = min
_mathEnv.pop('factorial')
_mathSafeEnv = dict([(x,y) for x,y in _mathEnv.items()
if x not in ['factorial']])
_mathRe = re.compile(r'((?:(?<![A-Fa-f\d)])-)?'
r'(?:0x[A-Fa-f\d]+|'
r'0[0-7]+|'
@ -196,7 +197,7 @@ class Math(callbacks.Plugin):
text = self._mathRe.sub(handleMatch, text)
try:
self.log.info('evaluating %q from %s', text, msg.prefix)
x = complex(eval(text, self._mathEnv, self._mathEnv))
x = complex(eval(text, self._mathSafeEnv, self._mathSafeEnv))
irc.reply(self._complexToString(x))
except OverflowError:
maxFloat = math.ldexp(0.9999999999999999, 1024)

View File

@ -110,6 +110,7 @@ class MathTestCase(PluginTestCase):
self.assertNotError('calc (1600 * 1200) - 2*(1024*1280)')
self.assertNotError('calc 3-2*4')
self.assertNotError('calc (1600 * 1200)-2*(1024*1280)')
self.assertError('calc factorial(99)')
def testCalcNoNameError(self):
self.assertNotRegexp('calc foobar(x)', 'NameError')
@ -134,6 +135,7 @@ class MathTestCase(PluginTestCase):
self.assertResponse('icalc 1^1', '0')
self.assertResponse('icalc 10**24', '1' + '0'*24)
self.assertRegexp('icalc 49/6', '8.16')
self.assertNotError('icalc factorial(99)')
def testRpn(self):
self.assertResponse('rpn 5 2 +', '7')