itertools.ilen?

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Thu Aug 7 11:42:01 EDT 2003


On Thu, 07 Aug 2003 03:10:10 -0400, rumours say that Jeremy Fincher
<fincher.*@osu.edu> might have written:

>        objs = gc.get_objects()
>        classes = len([obj for obj in objs if inspect.isclass(obj)])
>        functions = len([obj for obj in objs if inspect.isroutine(obj)])
>        modules = len([obj for obj in objs if inspect.ismodule(obj)])
>        dicts = len([obj for obj in objs if type(obj) == types.DictType])
>        lists = len([obj for obj in objs if type(obj) == types.ListType])
>        tuples = len([obj for obj in objs if type(obj) == types.TupleType])

Another way to count objects:

# code start
import types, gc

type2key = {
    types.ClassType: "classes",
    types.FunctionType: "functions",
    types.MethodType: "functions",
    types.ModuleType: "modules",
    types.DictType: "dicts",
    types.ListType: "lists",
    types.TupleType: "tuples"
}

sums = {
    "classes": 0, "functions": 0, "modules": 0, "dicts": 0,
    "lists": 0, "tuples": 0
}

for obj in gc.get_objects():
    try:
        sums[type2key[type(obj)]] += 1
    except KeyError:
        pass
# code end

This code is intended to be <2.3 compatible.
-- 
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.




More information about the Python-list mailing list