Welcome, guest | Sign In | My Account | Store | Cart

This function creates a proxy for a class so that you can count how many times each method called. It only works for mutable types.

Python, 36 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def MakeProxy(obje):
	from collections import Counter
	class proxy(obje):
		def __init__(self,*args,**kwargs):
                        super(proxy,self).__init__(*args,**kwargs)
                        self.counter = Counter()
                def __getattribute__(self,attr):
                        counter = super(proxy,self).__getattribute__("counter")
                        if attr == "counter":
                            return counter
                        counter[attr] += 1
                        return super(proxy,self).__getattribute__(attr)
        return proxy
"""
>>> list_proxy = MakeProxy(list)
>>> a = list_proxy((1,2,3,4))
>>> a
[1, 2, 3, 4]
>>> a.extend([7,8,9])
>>> a
[1, 2, 3, 4, 7, 8, 9]
>>> a.counter["extend"]
1
>>> dict_proxy = MakeProxy(dict)
>>> b = dict_proxy({})
>>> b
{}
>>> b["osman"] = "arabaci"
>>> b
{'osman': 'arabaci'}
>>> b.keys()
['osman']
>>> b.counter["keys"]
1

"""