justingood/terribot — pull request #25 by syntivinds (2014-04-19 02:50:25+00:00)
Title: refactor/rewrite of magic.py

Good news: I've tried rewriting `magic.py` to be more modular and encapsulated, which should make it easier to maintain and modify in the future.

Each "thing" that the bot does is spun out into it's own class. This gives it it's own little namespace to work in and should prevent variable naming conflicts down the road.
Each is a subclass of a `Handler` base class. This makes it straightforward when the module is loaded to loop through local() and create instances of every such subclass. The `do_magic` function (formally known as just `do`, but I changed it to appease pylint) goes through the list of these instances and checks each to see if it wants to do anything. Adding new functionality to the bot should be as simple as subclassing `Handler` and implementing the `run` method. (As I write this I realise that "Feature" is a better noun to use than "Handler" and that "Spell" would jive with the module being called "magic". So watch for that to change down the road.)

I've tweaked the workings of things a bit. Every call to the Internet uses the `requests` module now (which apparently already includes json support), so I got rid of a few of the import statements. Lists of potential replies are present as triple-quoted blocks of newline-separated text, which should make editing them a little easier. Gone are the lists padded with blanks, instead I picked probabilities and use random numbers. I tried to augment the Urban Dictionary code so it will prefer shorter definitions. I touched a lot of the regular expressions too. I added docstrings to label everything.

Since I created my fork, Jeff added some new code which I've incorporated into my copy. I've also added a new feature for handling locations shared to the group that calculates the distance from a fixed point.

**Bad News**: I only have a shitty, old windows laptop with cygwin. This virtualenv/telegram-client brouhaha that I'd need to actually run this code is a mite intimidating, so I haven't actually tried to run this new code. It might work. It might start World War Three. Your guess is as good as mine.
I have been running pylint though, so at least there aren't any really obvious syntax errors.