Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: decisionbot.py

Issue 29337738: Issue 3703 - Add "coin flip" feature to abpbot (Closed)
Left Patch Set: Addressed feedback Created Feb. 25, 2016, 8:28 p.m.
Right Patch Set: Fixed a couple more nits Created Feb. 25, 2016, 8:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « abpbot.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """ 1 """
2 Decisionbot - A simple IRC bot to help make "coin flip" decisions. 2 Decisionbot - A simple IRC bot to help make "coin flip" decisions.
3 3
4 botname: x or y? 4 botname: x or y?
5 => x 5 => x
6 6
7 botname: a or b or c? 7 botname: a or b or c?
8 => b 8 => b
9 """ 9 """
10
10 import random 11 import random
Sebastian Noack 2016/02/25 20:47:53 Nit: Please add an empty line between the docstrin
kzar 2016/02/25 20:52:40 Done.
11 import re 12 import re
12 13
13 from irclib import nm_to_n 14 from irclib import nm_to_n
14 15
15 16
16 class Decisionbot(): 17 class Decisionbot():
17 def __init__(self, config, queue): 18 def __init__(self, config, queue):
18 self.queue = queue 19 self.queue = queue
19 20
20 nickname = config.get("main", "nickname") 21 nickname = config.get("main", "nickname")
21 self.question_regexp = re.compile("^%s:?(.+\s+or\s+.+)\?+\s*$" % 22 self.question_regexp = re.compile(r"^%s:?(.+\s+or\s+.+)\?+\s*$" %
Sebastian Noack 2016/02/25 20:47:53 I just realized that you don't use r"" strings. Bu
kzar 2016/02/25 20:52:40 Done.
22 re.escape(nickname), re.IGNORECASE) 23 re.escape(nickname), re.IGNORECASE)
23 self.question_delim_regexp = re.compile("\s+or\s+", re.IGNORECASE) 24 self.question_delim_regexp = re.compile(r"\s+or\s+", re.IGNORECASE)
24 25
25 def on_pubmsg(self, connection, event): 26 def on_pubmsg(self, connection, event):
26 channel = event.target() 27 channel = event.target()
27 message = event.arguments()[0] 28 message = event.arguments()[0]
28 sender = nm_to_n(event.source()) 29 sender = nm_to_n(event.source())
29 30
30 match = self.question_regexp.search(message) 31 match = self.question_regexp.search(message)
31 if (match): 32 if (match):
32 choices = self.question_delim_regexp.split(match.group(1).strip("? \t")) 33 choices = self.question_delim_regexp.split(match.group(1).strip("? \t"))
33 if len(choices) > 1: 34 if len(choices) > 1:
34 self.say_public(channel, "%s: %s" % (sender, random.choice(choices))) 35 self.say_public(channel, "%s: %s" % (sender, random.choice(choices)))
35 36
36 def say_public(self, channel, text): 37 def say_public(self, channel, text):
37 self.queue.send(text, channel) 38 self.queue.send(text, channel)
LEFTRIGHT
« abpbot.py ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld