diff options
Diffstat (limited to 'tcosmonitor/counter.py')
-rw-r--r-- | tcosmonitor/counter.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tcosmonitor/counter.py b/tcosmonitor/counter.py new file mode 100644 index 0000000..ca5b7ad --- /dev/null +++ b/tcosmonitor/counter.py @@ -0,0 +1,51 @@ +# -*- coding: UTF-8 -*- +# -*- Mode: Python; tab-width: 4 -*- +# +# Source taken from python-M2Crypto demos +# +# It is tempting to add an __int__ method to this class, but it's not +# a good idea. This class tries to gracefully handle integer +# overflow, and to hide this detail from both the programmer and the +# user. Note that the __str__ method can be relied on for printing out +# the value of a counter: +# +# >>> print 'Total Client: %s' % self.total_clients +# +# If you need to do arithmetic with the value, then use the 'as_long' +# method, the use of long arithmetic is a reminder that the counter +# will overflow. + +class counter: + "general-purpose counter" + + def __init__ (self, initial_value=0): + self.value = initial_value + + def increment (self, delta=1): + result = self.value + try: + self.value = self.value + delta + except OverflowError: + self.value = long(self.value) + delta + return result + + def decrement (self, delta=1): + result = self.value + try: + self.value = self.value - delta + except OverflowError: + self.value = long(self.value) - delta + return result + + def as_long (self): + return long(self.value) + + def __nonzero__ (self): + return self.value != 0 + + def __repr__ (self): + return '<counter value=%s at %x>' % (self.value, id(self)) + + def __str__ (self): + return str(long(self.value)) + #return str(long(self.value))[:-1] |