A way to create transparent NetworkTables variables
Project description
NTProp
A simple mechanism for using Network Tables entries as semi-transparent member variables
Installation
Using your Python package manager of choice, just install the ntprop package from PyPI.
pip install ntprop
Usage
To start using these, you just need to import NTPropertyHost from ntprop and have
your class inherit from it. From there, any member varaible that is of the types NumberProperty,
BooleanProperty, or StringProperty (also from ntprop) will act like a simple, strictly-typed
member variable that transparently updates and is updated by the relevant NT entry.
Example
from ntprop import NTPropertyHost, NumberProperty
class Foo(NTPropertyHost):
# for purposes of type-checking here, we could just call this a float, since there's no difference in
# usage at runtime. This is only valid if the owning class is an NTPropertyHost.
bar: NumberProperty
def __init__(self):
self.bar = NumberProperty("ntbar", default=1, readonly=False) #defaults are 0.0 and False, respectively
def do_something(self):
# += may work?
self.bar = self.bar + 1
print(self.bar)
It's also possible to use an NTProperty without it being a member of ah NTPropertyHost. To do that, you just
need to use the get and set methods of the type. NOTE: These methods are not available in NTPropertyHosts
since that type already does the necessary calls to get and set behind the scenes and resolves them prior to
returning a value.
Bindings
It's possible to bind to changes to a property using the NTProperty.bind method, and providing a callback
compatible with Callable[[Any], None]. The callback will be called any time the value changes, both from the
local program and NetworkTables.
To bind a callback for a property in an NTPropertyHost, call the NTPropertyHost.bind method, passing the
name of the NT entry with the callback using kwargs syntax. If your entry name is incompatible with Python
name syntax, you can use a dict with the entry name as the key and the callback as the associated value.
Either argument method allows multiple key-value pairs, so you can bind multiple callbacks at once.
Note: Be aware that local changes may cause the callback to be called multiple times. It is recommended to only use idempotent (same every time) callbacks as well as not make them any more expensive to call than strictly necessary.
Warning: There is no guarantee of the callback being called in the same thread it was created it (e.g. the NT
listener that does background updates runs in a different non-Python thread), so any interactions with the outside
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,
but not how to fix it, visit the Python docs on the threading module.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ntprop-0.2.3.tar.gz.
File metadata
- Download URL: ntprop-0.2.3.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.4 CPython/3.14.4 Darwin/23.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10e9b65622bca5a09e6373cc84213ec59c98060e9d07e2accfc9828c57c15316
|
|
| MD5 |
43568bd4fa92e2eda9b711d3646bc6ba
|
|
| BLAKE2b-256 |
3656eea6c32572d0b0bf40baa7bfb16a7c8b579dfaccc7d744529cc59f46aa1c
|
File details
Details for the file ntprop-0.2.3-py3-none-any.whl.
File metadata
- Download URL: ntprop-0.2.3-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.4 CPython/3.14.4 Darwin/23.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a725be841b8c633ac2e730fa4705287f3a4875a917049dc306c3442eb934d0b
|
|
| MD5 |
bb227f7a9ef9092be7eca33ca6bde822
|
|
| BLAKE2b-256 |
b876d182b5c5aff0713575a26e20a8773905b4c7b15bba64d775f1fd41eeb6c4
|