Added an icalc command for doing calculations with real integers.

This commit is contained in:
Jeremy Fincher 2003-12-04 08:29:19 +00:00
parent e46765f426
commit c8783e5ce6
1 changed files with 24 additions and 0 deletions

View File

@ -149,6 +149,30 @@ class Math(callbacks.Privmsg):
except Exception, e:
irc.error(msg, utils.exnToString(e))
def icalc(self, irc, msg, args):
"""<math expression>
This is the same as the calc command except that it allows integer
math, and can thus cause the bot to suck up CPU. Hence it requires
the 'trusted' capability to use.
"""
text = privmsgs.getArgs(args)
text = text.translate(string.ascii, '_[] \t')
text = text.replace('lambda', '')
try:
self.log.info('evaluating %r from %s' % (text, msg.prefix))
irc.reply(msg, str(eval(text, self._mathEnv, self._mathEnv)))
except OverflowError:
maxFloat = math.ldexp(0.9999999999999999, 1024)
irc.error(msg, 'The answer exceeded %s or so.' % maxFloat)
except TypeError:
irc.error(msg, 'Something in there wasn\'t a valid number.')
except NameError, e:
irc.error(msg, '%s is not a defined function.' % str(e).split()[1])
except Exception, e:
irc.error(msg, utils.exnToString(e))
icalc = privmsgs.checkCapability(icalc, 'trusted')
_rpnEnv = {
'dup': lambda s: s.extend([s.pop()]*2),
'swap': lambda s: s.extend([s.pop(), s.pop()])