ntprop 0.2.3__tar.gz → 0.2.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -17,7 +17,8 @@ class NTProperty:
|
|
|
17
17
|
self.bindings = []
|
|
18
18
|
self.path = name
|
|
19
19
|
self.default = default
|
|
20
|
-
self.
|
|
20
|
+
self._update(self.default, remote=False)
|
|
21
|
+
# self.cached = self.default
|
|
21
22
|
topic = getattr(NTProperty.nt_instance, f"get{type(self).ty}Topic")(name)
|
|
22
23
|
self.sub = topic.subscribe(default)
|
|
23
24
|
if not readonly or force_publish:
|
|
@@ -30,7 +31,7 @@ class NTProperty:
|
|
|
30
31
|
|
|
31
32
|
def update(evt: ntcore.Event):
|
|
32
33
|
value = getattr(evt.data.value, f"get{type(self).ty}")()
|
|
33
|
-
self.
|
|
34
|
+
self._update(value, remote=True)
|
|
34
35
|
|
|
35
36
|
self.value_listener = NTProperty.nt_instance.addListener(self.sub, ntcore.EventFlags.kValueAll, update)
|
|
36
37
|
|
|
@@ -43,21 +44,27 @@ class NTProperty:
|
|
|
43
44
|
if self.pub is not None:
|
|
44
45
|
self.pub.close()
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
def cached(self) -> Any:
|
|
48
|
-
return self._cached
|
|
49
|
-
|
|
50
|
-
@cached.setter
|
|
51
|
-
def cached(self, value: Any):
|
|
52
|
-
for binding in self.bindings:
|
|
53
|
-
binding(value)
|
|
47
|
+
def _update(self, value: Any, remote: bool=False):
|
|
54
48
|
self._cached = value
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
for binding in self.bindings:
|
|
50
|
+
binding(value, remote)
|
|
51
|
+
# @property
|
|
52
|
+
# def cached(self) -> Any:
|
|
53
|
+
# return self._cached
|
|
54
|
+
#
|
|
55
|
+
# @cached.setter
|
|
56
|
+
# def cached(self, value: Any):
|
|
57
|
+
# for binding in self.bindings:
|
|
58
|
+
# binding(value)
|
|
59
|
+
# self._cached = value
|
|
60
|
+
|
|
61
|
+
def bind(self, callback: Callable[[Any, bool], None]):
|
|
57
62
|
self.bindings.append(callback)
|
|
58
63
|
|
|
59
64
|
def get(self) -> Any:
|
|
60
|
-
|
|
65
|
+
"""Returns the current value of the property. The base implementation is sufficient for basic behaviour, however, the
|
|
66
|
+
type signature returns an `Any`. Override the method and return `self._cached` if this is undesirable"""
|
|
67
|
+
return self._cached
|
|
61
68
|
|
|
62
69
|
def set(self, value: Any):
|
|
63
70
|
raise NotImplementedError()
|
|
@@ -81,7 +88,7 @@ class NTPropertyHost:
|
|
|
81
88
|
return super().__getattribute__(name).get()
|
|
82
89
|
return super().__getattribute__(name)
|
|
83
90
|
|
|
84
|
-
def bind(self, **kwargs: Callable[[Any], None]):
|
|
91
|
+
def bind(self, **kwargs: Callable[[Any, bool], None]):
|
|
85
92
|
if not hasattr(self, "_ntdict"):
|
|
86
93
|
return
|
|
87
94
|
for name, callback in kwargs.items():
|
|
@@ -95,12 +102,12 @@ class NumberProperty(NTProperty, ty="Double"):
|
|
|
95
102
|
super().__init__(name, default, readonly, force_publish)
|
|
96
103
|
|
|
97
104
|
def get(self) -> float:
|
|
98
|
-
return self.
|
|
105
|
+
return self._cached
|
|
99
106
|
|
|
100
107
|
def set(self, value: float | int):
|
|
101
108
|
if type(value) is not float and type(value) is not int:
|
|
102
109
|
raise ValueError("Can not assign non-numeric to NumberProperty")
|
|
103
|
-
self.
|
|
110
|
+
self._update(value)
|
|
104
111
|
self.pub.set(value)
|
|
105
112
|
|
|
106
113
|
|
|
@@ -110,14 +117,14 @@ class BooleanProperty(NTProperty, ty="Boolean"):
|
|
|
110
117
|
self.strict = strict
|
|
111
118
|
|
|
112
119
|
def get(self) -> bool:
|
|
113
|
-
return self.
|
|
120
|
+
return self._cached
|
|
114
121
|
|
|
115
122
|
def set(self, value: bool):
|
|
116
123
|
if self.strict and type(value) is not bool:
|
|
117
124
|
raise ValueError("Can not assign non-boolean to BooleanProperty in strict mode")
|
|
118
125
|
|
|
119
126
|
value = bool(value)
|
|
120
|
-
self.
|
|
127
|
+
self._update(value)
|
|
121
128
|
self.pub.set(value)
|
|
122
129
|
|
|
123
130
|
|
|
@@ -127,12 +134,12 @@ class StringProperty(NTProperty, ty="String"):
|
|
|
127
134
|
self.strict = strict
|
|
128
135
|
|
|
129
136
|
def get(self) -> str:
|
|
130
|
-
return self.
|
|
137
|
+
return self._cached
|
|
131
138
|
|
|
132
139
|
def set(self, value: str):
|
|
133
140
|
if self.strict and type(value) is not str:
|
|
134
141
|
raise ValueError("Can not assign non-string to StringProperty in strict mode")
|
|
135
142
|
|
|
136
143
|
value = str(value)
|
|
137
|
-
self.
|
|
144
|
+
self._update(value)
|
|
138
145
|
self.pub.set(value)
|
|
File without changes
|
|
File without changes
|