mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-11 12:00:45 +01:00
Added the Math plugin in the new plugin package format.
This commit is contained in:
parent
4aa220eada
commit
d9df5136cd
1
plugins/Math/README.txt
Normal file
1
plugins/Math/README.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Insert a description of your plugin here, with any notes, etc. about using it.
|
54
plugins/Math/__init__.py
Normal file
54
plugins/Math/__init__.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
###
|
||||||
|
# Copyright (c) 2004, Jeremiah Fincher
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer in the
|
||||||
|
# documentation and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of the author of this software nor the name of
|
||||||
|
# contributors to this software may be used to endorse or promote products
|
||||||
|
# derived from this software without specific prior written consent.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
###
|
||||||
|
|
||||||
|
"""
|
||||||
|
Various math-related commands.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import supybot
|
||||||
|
import supybot.world as world
|
||||||
|
|
||||||
|
__author__ = supybot.authors.jemfinch
|
||||||
|
|
||||||
|
# This is a dictionary mapping supybot.Author instances to lists of
|
||||||
|
# contributions.
|
||||||
|
__contributors__ = {supybot.Author('Keith Jones', 'kmj', ''): 'convert'}
|
||||||
|
|
||||||
|
import config
|
||||||
|
import plugin
|
||||||
|
reload(plugin) # In case we're being reloaded.
|
||||||
|
|
||||||
|
if world.testing:
|
||||||
|
import test
|
||||||
|
|
||||||
|
Class = plugin.Class
|
||||||
|
configure = config.configure
|
||||||
|
|
||||||
|
|
||||||
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
48
plugins/Math/config.py
Normal file
48
plugins/Math/config.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
###
|
||||||
|
# Copyright (c) 2004, Jeremiah Fincher
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer in the
|
||||||
|
# documentation and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of the author of this software nor the name of
|
||||||
|
# contributors to this software may be used to endorse or promote products
|
||||||
|
# derived from this software without specific prior written consent.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
###
|
||||||
|
|
||||||
|
import supybot.conf as conf
|
||||||
|
import supybot.registry as registry
|
||||||
|
|
||||||
|
def configure(advanced):
|
||||||
|
# This will be called by supybot to configure this module. advanced is
|
||||||
|
# a bool that specifies whether the user identified himself as an advanced
|
||||||
|
# user or not. You should effect your configuration by manipulating the
|
||||||
|
# registry as appropriate.
|
||||||
|
from supybot.questions import expect, anything, something, yn
|
||||||
|
conf.registerPlugin('Math', True)
|
||||||
|
|
||||||
|
|
||||||
|
Math = conf.registerPlugin('Math')
|
||||||
|
# This is where your configuration variables (if any) should go. For example:
|
||||||
|
# conf.registerGlobalValue(Math, 'someConfigVariableName',
|
||||||
|
# registry.Boolean(False, """Help for someConfigVariableName."""))
|
||||||
|
|
||||||
|
|
||||||
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78
|
1181
plugins/Math/convertcore.py
Normal file
1181
plugins/Math/convertcore.py
Normal file
File diff suppressed because it is too large
Load Diff
318
plugins/Math/plugin.py
Normal file
318
plugins/Math/plugin.py
Normal file
@ -0,0 +1,318 @@
|
|||||||
|
###
|
||||||
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer in the
|
||||||
|
# documentation and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of the author of this software nor the name of
|
||||||
|
# contributors to this software may be used to endorse or promote products
|
||||||
|
# derived from this software without specific prior written consent.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
###
|
||||||
|
|
||||||
|
from __future__ import division
|
||||||
|
|
||||||
|
import supybot.plugins as plugins
|
||||||
|
|
||||||
|
import re
|
||||||
|
import math
|
||||||
|
import cmath
|
||||||
|
import types
|
||||||
|
import string
|
||||||
|
from itertools import imap
|
||||||
|
|
||||||
|
import supybot.utils as utils
|
||||||
|
from supybot.commands import *
|
||||||
|
import supybot.callbacks as callbacks
|
||||||
|
|
||||||
|
import convertcore
|
||||||
|
|
||||||
|
baseArg = ('int', 'base', lambda i: i <= 36)
|
||||||
|
|
||||||
|
class Math(callbacks.Privmsg):
|
||||||
|
def base(self, irc, msg, args, frm, to, number):
|
||||||
|
"""<fromBase> [<toBase>] <number>
|
||||||
|
|
||||||
|
Converts <number> from base <fromBase> to base <toBase>.
|
||||||
|
If <toBase> is left out, it converts to decimal.
|
||||||
|
"""
|
||||||
|
if not number:
|
||||||
|
number = str(to)
|
||||||
|
to = 10
|
||||||
|
try:
|
||||||
|
irc.reply(self._convertBaseToBase(number, to, frm))
|
||||||
|
except ValueError:
|
||||||
|
irc.error('Invalid <number> for base %s: %s' % (frm, number))
|
||||||
|
base = wrap(base, [('int', 'base', lambda i: 2 <= i <= 36),
|
||||||
|
optional(('int', 'base', lambda i: 2 <= i <= 36), 10),
|
||||||
|
additional('something')])
|
||||||
|
|
||||||
|
def _convertDecimalToBase(self, number, base):
|
||||||
|
"""Convert a decimal number to another base; returns a string."""
|
||||||
|
if number == 0:
|
||||||
|
return '0'
|
||||||
|
elif number < 0:
|
||||||
|
negative = True
|
||||||
|
number = -number
|
||||||
|
else:
|
||||||
|
negative = False
|
||||||
|
digits = []
|
||||||
|
while number != 0:
|
||||||
|
digit = number % base
|
||||||
|
if digit >= 10:
|
||||||
|
digit = string.uppercase[digit - 10]
|
||||||
|
else:
|
||||||
|
digit = str(digit)
|
||||||
|
digits.append(digit)
|
||||||
|
number = number // base
|
||||||
|
digits.reverse()
|
||||||
|
return '-'*negative + ''.join(digits)
|
||||||
|
|
||||||
|
def _convertBaseToBase(self, number, toBase, fromBase):
|
||||||
|
"""Convert a number from any base, 2 through 36, to any other
|
||||||
|
base, 2 through 36. Returns a string."""
|
||||||
|
number = long(str(number), fromBase)
|
||||||
|
if toBase == 10:
|
||||||
|
return str(number)
|
||||||
|
return self._convertDecimalToBase(number, toBase)
|
||||||
|
|
||||||
|
_mathEnv = {'__builtins__': types.ModuleType('__builtins__'), 'i': 1j}
|
||||||
|
_mathEnv.update(math.__dict__)
|
||||||
|
_mathEnv.update(cmath.__dict__)
|
||||||
|
def _sqrt(x):
|
||||||
|
if isinstance(x, complex) or x < 0:
|
||||||
|
return cmath.sqrt(x)
|
||||||
|
else:
|
||||||
|
return math.sqrt(x)
|
||||||
|
_mathEnv['sqrt'] = _sqrt
|
||||||
|
_mathEnv['abs'] = abs
|
||||||
|
_mathEnv['max'] = max
|
||||||
|
_mathEnv['min'] = min
|
||||||
|
_mathRe = re.compile(r'((?:(?<![A-Fa-f\d)])-)?'
|
||||||
|
r'(?:0x[A-Fa-f\d]+|'
|
||||||
|
r'0[0-7]+|'
|
||||||
|
r'\d+\.\d+|'
|
||||||
|
r'\.\d+|'
|
||||||
|
r'\d+\.|'
|
||||||
|
r'\d+))')
|
||||||
|
def _floatToString(self, x):
|
||||||
|
if -1e-10 < x < 1e-10:
|
||||||
|
return '0'
|
||||||
|
elif -1e-10 < int(x) - x < 1e-10:
|
||||||
|
return str(int(x))
|
||||||
|
else:
|
||||||
|
return str(x)
|
||||||
|
|
||||||
|
def _complexToString(self, x):
|
||||||
|
realS = self._floatToString(x.real)
|
||||||
|
imagS = self._floatToString(x.imag)
|
||||||
|
if imagS == '0':
|
||||||
|
return realS
|
||||||
|
elif imagS == '1':
|
||||||
|
imagS = '+i'
|
||||||
|
elif imagS == '-1':
|
||||||
|
imagS = '-i'
|
||||||
|
elif x.imag < 0:
|
||||||
|
imagS = '%si' % imagS
|
||||||
|
else:
|
||||||
|
imagS = '+%si' % imagS
|
||||||
|
if realS == '0' and imagS == '0':
|
||||||
|
return '0'
|
||||||
|
elif realS == '0':
|
||||||
|
return imagS.lstrip('+')
|
||||||
|
elif imagS == '0':
|
||||||
|
return realS
|
||||||
|
else:
|
||||||
|
return '%s%s' % (realS, imagS)
|
||||||
|
|
||||||
|
###
|
||||||
|
# So this is how the 'calc' command works:
|
||||||
|
# First, we make a nice little safe environment for evaluation; basically,
|
||||||
|
# the names in the 'math' and 'cmath' modules. Then, we remove the ability
|
||||||
|
# of a random user to get ints evaluated: this means we have to turn all
|
||||||
|
# int literals (even octal numbers and hexadecimal numbers) into floats.
|
||||||
|
# Then we delete all square brackets, underscores, and whitespace, so no
|
||||||
|
# one can do list comprehensions or call __...__ functions.
|
||||||
|
###
|
||||||
|
def calc(self, irc, msg, args, text):
|
||||||
|
"""<math expression>
|
||||||
|
|
||||||
|
Returns the value of the evaluated <math expression>. The syntax is
|
||||||
|
Python syntax; the type of arithmetic is floating point. Floating
|
||||||
|
point arithmetic is used in order to prevent a user from being able to
|
||||||
|
crash to the bot with something like 10**10**10**10. One consequence
|
||||||
|
is that large values such as 10**24 might not be exact.
|
||||||
|
"""
|
||||||
|
if text != text.translate(string.ascii, '_[]'):
|
||||||
|
irc.error('There\'s really no reason why you should have '
|
||||||
|
'underscores or brackets in your mathematical '
|
||||||
|
'expression. Please remove them.')
|
||||||
|
return
|
||||||
|
#text = text.translate(string.ascii, '_[] \t')
|
||||||
|
if 'lambda' in text:
|
||||||
|
irc.error('You can\'t use lambda in this command.')
|
||||||
|
return
|
||||||
|
text = text.lower()
|
||||||
|
def handleMatch(m):
|
||||||
|
s = m.group(1)
|
||||||
|
if s.startswith('0x'):
|
||||||
|
i = int(s, 16)
|
||||||
|
elif s.startswith('0') and '.' not in s:
|
||||||
|
try:
|
||||||
|
i = int(s, 8)
|
||||||
|
except ValueError:
|
||||||
|
i = int(s)
|
||||||
|
else:
|
||||||
|
i = float(s)
|
||||||
|
x = complex(i)
|
||||||
|
if x == abs(x):
|
||||||
|
x = abs(x)
|
||||||
|
return str(x)
|
||||||
|
text = self._mathRe.sub(handleMatch, text)
|
||||||
|
try:
|
||||||
|
self.log.info('evaluating %s from %s' %
|
||||||
|
(utils.quoted(text), msg.prefix))
|
||||||
|
x = complex(eval(text, self._mathEnv, self._mathEnv))
|
||||||
|
irc.reply(self._complexToString(x))
|
||||||
|
except OverflowError:
|
||||||
|
maxFloat = math.ldexp(0.9999999999999999, 1024)
|
||||||
|
irc.error('The answer exceeded %s or so.' % maxFloat)
|
||||||
|
except TypeError:
|
||||||
|
irc.error('Something in there wasn\'t a valid number.')
|
||||||
|
except NameError, e:
|
||||||
|
irc.error('%s is not a defined function.' % str(e).split()[1])
|
||||||
|
except Exception, e:
|
||||||
|
irc.error(str(e))
|
||||||
|
calc = wrap(calc, ['text'])
|
||||||
|
|
||||||
|
def icalc(self, irc, msg, args, text):
|
||||||
|
"""<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.
|
||||||
|
"""
|
||||||
|
if text != text.translate(string.ascii, '_[]'):
|
||||||
|
irc.error('There\'s really no reason why you should have '
|
||||||
|
'underscores or brackets in your mathematical '
|
||||||
|
'expression. Please remove them.')
|
||||||
|
return
|
||||||
|
# This removes spaces, too, but we'll leave the removal of _[] for
|
||||||
|
# safety's sake.
|
||||||
|
text = text.translate(string.ascii, '_[] \t')
|
||||||
|
if 'lambda' in text:
|
||||||
|
irc.error('You can\'t use lambda in this command.')
|
||||||
|
return
|
||||||
|
text = text.replace('lambda', '')
|
||||||
|
try:
|
||||||
|
self.log.info('evaluating %s from %s' %
|
||||||
|
(utils.quoted(text), msg.prefix))
|
||||||
|
irc.reply(str(eval(text, self._mathEnv, self._mathEnv)))
|
||||||
|
except OverflowError:
|
||||||
|
maxFloat = math.ldexp(0.9999999999999999, 1024)
|
||||||
|
irc.error('The answer exceeded %s or so.' % maxFloat)
|
||||||
|
except TypeError:
|
||||||
|
irc.error('Something in there wasn\'t a valid number.')
|
||||||
|
except NameError, e:
|
||||||
|
irc.error('%s is not a defined function.' % str(e).split()[1])
|
||||||
|
except Exception, e:
|
||||||
|
irc.error(utils.exnToString(e))
|
||||||
|
icalc = wrap(icalc, [('checkCapability', 'trusted'), 'text'])
|
||||||
|
|
||||||
|
_rpnEnv = {
|
||||||
|
'dup': lambda s: s.extend([s.pop()]*2),
|
||||||
|
'swap': lambda s: s.extend([s.pop(), s.pop()])
|
||||||
|
}
|
||||||
|
def rpn(self, irc, msg, args):
|
||||||
|
"""<rpn math expression>
|
||||||
|
|
||||||
|
Returns the value of an RPN expression.
|
||||||
|
"""
|
||||||
|
stack = []
|
||||||
|
for arg in args:
|
||||||
|
try:
|
||||||
|
x = complex(arg)
|
||||||
|
if x == abs(x):
|
||||||
|
x = abs(x)
|
||||||
|
stack.append(x)
|
||||||
|
except ValueError: # Not a float.
|
||||||
|
if arg in self._mathEnv:
|
||||||
|
f = self._mathEnv[arg]
|
||||||
|
if callable(f):
|
||||||
|
called = False
|
||||||
|
arguments = []
|
||||||
|
while not called and stack:
|
||||||
|
arguments.append(stack.pop())
|
||||||
|
try:
|
||||||
|
stack.append(f(*arguments))
|
||||||
|
called = True
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
if not called:
|
||||||
|
irc.error('Not enough arguments for %s' % arg)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
stack.append(f)
|
||||||
|
elif arg in self._rpnEnv:
|
||||||
|
self._rpnEnv[arg](stack)
|
||||||
|
else:
|
||||||
|
arg2 = stack.pop()
|
||||||
|
arg1 = stack.pop()
|
||||||
|
s = '%s%s%s' % (arg1, arg, arg2)
|
||||||
|
try:
|
||||||
|
stack.append(eval(s, self._mathEnv, self._mathEnv))
|
||||||
|
except SyntaxError:
|
||||||
|
irc.error('%s is not a defined function.' %
|
||||||
|
utils.quoted(arg))
|
||||||
|
return
|
||||||
|
if len(stack) == 1:
|
||||||
|
irc.reply(str(self._complexToString(complex(stack[0]))))
|
||||||
|
else:
|
||||||
|
s = ', '.join(imap(self._complexToString, imap(complex, stack)))
|
||||||
|
irc.reply('Stack: [%s]' % s)
|
||||||
|
|
||||||
|
def convert(self, irc, msg, args, number, unit1, unit2):
|
||||||
|
"""[<number>] <unit> to <other unit>
|
||||||
|
|
||||||
|
Converts from <unit> to <other unit>. If number isn't given, it
|
||||||
|
defaults to 1. For unit information, see 'units' command.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
newNum = convertcore.convert(number, unit1, unit2)
|
||||||
|
newNum = self._floatToString(newNum)
|
||||||
|
irc.reply(str(newNum))
|
||||||
|
except convertcore.UnitDataError, ude:
|
||||||
|
irc.error(str(ude))
|
||||||
|
convert = wrap(convert, [optional('float', 1.0),'something','to','text'])
|
||||||
|
|
||||||
|
def units(self, irc, msg, args, type):
|
||||||
|
""" [<type>]
|
||||||
|
|
||||||
|
With no arguments, returns a list of measurement types, which can be
|
||||||
|
passed as arguments. When called with a type as an argument, returns
|
||||||
|
the units of that type.
|
||||||
|
"""
|
||||||
|
|
||||||
|
irc.reply(convertcore.units(type))
|
||||||
|
units = wrap(units, [additional('text')])
|
||||||
|
|
||||||
|
Class = Math
|
||||||
|
|
||||||
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
189
plugins/Math/test.py
Normal file
189
plugins/Math/test.py
Normal file
@ -0,0 +1,189 @@
|
|||||||
|
###
|
||||||
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer in the
|
||||||
|
# documentation and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of the author of this software nor the name of
|
||||||
|
# contributors to this software may be used to endorse or promote products
|
||||||
|
# derived from this software without specific prior written consent.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
###
|
||||||
|
|
||||||
|
from supybot.test import *
|
||||||
|
|
||||||
|
class MathTestCase(PluginTestCase):
|
||||||
|
plugins = ('Math',)
|
||||||
|
def testBase(self):
|
||||||
|
self.assertNotRegexp('base 56 asdflkj', 'ValueError')
|
||||||
|
self.assertResponse('base 16 2 F', '1111')
|
||||||
|
self.assertResponse('base 2 16 1111', 'F')
|
||||||
|
self.assertResponse('base 20 BBBB', '92631')
|
||||||
|
self.assertResponse('base 10 20 92631', 'BBBB')
|
||||||
|
self.assertResponse('base 2 36 10', '2')
|
||||||
|
self.assertResponse('base 36 2 10', '100100')
|
||||||
|
self.assertResponse('base 2 1010101', '85')
|
||||||
|
self.assertResponse('base 2 2 11', '11')
|
||||||
|
|
||||||
|
self.assertResponse('base 12 0', '0')
|
||||||
|
self.assertResponse('base 36 2 0', '0')
|
||||||
|
|
||||||
|
|
||||||
|
self.assertNotError("base 36 " +\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ")
|
||||||
|
|
||||||
|
self.assertResponse("base 10 36 [base 36 " +\
|
||||||
|
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"\
|
||||||
|
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"\
|
||||||
|
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"\
|
||||||
|
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"\
|
||||||
|
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"\
|
||||||
|
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"\
|
||||||
|
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz]",
|
||||||
|
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"\
|
||||||
|
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ")
|
||||||
|
|
||||||
|
self.assertResponse('base 2 10 [base 10 2 12]', '12')
|
||||||
|
self.assertResponse('base 16 2 [base 2 16 110101]', '110101')
|
||||||
|
self.assertResponse('base 10 8 [base 8 76532]', '76532')
|
||||||
|
self.assertResponse('base 10 36 [base 36 csalnwea]', 'CSALNWEA')
|
||||||
|
self.assertResponse('base 5 4 [base 4 5 212231]', '212231')
|
||||||
|
|
||||||
|
self.assertError('base 37 1')
|
||||||
|
self.assertError('base 1 1')
|
||||||
|
self.assertError('base 12 1 1')
|
||||||
|
self.assertError('base 1 12 1')
|
||||||
|
self.assertError('base 1.0 12 1')
|
||||||
|
self.assertError('base A 1')
|
||||||
|
|
||||||
|
self.assertError('base 4 4')
|
||||||
|
self.assertError('base 10 12 A')
|
||||||
|
|
||||||
|
print
|
||||||
|
print "If we have not fixed a bug with Math.base, the following ",
|
||||||
|
print "tests will hang the test-suite."
|
||||||
|
self.assertRegexp('base 2 10 [base 10 2 -12]', '-12')
|
||||||
|
self.assertRegexp('base 16 2 [base 2 16 -110101]', '-110101')
|
||||||
|
|
||||||
|
def testCalc(self):
|
||||||
|
self.assertResponse('calc 5*0.06', str(5*0.06))
|
||||||
|
self.assertResponse('calc 2.0-7.0', str(2-7))
|
||||||
|
self.assertResponse('calc (-1)**.5', 'i')
|
||||||
|
self.assertResponse('calc e**(i*pi)+1', '0')
|
||||||
|
self.assertResponse('calc (-5)**.5', '2.2360679775i')
|
||||||
|
self.assertResponse('calc -((-5)**.5)', '-2.2360679775i')
|
||||||
|
self.assertNotRegexp('calc [9, 5] + [9, 10]', 'TypeError')
|
||||||
|
self.assertError('calc [9, 5] + [9, 10]')
|
||||||
|
self.assertNotError('calc degrees(2)')
|
||||||
|
self.assertNotError('calc (2 * 3) - 2*(3*4)')
|
||||||
|
self.assertNotError('calc (3) - 2*(3*4)')
|
||||||
|
self.assertNotError('calc (1600 * 1200) - 2*(1024*1280)')
|
||||||
|
self.assertNotError('calc 3-2*4')
|
||||||
|
self.assertNotError('calc (1600 * 1200)-2*(1024*1280)')
|
||||||
|
|
||||||
|
def testCalcNoNameError(self):
|
||||||
|
self.assertNotRegexp('calc foobar(x)', 'NameError')
|
||||||
|
|
||||||
|
def testCalcImaginary(self):
|
||||||
|
self.assertResponse('calc 3 + sqrt(-1)', '3+i')
|
||||||
|
|
||||||
|
def testCalcFloorWorksWithSqrt(self):
|
||||||
|
self.assertNotError('calc floor(sqrt(5))')
|
||||||
|
|
||||||
|
def testCaseInsensitive(self):
|
||||||
|
self.assertNotError('calc PI**PI')
|
||||||
|
|
||||||
|
def testCalcMaxMin(self):
|
||||||
|
self.assertResponse('calc max(1,2)', '2')
|
||||||
|
self.assertResponse('calc min(1,2)', '1')
|
||||||
|
|
||||||
|
def testICalc(self):
|
||||||
|
self.assertResponse('icalc 1^1', '0')
|
||||||
|
self.assertResponse('icalc 10**24', '1' + '0'*24)
|
||||||
|
self.assertRegexp('icalc 49/6', '8.16')
|
||||||
|
|
||||||
|
def testRpn(self):
|
||||||
|
self.assertResponse('rpn 5 2 +', '7')
|
||||||
|
self.assertResponse('rpn 1 2 3 +', 'Stack: [1, 5]')
|
||||||
|
self.assertResponse('rpn 1 dup', 'Stack: [1, 1]')
|
||||||
|
self.assertResponse('rpn 2 3 4 + -', str(2-7))
|
||||||
|
self.assertNotError('rpn 2 degrees')
|
||||||
|
|
||||||
|
def testRpnSwap(self):
|
||||||
|
self.assertResponse('rpn 1 2 swap', 'Stack: [2, 1]')
|
||||||
|
|
||||||
|
def testRpmNoSyntaxError(self):
|
||||||
|
self.assertNotRegexp('rpn 2 3 foobar', 'SyntaxError')
|
||||||
|
|
||||||
|
def testConvert(self):
|
||||||
|
self.assertResponse('convert 1 m to cm', '100')
|
||||||
|
self.assertResponse('convert m to cm', '100')
|
||||||
|
self.assertResponse('convert 3 metres to km', '0.003')
|
||||||
|
self.assertResponse('convert 32 F to C', '0')
|
||||||
|
self.assertResponse('convert 32 C to F', '89.6')
|
||||||
|
self.assertResponse('convert [calc 2*pi] rad to degree', '360')
|
||||||
|
self.assertResponse('convert amu to atomic mass unit',
|
||||||
|
'1')
|
||||||
|
self.assertResponse('convert [calc 2*pi] rad to circle', '1')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
self.assertError('convert 1 meatball to bananas')
|
||||||
|
self.assertError('convert 1 gram to meatballs')
|
||||||
|
self.assertError('convert 1 mol to grams')
|
||||||
|
self.assertError('convert 1 m to kpa')
|
||||||
|
|
||||||
|
def testConvertSingularPlural(self):
|
||||||
|
self.assertResponse('convert [calc 2*pi] rads to degrees', '360')
|
||||||
|
self.assertResponse('convert 1 carat to grams', '0.2')
|
||||||
|
self.assertResponse('convert 10 lbs to oz', '160')
|
||||||
|
self.assertResponse('convert mA to amps', '0.001')
|
||||||
|
|
||||||
|
def testConvertCaseSensitivity(self):
|
||||||
|
self.assertError('convert MA to amps')
|
||||||
|
self.assertError('convert M to amps')
|
||||||
|
self.assertError('convert Radians to rev')
|
||||||
|
|
||||||
|
def testUnits(self):
|
||||||
|
self.assertNotError('units')
|
||||||
|
self.assertNotError('units mass')
|
||||||
|
self.assertNotError('units flux density')
|
||||||
|
|
||||||
|
def testAbs(self):
|
||||||
|
self.assertResponse('calc abs(2)', '2')
|
||||||
|
self.assertResponse('calc abs(-2)', '2')
|
||||||
|
self.assertResponse('calc abs(2.0)', '2')
|
||||||
|
self.assertResponse('calc abs(-2.0)', '2')
|
||||||
|
|
||||||
|
|
||||||
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
|
21
setup.py
21
setup.py
@ -29,6 +29,17 @@
|
|||||||
# POSSIBILITY OF SUCH DAMAGE.
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
###
|
###
|
||||||
|
|
||||||
|
plugins = [
|
||||||
|
'Admin',
|
||||||
|
'Channel',
|
||||||
|
'Config',
|
||||||
|
'Math',
|
||||||
|
'Misc',
|
||||||
|
'Owner',
|
||||||
|
'User',
|
||||||
|
'Utilities',
|
||||||
|
]
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if sys.version_info < (2, 3, 0):
|
if sys.version_info < (2, 3, 0):
|
||||||
@ -84,16 +95,6 @@ if clean:
|
|||||||
print 'Couldn\'t remove former installation: %s' % e
|
print 'Couldn\'t remove former installation: %s' % e
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
plugins = [
|
|
||||||
'Admin',
|
|
||||||
'Channel',
|
|
||||||
'Config',
|
|
||||||
'Misc',
|
|
||||||
'Owner',
|
|
||||||
'User',
|
|
||||||
'Utilities',
|
|
||||||
]
|
|
||||||
|
|
||||||
packages = ['supybot',
|
packages = ['supybot',
|
||||||
'supybot.drivers',
|
'supybot.drivers',
|
||||||
'supybot.plugins',] + \
|
'supybot.plugins',] + \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user