ntprop 0.2.0__tar.gz → 0.2.2__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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ntprop
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: A way to create transparent NetworkTables variables
|
|
5
5
|
License: MIT
|
|
6
6
|
Author: Kellen Watt
|
|
@@ -55,3 +55,22 @@ need to use the `get` and `set` methods of the type. *NOTE: These methods are no
|
|
|
55
55
|
since that type already does the necessary calls to `get` and `set` behind the scenes and resolves them prior to
|
|
56
56
|
returning a value.*
|
|
57
57
|
|
|
58
|
+
### Bindings
|
|
59
|
+
It's possible to bind to changes to a property using the `NTProperty.bind` method, and providing a callback
|
|
60
|
+
compatible with `Callable[[Any], None]`. The callback will be called any time the value changes, both from the
|
|
61
|
+
local program and NetworkTables.
|
|
62
|
+
|
|
63
|
+
To bind a callback for a property in an `NTPropertyHost`, call the `NTPropertyHost.bind` method, passing the
|
|
64
|
+
name of the NT entry with the callback using `kwargs` syntax. If your entry name is incompatible with Python
|
|
65
|
+
name syntax, you can use a `dict` with the entry name as the key and the callback as the associated value.
|
|
66
|
+
Either argument method allows multiple key-value pairs, so you can bind multiple callbacks at once.
|
|
67
|
+
|
|
68
|
+
***Note:*** Be aware that local changes may cause the callback to be called multiple times. It is recommended to
|
|
69
|
+
only use idempotent (same every time) callbacks as well as not make them any more expensive to call than
|
|
70
|
+
strictly necessary.
|
|
71
|
+
|
|
72
|
+
***Warning:*** There is no guarantee of the callback being called in the same thread it was created it (e.g. the NT
|
|
73
|
+
listener that does background updates runs in a different non-Python thread), so any interactions with the outside
|
|
74
|
+
world must be thread safe. If you don't know what that means, you're probably fine, and if you do know what it means,
|
|
75
|
+
but not how to fix it, visit [the Python docs on the `threading` module](https://docs.python.org/3/library/threading.html).
|
|
76
|
+
|
|
@@ -39,3 +39,22 @@ It's also possible to use an NTProperty without it being a member of ah `NTPrope
|
|
|
39
39
|
need to use the `get` and `set` methods of the type. *NOTE: These methods are not available in `NTPropertyHost`s
|
|
40
40
|
since that type already does the necessary calls to `get` and `set` behind the scenes and resolves them prior to
|
|
41
41
|
returning a value.*
|
|
42
|
+
|
|
43
|
+
### Bindings
|
|
44
|
+
It's possible to bind to changes to a property using the `NTProperty.bind` method, and providing a callback
|
|
45
|
+
compatible with `Callable[[Any], None]`. The callback will be called any time the value changes, both from the
|
|
46
|
+
local program and NetworkTables.
|
|
47
|
+
|
|
48
|
+
To bind a callback for a property in an `NTPropertyHost`, call the `NTPropertyHost.bind` method, passing the
|
|
49
|
+
name of the NT entry with the callback using `kwargs` syntax. If your entry name is incompatible with Python
|
|
50
|
+
name syntax, you can use a `dict` with the entry name as the key and the callback as the associated value.
|
|
51
|
+
Either argument method allows multiple key-value pairs, so you can bind multiple callbacks at once.
|
|
52
|
+
|
|
53
|
+
***Note:*** Be aware that local changes may cause the callback to be called multiple times. It is recommended to
|
|
54
|
+
only use idempotent (same every time) callbacks as well as not make them any more expensive to call than
|
|
55
|
+
strictly necessary.
|
|
56
|
+
|
|
57
|
+
***Warning:*** There is no guarantee of the callback being called in the same thread it was created it (e.g. the NT
|
|
58
|
+
listener that does background updates runs in a different non-Python thread), so any interactions with the outside
|
|
59
|
+
world must be thread safe. If you don't know what that means, you're probably fine, and if you do know what it means,
|
|
60
|
+
but not how to fix it, visit [the Python docs on the `threading` module](https://docs.python.org/3/library/threading.html).
|
|
@@ -44,7 +44,8 @@ class NTProperty:
|
|
|
44
44
|
def cached(self) -> Any:
|
|
45
45
|
return self._cached
|
|
46
46
|
|
|
47
|
-
@cached.setter
|
|
47
|
+
@cached.setter
|
|
48
|
+
def cached(self, value: Any):
|
|
48
49
|
for binding in self.bindings:
|
|
49
50
|
binding(value)
|
|
50
51
|
self._cached = value
|
|
@@ -86,7 +87,7 @@ class NTPropertyHost:
|
|
|
86
87
|
|
|
87
88
|
|
|
88
89
|
|
|
89
|
-
class NumberProperty(NTProperty):
|
|
90
|
+
class NumberProperty(NTProperty, ty="Double"):
|
|
90
91
|
def __init__(self, name: str, default: float | int = 0.0, readonly = False, force_publish: bool = True):
|
|
91
92
|
super().__init__(name, default, readonly, force_publish)
|
|
92
93
|
|
|
@@ -94,13 +95,13 @@ class NumberProperty(NTProperty):
|
|
|
94
95
|
return self.cached
|
|
95
96
|
|
|
96
97
|
def set(self, value: float | int):
|
|
97
|
-
if value is not float and value is not int:
|
|
98
|
+
if type(value) is not float and type(value) is not int:
|
|
98
99
|
raise ValueError("Can not assign non-numeric to NumberProperty")
|
|
99
100
|
self.cached = value
|
|
100
101
|
self.pub.set(value)
|
|
101
102
|
|
|
102
103
|
|
|
103
|
-
class BooleanProperty(NTProperty):
|
|
104
|
+
class BooleanProperty(NTProperty, ty="Boolean"):
|
|
104
105
|
def __init__(self, name: str, default: bool = False, readonly = False, strict: bool = False, force_publish: bool = True):
|
|
105
106
|
super().__init__(name, default, readonly, force_publish)
|
|
106
107
|
self.strict = strict
|
|
@@ -109,7 +110,7 @@ class BooleanProperty(NTProperty):
|
|
|
109
110
|
return self.cached
|
|
110
111
|
|
|
111
112
|
def set(self, value: bool):
|
|
112
|
-
if self.strict and value is not bool:
|
|
113
|
+
if self.strict and type(value) is not bool:
|
|
113
114
|
raise ValueError("Can not assign non-boolean to BooleanProperty in strict mode")
|
|
114
115
|
|
|
115
116
|
value = bool(value)
|
|
@@ -117,7 +118,7 @@ class BooleanProperty(NTProperty):
|
|
|
117
118
|
self.pub.set(value)
|
|
118
119
|
|
|
119
120
|
|
|
120
|
-
class StringProperty(NTProperty):
|
|
121
|
+
class StringProperty(NTProperty, ty="String"):
|
|
121
122
|
def __init__(self, name: str, default: str = "", readonly = False, strict: bool = False, force_publish: bool = True):
|
|
122
123
|
super().__init__(name, default, readonly, force_publish)
|
|
123
124
|
self.strict = strict
|
|
@@ -126,7 +127,7 @@ class StringProperty(NTProperty):
|
|
|
126
127
|
return self.cached
|
|
127
128
|
|
|
128
129
|
def set(self, value: str):
|
|
129
|
-
if self.strict and value is not str:
|
|
130
|
+
if self.strict and type(value) is not str:
|
|
130
131
|
raise ValueError("Can not assign non-string to StringProperty in strict mode")
|
|
131
132
|
|
|
132
133
|
value = str(value)
|
|
File without changes
|