def __init__(self, bot): ModuleBase.__init__(self, bot) self.name = "ModuleBaguette" self.users = userList.UserList() self.lines = lines.LinesSeqRnd("modules/resources/bag.txt") self.probabilityBag = Probability(self.RATE_BAG) self.probabilityNotBag = Probability(self.RATE_NOT_BAG)
class ModuleBaguette(ModuleBase): NUMBER_REPORT_MESSAGES = 5 + 1 RATE_BAG = 3/4 RATE_NOT_BAG = 2/100 def __init__(self, bot): ModuleBase.__init__(self, bot) self.name = "ModuleBaguette" self.users = userList.UserList() self.lines = lines.LinesSeqRnd("modules/resources/bag.txt") self.probabilityBag = Probability(self.RATE_BAG) self.probabilityNotBag = Probability(self.RATE_NOT_BAG) def notify_text(self, message_id, from_attr, date, chat, text): name = from_attr["first_name"] if (name in self.users and self.probabilityBag.next()) or self.probabilityNotBag.next(): text = name + " " + next(self.lines) self.bot.sendMessage(text, chat["id"]) def notify_command(self, message_id, from_attr, date, chat, commandName, commandStr): commandStr = commandStr.lower() if commandName == "bag": self.users.add(commandStr) elif commandName == "sbag": self.users.remove(commandStr) def get_commands(self): return [ ("bag", "Add baguette"), ("sbag", "Remove baguette"), ]
class ModuleBaguette(ModuleBase): NUMBER_REPORT_MESSAGES = 5 + 1 RATE_BAG = 3 / 4 RATE_NOT_BAG = 2 / 100 def __init__(self, bot): ModuleBase.__init__(self, bot) self.name = "ModuleBaguette" self.users = userList.UserList() self.lines = lines.LinesSeqRnd("modules/resources/bag.txt") self.probabilityBag = Probability(self.RATE_BAG) self.probabilityNotBag = Probability(self.RATE_NOT_BAG) def notify_text(self, message_id, from_attr, date, chat, text): name = from_attr["first_name"] if (name in self.users and self.probabilityBag.next()) or self.probabilityNotBag.next(): text = name + " " + next(self.lines) self.bot.sendMessage(text, chat["id"]) def notify_command(self, message_id, from_attr, date, chat, commandName, commandStr): commandStr = commandStr.lower() if commandName == "bag": self.users.add(commandStr) elif commandName == "sbag": self.users.remove(commandStr) def get_commands(self): return [ ("bag", "Add baguette"), ("sbag", "Remove baguette"), ]
def test_proba(self): rates = ( 0.25, 0.5, 0.75, ) for rate in rates: p = Probability(rate) cnt = 0 for i in range(self.N): if p.next(): cnt += 1 self.assertAlmostEqual(cnt, self.N * rate, delta=self.N * self.DIFF)
def test_one(self): p = Probability(1) for i in range(self.N): self.assertTrue(p.next())
def test_zero(self): p = Probability(0) for i in range(self.N): self.assertFalse(p.next())