Class counter in python

In my last project I need to count a lot of stuff during an importing program. I needed to have something simple to track each counter and to display the result at the end of the batch.

For this purpose I wrote a little class which is very simple and nice to use… this are my first steps in python…

Here the link to my public github repo

GitHUB

# 06.12.2017
# pierre koerber
# simple counter utility class with test

class Counter:

counter = {}

def __init__(self):
print "yeah"
def toString(self):
print self.counter
for key in self.counter:
print key + " = " + str(self.getCounter(key))
def increment(self,counter_name):
if counter_name in self.counter:
self.counter[counter_name] += 1
else:
self.counter[counter_name] = 1
def getCounter(self, counter_name):
if counter_name in self.counter:
return self.counter[counter_name]

print "start"

x = Counter()

x.increment("iteration")
x.increment("iteration")
x.increment("iteration")
x.increment("iteration")

x.increment("iterationx")
x.increment("iterationx")
x.increment("iterationx")
x.increment("iterationx")

x.toString()

print x.getCounter("iteration")

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*