def __init__(self, name, API): threading.Thread.__init__(self) self.name = name self.ipc = DynamicObject({}) self.ipc.extend(API) # make all functions accessible by default for key in vars(self.ipc): setattr(self, key, getattr(self.ipc, key)) # overwriting functions for convenience (and security) - abstracting the 'self' out def registerOutput(tag, defaults): self.ipc.registerOutput(self, tag, defaults) self.registerOutput = registerOutput def output(tag, value): return self.ipc.output(self, tag, value) self.output = output def message(msg): return self.ipc.message(self, msg) self.message = message
def registerOutput (owner, tag, default): with MsgLock: if (comms[tag] and comms[tag].owner != owner): return printSync("WARNING: Cannot register tag '{}'. Already registered by thread '{}'.".format(tag, comms[tag].owner.name)) default = DynamicObject(default) print("Registered '{}' tag with owner thread '{}' and default output '{}'.".format(tag, owner.name, default)) default.owner = owner comms[tag] = default
def registerOutput(owner, tag, default): with MsgLock: if (comms[tag] and comms[tag].owner != owner): return printSync( "WARNING: Cannot register tag '{}'. Already registered by thread '{}'." .format(tag, comms[tag].owner.name)) default = DynamicObject(default) print( "Registered '{}' tag with owner thread '{}' and default output '{}'." .format(tag, owner.name, default)) default.owner = owner comms[tag] = default
def __init__(self, name, API): threading.Thread.__init__(self) self.name = name self.ipc = DynamicObject({}) self.ipc.extend(API) # make all functions accessible by default for key in vars(self.ipc): setattr(self, key, getattr(self.ipc, key)) # overwriting functions for convenience (and security) - abstracting the 'self' out def registerOutput (tag, defaults): self.ipc.registerOutput(self, tag, defaults) self.registerOutput = registerOutput def output (tag, value): return self.ipc.output(self, tag, value) self.output = output def message (msg): return self.ipc.message(self, msg) self.message = message
#!/usr/bin/python # import from parent directory import sys import os.path sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) import DynamicObject DynamicObject = DynamicObject.Class # create object print("# create object:") obj = DynamicObject({"a": 5, "b": "something"}) print(obj) print("") # access with dot notation print("# access with dot notation") print(obj.a) print("") # update / create field with dot notation print("# update / create field with dot notation") obj.c = True print(obj) print("") # access like a dictionary / map print("# access like a dictionary / map") print(obj["a"])
import threading import time import copy import DynamicObject DynamicObject = DynamicObject.Class MsgLock = threading.RLock() IOLock = threading.RLock() # enable / disable messages logComms = False if (len(sys.argv) > 1 and sys.argv[1] == '-debug'): logComms = True threads = {} comms = DynamicObject({}) # load modules here import OpenCV OpenCV = OpenCV.Class import Servo Servo = Servo.Class import Audio Audio = Audio.Class def registerOutput(owner, tag, default): with MsgLock: if (comms[tag] and comms[tag].owner != owner): return printSync( "WARNING: Cannot register tag '{}'. Already registered by thread '{}'."
#!/usr/bin/python # import from parent directory import sys import os.path sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) import DynamicObject DynamicObject = DynamicObject.Class # create object print("# create object:") obj = DynamicObject({ "a": 5, "b": "something"}) print(obj) print("") # access with dot notation print("# access with dot notation") print(obj.a) print("") # update / create field with dot notation print("# update / create field with dot notation") obj.c = True print(obj) print("") # access like a dictionary / map print("# access like a dictionary / map") print(obj["a"]) print("")