Went over the Command class' basic methods, cleaning up and optimizing while still trying to keep things clean, such as using cleverer loops and try...except instead of if statements. Also cleaned up the way attributes are retrieved. Results in roughly a halving of the time that the code spends in the command.py module.

This commit is contained in:
Griatch 2012-02-06 00:59:41 +01:00
parent 7b2a4e4467
commit d36a79b2cc
3 changed files with 27 additions and 46 deletions

View file

@ -105,9 +105,10 @@ class Command(object):
key and aliases.
input can be either a cmd object or the name of a command.
"""
if type(cmd) != self:
try:
return self.match(cmd.key)
except AttributeError: # got a string
return self.match(cmd)
return self.match(cmd.key)
def __contains__(self, query):
"""
@ -117,10 +118,11 @@ class Command(object):
input can be either a command object or a command name.
"""
if type(query) == type(Command()):
try:
query = query.key
return (query in self.key) or \
(sum([query in alias for alias in self.aliases]) > 0)
except AttributeError: # we got a string
pass
return (query in self.key) or any(query in alias for alias in self.aliases)
def match(self, cmdname):
"""