Dec-07-2022, 03:37 PM
(Dec-06-2022, 08:57 PM)deanhystad Wrote: I'm not sure if I am following this completely, but it looks like classes could be useful. Classes work better for collecting related information than having a bunch of interrelated dictionaries. Classes also let you associate code with the data. Below I have a Device class and an Area class. When you set the status of an Area it sets the status of all the devices in the area. You could write other methods that check if all devices in an area have the same status. Get an area from a device and tell all the other devices in the area to be the same, etc...
class Area(): instances = {} @classmethod def get(cls, name): area = cls.instances.get(name, None) if area is None: area = Area(name) return area def __init__(self, name, status=0): self.name = name self.devices = {} self.status = status self.instances[name] = self def add_device(self, name, device): self.devices[name] = device def set_status(self, value): self.status = value for device in self.devices.values(): device.set_status(value) def __str__(self): return f'<Area: {self.name}, status:{self.status}>' class Device(): instances = {} def __init__(self, name, status, area): self.name = name self.status = 0 self.area = Area.get(area) self.area.add_device(name, self) self.instances[self.name] = self def set_status(self, value): self.status = value def __str__(self): return f'<Device: {self.name}, area:{self.area.name}, status:{self.status}>' device_ids = {'179,1': 'Dressing 2', '179,2': 'Dressing 2', '179,3': 'Dressing 1'} for device, area in device_ids.items(): Device(device, 0, area) print("\nDevices") for device in Device.instances.values(): print(device) Area.get('Dressing 2').set_status(1) print("\nDevices") for device in Device.instances.values(): print(device)
Output:Devices <Device: 179,1, area:Dressing 2, status:0> <Device: 179,2, area:Dressing 2, status:0> <Device: 179,3, area:Dressing 1, status:0> Devices <Device: 179,1, area:Dressing 2, status:1> <Device: 179,2, area:Dressing 2, status:1> <Device: 179,3, area:Dressing 1, status:0>
Thanks, I think you may be right. I will give this a shot.