Communications Reference
kevinbotlib.comm
kevinbotlib.comm.BaseSendable
Bases: BaseModel
, ABC
The base for all of KevinbotLib's sendables.
What is a sendable?
A sendable is a basic unit of data that can be transported through the RedisCommClient
and server
data_id = 'kevinbotlib.dtype.null'
class-attribute
instance-attribute
Internally used to differentiate sendable types
struct = {}
class-attribute
instance-attribute
Data structure suggestion for use in dashboard applications
get_dict()
Return the sendable in dictionary form
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The sendable data |
kevinbotlib.comm.SendableGenerator
Bases: ABC
Abstract class for a function capable of being sent over RedisCommClient
generate_sendable()
abstractmethod
Abstract method to generate a sendable
Returns:
Name | Type | Description |
---|---|---|
BaseSendable |
BaseSendable
|
The returned sendable |
kevinbotlib.comm.IntegerSendable
Bases: BaseSendable
value
instance-attribute
Value to send
data_id = 'kevinbotlib.dtype.int'
class-attribute
instance-attribute
Internally used to differentiate sendable types
struct = {'dashboard': [{'element': 'value', 'format': 'raw'}]}
class-attribute
instance-attribute
Data structure suggestion for use in dashboard applications
get_dict()
Return the sendable in dictionary form
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The sendable data |
kevinbotlib.comm.BooleanSendable
Bases: BaseSendable
value
instance-attribute
Value to send
data_id = 'kevinbotlib.dtype.bool'
class-attribute
instance-attribute
Internally used to differentiate sendable types
struct = {'dashboard': [{'element': 'value', 'format': 'raw'}]}
class-attribute
instance-attribute
Data structure suggestion for use in dashboard applications
get_dict()
Return the sendable in dictionary form
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The sendable data |
kevinbotlib.comm.StringSendable
Bases: BaseSendable
value
instance-attribute
Value to send
data_id = 'kevinbotlib.dtype.str'
class-attribute
instance-attribute
Internally used to differentiate sendable types
struct = {'dashboard': [{'element': 'value', 'format': 'raw'}]}
class-attribute
instance-attribute
Data structure suggestion for use in dashboard applications
get_dict()
Return the sendable in dictionary form
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The sendable data |
kevinbotlib.comm.FloatSendable
Bases: BaseSendable
value
instance-attribute
Value to send
data_id = 'kevinbotlib.dtype.float'
class-attribute
instance-attribute
Internally used to differentiate sendable types
struct = {'dashboard': [{'element': 'value', 'format': 'raw'}]}
class-attribute
instance-attribute
Data structure suggestion for use in dashboard applications
get_dict()
Return the sendable in dictionary form
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The sendable data |
kevinbotlib.comm.AnyListSendable
Bases: BaseSendable
value
instance-attribute
Value to send
data_id = 'kevinbotlib.dtype.list.any'
class-attribute
instance-attribute
Internally used to differentiate sendable types
struct = {'dashboard': [{'element': 'value', 'format': 'raw'}]}
class-attribute
instance-attribute
Data structure suggestion for use in dashboard applications
get_dict()
Return the sendable in dictionary form
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The sendable data |
kevinbotlib.comm.DictSendable
Bases: BaseSendable
value
instance-attribute
Value to send
data_id = 'kevinbotlib.dtype.dict'
class-attribute
instance-attribute
Internally used to differentiate sendable types
struct = {'dashboard': [{'element': 'value', 'format': 'raw'}]}
class-attribute
instance-attribute
Data structure suggestion for use in dashboard applications
get_dict()
Return the sendable in dictionary form
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The sendable data |
kevinbotlib.comm.BinarySendable
Bases: BaseSendable
value
instance-attribute
Value to send
data_id = 'kevinbotlib.dtype.bin'
class-attribute
instance-attribute
Internally used to differentiate sendable types
struct = {'dashboard': [{'element': 'value', 'format': 'limit:1024'}]}
class-attribute
instance-attribute
Data structure suggestion for use in dashboard applications
get_dict()
Return the sendable in dictionary form
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The sendable data |
kevinbotlib.comm.RedisCommClient
__init__(host='localhost', port=6379, db=0, timeout=2, on_connect=None, on_disconnect=None)
Initialize a Redis Communication Client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
host
|
str
|
Host of the Redis server. |
'localhost'
|
port
|
int
|
Port of the Redis server. |
6379
|
db
|
int
|
Database number to use. |
0
|
timeout
|
float
|
Socket timeout in seconds. |
2
|
on_connect
|
Callable[[], None] | None
|
Connection callback. |
None
|
on_disconnect
|
Callable[[], None] | None
|
Disconnection callback. |
None
|
register_type(data_type)
Register a custom sendable type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_type
|
type[BaseSendable]
|
Sendable type to register. |
required |
add_hook(key, data_type, callback)
Add a callback to be triggered when sendable of data_type is set for a key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
CommPath | str
|
Key to listen to. |
required |
data_type
|
type[T]
|
Sendable type to listen for. |
required |
callback
|
Callable[[str, T | None], None]
|
Callback to trigger. |
required |
get(key, data_type)
Retrieve and deserialize sendable by key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
CommPath | str
|
Key to retrieve. |
required |
data_type
|
type[T]
|
Sendable type to deserialize to. |
required |
Returns:
Type | Description |
---|---|
T | None
|
Sendable or None if not found. |
get_keys()
Gets all keys in the Redis database.
Returns:
Type | Description |
---|---|
list[str]
|
List of keys. |
get_raw(key)
Retrieve the raw JSON for a key, ignoring the sendable deserialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
CommPath | str
|
Key to retrieve. |
required |
Returns:
Type | Description |
---|---|
dict | None
|
Raw JSON value or None if not found. |
get_all_raw()
Retrieve all raw JSON values as a dictionary of a key to raw value. May have slow performance.
Returns:
Type | Description |
---|---|
dict[str, dict] | None
|
Dictionary of a key to raw value or None if not found. |
set(key, sendable)
Set sendable in the Redis database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
CommPath | str
|
Key to set |
required |
sendable
|
BaseSendable | SendableGenerator
|
Sendable to set |
required |
publish(key, sendable)
Publish sendable in the Redis Pub/Sub client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
CommPath | str
|
Key to publish to |
required |
sendable
|
BaseSendable | SendableGenerator
|
Sendable to publish |
required |
subscribe(key, data_type, callback)
Subscribe to a Pub/Sub key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
CommPath | str
|
Key to subscribe to. |
required |
data_type
|
type[T]
|
Sendable type to deserialize to. |
required |
callback
|
Callable[[str, T], None]
|
Callback when data is received. |
required |
wipeall()
Delete all keys in the Redis database.
delete(key)
Delete a key from the Redis database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
CommPath | str
|
Key to delete. |
required |
connect()
Connect to the Redis server.
is_connected()
Check if the Redis connection is established.
Returns:
Type | Description |
---|---|
bool
|
Is the connection established? |
get_latency()
Measure the round-trip latency to the Redis server in milliseconds.
Returns:
Type | Description |
---|---|
float | None
|
Latency in milliseconds or None if not connected. |
wait_until_connected(timeout=5.0)
Wait until the Redis connection is established.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
Timeout in seconds. Defaults to 5.0 seconds. |
5.0
|
close()
Close the Redis connection and stop the pubsub thread.
reset_connection()
Reset the connection to the Redis server