Skip to content

Instantly share code, notes, and snippets.

@AndiH
Last active September 25, 2018 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndiH/ba697e888fb4d92f37b31983949fe17c to your computer and use it in GitHub Desktop.
Save AndiH/ba697e888fb4d92f37b31983949fe17c to your computer and use it in GitHub Desktop.
Python Simple Counter (decorator; explicit function call)
# import inspect
from __future__ import print_function
class Counter(object):
"""Simple counter, counting an invocation my means of a string (explicit call) or function name (decorator)"""
counters = {}
def __init__(self, *args, **kwargs):
"""Construct class. First, check if class is called via a decorator or directly. If true, defer incrementing counter until wrapped function is actually called (see __call__). If false, increment counter now."""
if callable(args[0]):
self.func = args[0]
self.name = args[0].__name__
else:
name = args[0]
self.potentiallyIncrementByName(name)
def __call__(self, *args, **kwargs):
"""Function for when Counter is used as a decorator. In that case, this class method call with increment the counter."""
self.potentiallyIncrementByName(self.name)
return self.func(*args, **kwargs)
@classmethod
def potentiallyIncrementByName(self, name):
"""Either the Counter is incremented (if it exists already) or created"""
# frame = inspect.getframeinfo(inspect.stack()[2][0])
if name in Counter.counters:
Counter.counters[name] += 1
else:
Counter.counters[name] = 1
# print(frame.function)
# print(frame.lineno)
@classmethod
def print(self):
"""Convenience method to print counted methods."""
padding = len(max(Counter.counters.keys()))
for (name, count) in Counter.counters.items():
print("{name:>{pad}}: {count}".format(name=name, pad=padding, count=count))
@Counter
def testfunction():
return 1
def example():
Counter("Start")
Counter("Start")
Counter("End")
testfunction()
testfunction()
testfunction()
Counter.print()
if __name__ == '__main__':
example()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment