A few minor optimizations to take advantage of itertools.

This commit is contained in:
Jeremy Fincher 2003-08-07 06:25:33 +00:00
parent 031c81e937
commit 6dded57ea2

View File

@ -34,6 +34,7 @@ Provides a multitude of fun, useless commands.
""" """
from baseplugin import * from baseplugin import *
from itertools import imap, ifilter
import os import os
import gc import gc
@ -62,7 +63,6 @@ import ircmsgs
import privmsgs import privmsgs
import callbacks import callbacks
class FunCommands(callbacks.Privmsg): class FunCommands(callbacks.Privmsg):
def __init__(self): def __init__(self):
callbacks.Privmsg.__init__(self) callbacks.Privmsg.__init__(self)
@ -473,20 +473,22 @@ class FunCommands(callbacks.Privmsg):
if len(stack) == 1: if len(stack) == 1:
irc.reply(msg, str(self._complexToString(complex(stack[0])))) irc.reply(msg, str(self._complexToString(complex(stack[0]))))
else: else:
s = ', '.join(map(self._complexToString, map(complex, stack))) s = ', '.join(imap(self._complexToString, imap(complex, stack)))
irc.reply(msg, 'Stack: [%s]' % s) irc.reply(msg, 'Stack: [%s]' % s)
def objects(self, irc, msg, args): def objects(self, irc, msg, args):
"""takes no arguments. """takes no arguments.
Returns the number and types of Python objects in memory.""" Returns the number and types of Python objects in memory."""
def istype(t):
return lambda x: isinstance(x, t)
objs = gc.get_objects() objs = gc.get_objects()
classes = len([obj for obj in objs if inspect.isclass(obj)]) classes = ilen(itertools.ifilter(inspect.isclass, objs))
functions = len([obj for obj in objs if inspect.isroutine(obj)]) functions = ilen(itertools.ifilter(inspect.isroutine, objs))
modules = len([obj for obj in objs if inspect.ismodule(obj)]) modules = ilen(itertools.ifilter(inspect.ismodule, objs))
dicts = len([obj for obj in objs if type(obj) == types.DictType]) dicts = ilen(itertools.ifilter(istype(dict), objs))
lists = len([obj for obj in objs if type(obj) == types.ListType]) lists = ilen(itertools.ifilter(istype(list), objs))
tuples = len([obj for obj in objs if type(obj) == types.TupleType]) tuples = ilen(itertools.ifilter(istype(tuple), objs))
response = 'I have %s objects: %s modules, %s classes, %s functions, '\ response = 'I have %s objects: %s modules, %s classes, %s functions, '\
'%s dictionaries, %s lists, and %s tuples (and a few other'\ '%s dictionaries, %s lists, and %s tuples (and a few other'\
' different types).' %\ ' different types).' %\