itertools.ilen?
Duncan Booth
duncan at NOSPAMrcp.co.uk
Fri Aug 8 06:02:39 EDT 2003
Christos "TZOTZIOY" Georgiou <tzot at sil-tec.gr> wrote in
news:98s4jvkqv94c0gj99epjanq9b3aqvq8i2q at 4ax.com:
> 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
>
I'm just curious, why did you decide to map the types to strings instead of
just using the types themselves?
e.g.
>>> import gc
>>> sums = {}
>>> for obj in gc.get_objects():
if type(obj) not in sums:
sums[type(obj)] = 1
else:
sums[type(obj)] += 1
>>> for typ, count in sums.iteritems():
print typ.__name__, count
instance 525
tuple 4273
class 162
getset_descriptor 14
traceback 2
wrapper_descriptor 165
list 258
module 71
instance method 279
function 1222
weakref 18
dict 1647
method_descriptor 82
member_descriptor 75
frame 18
>>>
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list