-
Notifications
You must be signed in to change notification settings - Fork 1
/
counter.py
33 lines (25 loc) · 1023 Bytes
/
counter.py
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
# Copyright (c) Charl P. Botha, TU Delft.
# All rights reserved.
# See COPYRIGHT for details.
# I could have done this with just a module variable, but I found this
# Borg thingy too nice not to use. See:
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531
class CounterBorg:
"""Borg-pattern (similar to a Singleton) for maintaining a
monotonically increasing counter.
Instantiate this anywhere, and call get() to return and increment
the increasing counter. DeVIDE uses this to stamp modified and
execute times of modules.
"""
# we start with 1, so that client code can init to 0 and guarantee
# an initial invalid state. The counter is explicitly long, this
# gives us unlimited precision (up to your memory limits, YEAH!)
__shared_state = {'counter' : 1L}
def __init__(self):
self.__dict__ = self.__shared_state
def get(self):
c = self.counter
self.counter += 1
return c
def counter():
return CounterBorg().get()