| Total Complexity | 5 |
| Total Lines | 48 |
| Duplicated Lines | 100 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | from event import Event |
||
| 2 | View Code Duplication | class BaseModule(object): |
|
|
|
|||
| 3 | """ |
||
| 4 | A base module class for deriving modules (anything you fine folk write, probably) to inherit from. |
||
| 5 | The nice this is this allows you to define your own post_init and handle functions. |
||
| 6 | |||
| 7 | In your module's post_init, define and register your own events, and pass your module in. |
||
| 8 | |||
| 9 | .. code-block:: python |
||
| 10 | |||
| 11 | def MyModule(BaseModule): |
||
| 12 | def post_init(self): |
||
| 13 | e = Event("__wee__") |
||
| 14 | e.define("foo") |
||
| 15 | self.bot.register_event(e,self) |
||
| 16 | |||
| 17 | |||
| 18 | Bam, you've got the things you need (a bot handle, mostly) and by extending BaseModuleyou implement the right things to be called without error. |
||
| 19 | Elzar. |
||
| 20 | """ |
||
| 21 | def __init__(self, events=None, printer_handle=None, bot=None, say=None): |
||
| 22 | """ |
||
| 23 | This is called by load_modules for every .py in the modules directory. Your module must implement this, one way or the other, either directly, |
||
| 24 | by implementing this function signature, or indirectly, by inheriting from BaseModule. I suggest the latter. |
||
| 25 | """ |
||
| 26 | self.events = events |
||
| 27 | self.printer = printer_handle |
||
| 28 | self.interests = [] |
||
| 29 | self.bot = bot |
||
| 30 | self.say = say |
||
| 31 | |||
| 32 | # IMPORTANT: you must subscribe to events before you add your own below, or you'll subscribe twice. |
||
| 33 | # register ourself for any events that we're interested in that exist already |
||
| 34 | for event in events: |
||
| 35 | if event._type in self.interests: |
||
| 36 | event.subscribe(self) |
||
| 37 | |||
| 38 | self.help = None |
||
| 39 | |||
| 40 | self.post_init() |
||
| 41 | |||
| 42 | def handle(self, event): |
||
| 43 | pass |
||
| 44 | |||
| 45 | def post_init(self): |
||
| 46 | """ |
||
| 47 | Called after init is set up and builds out our basic module's needs. Allows you to do your own post-processing when inheriting from BaseModule. |
||
| 48 | """ |
||
| 49 | pass |
||
| 50 |