This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample2.py
70 lines (54 loc) · 1.65 KB
/
example2.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Counter:
def __init__(self, initial=0, step=1):
self._initial = initial
self._step = step
self._counter = initial
def increment(self):
self._counter += self._step
@property
def total(self):
return self._counter
@property
def step(self):
return self._step
class TwoWayCounter(Counter):
def __init__(self, initial=0, step=1):
super().__init__(initial, step)
def decrement(self):
self._counter -= self._step
class LimitedCounter(Counter):
def __init__(self, max, initial=0, step=1):
super().__init__(initial, step)
self._max = max
def increment(self):
if self.total < self._max:
super().increment()
@property
def get_max(self):
return self._max
class LimitedTwoWayCounter(TwoWayCounter, LimitedCounter):
def __init__(self, min, max, initial=0, step=1):
super().__init__(initial, step)
#super(TwoWayCounter, self) to access LimitedCounter
super(TwoWayCounter, self).__init__(max, initial, step)
self._min = min
def increment(self):
super(TwoWayCounter, self).increment()
def decrement(self):
if self.total > self._min:
super().decrement()
@property
def get_min(self):
return self._min
class Semaphore(LimitedTwoWayCounter):
def __init__(self, is_available=False):
initial = 1 if is_available else 0
super().__init__(0, 1, initial, 1)
c = LimitedTwoWayCounter(0, 10, 0, 1)
c.increment()
c.increment()
print(c.total)
c.increment()
print(c.total)
c.decrement()
print(c.total)