From: Ira W. Snyder Date: Mon, 25 Dec 2006 18:55:07 +0000 (-0800) Subject: [RARSLAVE] Change RarslaveLogger operation X-Git-Tag: v2.0.0~49 X-Git-Url: https://www.irasnyder.com/gitweb/?p=rarslave2.git;a=commitdiff_plain;h=e33dad4f4feea215742be92a5a5ed21832275f39 [RARSLAVE] Change RarslaveLogger operation Changes the RarslaveLogger class' method of operation to not require a pre-generated id. Instead, it still requires an id, but keeps track of all id's given to it. This also removes the unit tests that tested the old behavior. Signed-off-by: Ira W. Snyder --- diff --git a/RarslaveLogger.py b/RarslaveLogger.py index bed18d4..83752f0 100644 --- a/RarslaveLogger.py +++ b/RarslaveLogger.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# vim: set ts=4 sts=4 sw=4 textwidth=112: +# vim: set ts=4 sts=4 sw=4 textwidth=92: class RarslaveMessage (object): def __init__ (self, msg, printquiet): @@ -19,34 +19,28 @@ class RarslaveLogger (object): # It will then be able to print all of them out. def __init__ (self): - self.__uniq_num = 0 self.__msg_dict = {} - def getUniqueID (self): - # Generate a unique id. This implementation is pretty simple, - # just generating an increasing set of integers. - - # This function also sets up the dictionary which will hold the messages. - self.__uniq_num += 1 - self.__msg_dict[self.__uniq_num] = [] - - return self.__uniq_num - - def isValidID (self, id): - # Check if the id given is a valid one. This is done by checking if - # the id is in the dictionary. - return id in self.__msg_dict.keys() + def __get_key (self, id): + """__get_key (id) -> key""" + # Get a valid key which will always be the same as any other similar key, even if + # the actual memory reference is different. + return str(id) def addMessage (self, id, msg, printquiet=True): # Add a message to the dictionary, to be printed later. # The printquiet variable controls whether the message will be printed # normally, or only in verbose mode. The default is to print always. - # Check the id - assert self.isValidID (id) + # Get the key + key = self.__get_key (id) + + # Create the dictionary entry if necessary + if key not in self.__msg_dict.keys(): + self.__msg_dict[key] = [] # Get the list from the dictionary, then add the message to it - self.__msg_dict[id].append (RarslaveMessage (msg, printquiet)) + self.__msg_dict[key].append (RarslaveMessage (msg, printquiet)) def printAllMessages (self, verbose=False): # Print all messages in groups by id @@ -62,7 +56,12 @@ class RarslaveLogger (object): print def main (): - pass + logger = RarslaveLogger () + logger.addMessage (1, 'hello 1') + logger.addMessage (2, 'hello 2') + logger.addMessage (2, 'world 2') + logger.addMessage (1, 'world 1') + logger.printAllMessages () if __name__ == '__main__': main () diff --git a/rarslave-test.py b/rarslave-test.py index e2733ba..389dcf2 100644 --- a/rarslave-test.py +++ b/rarslave-test.py @@ -113,28 +113,6 @@ class rarslavetest (unittest.TestCase): self.assertFalse (RarslaveMessage (STR1, True).isVerbose()) self.assertFalse (RarslaveMessage (STR2, True).isVerbose()) - ### RarslaveLogger tests - - def testgetUniqueID (self): - generated = [] - - for i in xrange(100): - id = self.logger.getUniqueID () - if id in generated: - self.fail ("Already generated ID was generated again") - - generated.append (id) - - def testisValidID (self): - id1 = self.logger.getUniqueID () - id2 = self.logger.getUniqueID () - - self.assertFalse (self.logger.isValidID (-100)) - self.assertFalse (self.logger.isValidID (1000)) - - self.assertTrue (self.logger.isValidID (id1)) - self.assertTrue (self.logger.isValidID (id2)) - if __name__ == '__main__': unittest.main ()