-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLK.py
46 lines (36 loc) · 1.21 KB
/
CLK.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
"""
The following classes are defined:
Clock
"""
class Clock:
"""Initialize a new clock with value 0. After initialization, the value of
the clock can be both accessed and mutated using clock.value.
Raises:
ValueError: If value assigned to clock is not 0 or 1.
"""
def __init__(self, value=0):
if ((value != 0) and (value != 1)):
raise ValueError(
"Clock value must be 0 or 1, received \"{0}\".".format(value))
self._value = value
self.connections = []
@property
def value(self):
return self._value
@value.setter
def value(self, value):
if ((value != 0) and (value != 1)):
raise ValueError(
"Clock value must be 0 or 1, received \"{0}\".".format(value))
if value != self._value:
self._value = value
for callback in self.connections:
callback(self._value)
def _bind_to(self, callback):
if callback not in self.connections:
self.connections.append(callback)
def __str__(self):
return str(self._value)
def __call__(self, value=None):
if value is not None:
self._value = value