Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions VariableUpdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

class VariableUpdater:
"""
This class has a floating value called 'variable' that can be updated by a
This class has a value called 'variable' that can be updated by a
separate process. The class listens for updates to the variable and updates
it accordingly. Multiple instances of this class can be created to listen
for updates to different variables as long as they have different
subscription ports.
"""
def __init__(self, initial_value:float, sub_port:str="5551",
def __init__(self, initial_value, sub_port:str="5551",
update_interval:int=1):
"""
Initialize the VariableUpdater object with the initial value of the
variable and the subscription port to listen for updates.

Args:
initial_value (float): The initial value of the variable
initial_value: The initial value of the variable (can be any Python object)
sub_port (str, optional): The subscription port to listen for updates.
Defaults to "5551".
update_interval (int, optional): The interval at which to check for
Expand All @@ -44,9 +44,8 @@ def listen_for_updates(self):
try:
# Receive a Python object
message = self.subscriber.recv_pyobj(flags=zmq.NOBLOCK)
# Ensure it's a float
if isinstance(message, float):
self.variable = message
# Update the variable with the received object
self.variable = message
# If we don't receive a message, continue loop
except zmq.Again:
pass
Expand All @@ -57,6 +56,10 @@ def send_update(new_value, pub_port="5551"):
"""
Send an update to the variable to the subscriber listening on the specified
port.

Args:
new_value: The new value to send (can be any Python object)
pub_port (str, optional): The port to publish the update on. Defaults to "5551".
"""
# Create a ZeroMQ context to generate a publish socket
context = zmq.Context()
Expand Down
9 changes: 7 additions & 2 deletions test/example_VariableUpdater_receive.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import sys
import os
import time

from udpate_vars import VariableUpdater
# Add the parent folder to the Python path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from VariableUpdater import VariableUpdater

# Create a VariableUpdater object with an initial value of 0.0
updater = VariableUpdater(initial_value=0.0)
Expand All @@ -9,4 +14,4 @@
while True:
print(f"Current variable value: {updater.variable}")
# Simulate work
time.sleep(0.3)
time.sleep(0.3)
33 changes: 28 additions & 5 deletions test/example_VariableUpdater_send.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
from udpate_vars import send_update
import sys
import os

# Prompt the user for a new value for the variable
new_value = float(input("Enter new value for the variable: "))
# Send the new value to the subscriber
send_update(new_value)
# Add the parent folder to the Python path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from VariableUpdater import send_update


def test_float():
# Prompt the user for a new value for the variable
new_value = float(input("Enter new value for the variable: "))
# Send the new value to the subscriber
send_update(new_value)

def test_dictionary():
dictionary = {
"key1": 4,
"key2": [1, 2, 3],
"key3": "test",
}
# Send the dictionary to the subscriber
send_update(dictionary)

if __name__ == "__main__":
# Test sending a float value
# test_float()
# Test sending a dictionary
test_dictionary()