simplifying interface (functions can now take one argument and return one string)

This commit is contained in:
Ryan Hitchman 2009-03-15 01:25:32 -06:00
parent ffc57460b1
commit a048452deb
5 changed files with 18 additions and 14 deletions

6
bot.py
View File

@ -119,7 +119,11 @@ class FakeBot(object):
self.say(self.input.nick + ': ' + msg) self.say(self.input.nick + ': ' + msg)
def run(self): def run(self):
out = self.func(self, self.input) ac = self.func.func_code.co_argcount
if ac == 2:
out = self.func(self, self.input)
elif ac == 1:
out = self.func(self.input.inp)
if out is not None: if out is not None:
if self.doreply: if self.doreply:
self.reply(unicode(out)) self.reply(unicode(out))

View File

@ -8,10 +8,10 @@ BUFFER_SIZE = 5000
MAX_STEPS = 1000000 MAX_STEPS = 1000000
#command #command
def bf(bot, input): def bf(input):
"""Runs a Brainfuck program.""" """Runs a Brainfuck program."""
program = re.sub('[^][<>+-.,]', '', input.inp) program = re.sub('[^][<>+-.,]', '', input)
# create a dict of brackets pairs, for speed later on # create a dict of brackets pairs, for speed later on
brackets={} brackets={}

View File

@ -26,13 +26,13 @@ def nrolls(count, n):
(((n+1)*(2*n+1)/6.-(.5*(1+n))**2)*count)**.5)) (((n+1)*(2*n+1)/6.-(.5*(1+n))**2)*count)**.5))
#command #command
def dice(bot, input): def dice(input):
".dice <diceroll> - simulates dicerolls, e.g. .dice 2d20-d5+4 roll 2 " \ ".dice <diceroll> - simulates dicerolls, e.g. .dice 2d20-d5+4 roll 2 " \
"D20s, subtract 1D5, add 4" "D20s, subtract 1D5, add 4"
if not input.inp.strip(): if not input.strip():
return dice.__doc__ return dice.__doc__
spec = whitespace_re.sub('', input.inp) spec = whitespace_re.sub('', input)
if not valid_diceroll_re.match(spec): if not valid_diceroll_re.match(spec):
return "Invalid diceroll" return "Invalid diceroll"
sum = 0 sum = 0

View File

@ -1,14 +1,14 @@
import hashlib import hashlib
#command #command
def md5(bot, input): def md5(input):
return hashlib.md5(input.inp).hexdigest() return hashlib.md5(input).hexdigest()
#command #command
def sha1(bot, input): def sha1(input):
return hashlib.sha1(input.inp).hexdigest() return hashlib.sha1(input).hexdigest()
#command #command
def hash(bot, input): def hash(input):
return ', '.join(x + ": " + getattr(hashlib, x)(input.inp).hexdigest() return ', '.join(x + ": " + getattr(hashlib, x)(input).hexdigest()
for x in 'md5 sha1 sha256'.split()) for x in 'md5 sha1 sha256'.split())

View File

@ -4,9 +4,9 @@ import re
re_lineends = re.compile(r'[\r\n]*') re_lineends = re.compile(r'[\r\n]*')
#command #command
def py(bot, input): def py(input):
res = urllib.urlopen("http://eval.appspot.com/eval?statement=%s" % res = urllib.urlopen("http://eval.appspot.com/eval?statement=%s" %
urllib.quote(input.inp.strip(),safe='')).readlines() urllib.quote(input.strip(),safe='')).readlines()
if len(res) == 0: if len(res) == 0:
return return
res[0] = re_lineends.split(res[0])[0] res[0] = re_lineends.split(res[0])[0]