So I’m starting to write a simple procedural Python IRC bot from scratch (i.e. raw sockets) and I’m trying to work out the best way of designing it.
Usually I have a big ol’ while() loop that’ll sit there and push data received from the socket into a buffer and I’ll use a massive if/else statement to scan through the string (using regular expressions) to work out what to do with it. I have a feeling that I shouldn’t be doing that because it feels awful.
I decided to make a dictionary of regexes and their associated meanings, e.g.
regexes = {"^PING: (.+)": "incomming_ping",
"more regex": "more meanings"}
and just use a for/in loop to search through the text and find out which regex matches it. I’ve gotten this far, and I the first thing I thought was “okay, i can just make each ‘procedure’ to be called when a specific regex matches into a function, and call the appropriate function based on the meaning. I’m either stuck with using a massive if/else statement, which i didn’t want to do in the first place, or I could use some sort of Pythonic ‘eval’, which immediately sets off alarm bells.
Either way I’m screwed, and I can’t think of a way to approach this without going fully OOP (I don’t plan on doing this at the moment, don’t ask why).
Any ideas?
Instead of strings, use references to functions.
ps. If you’re “serious” about the IRC bot thing, you might want to have a look at Twisted.