Skip to content

Instantly share code, notes, and snippets.

@ptone
Last active December 18, 2015 00:09
Show Gist options
  • Save ptone/5694491 to your computer and use it in GitHub Desktop.
Save ptone/5694491 to your computer and use it in GitHub Desktop.
I'm working on something that takes physical hardware and allows one to add additional behaviors to it. In the end, I'd like the API to present the modeled logical item as a single entity in code with all state flattened. If the logical objects that add behaviors to the physical devices were 1:1 with the physical devices, then it would make sens…
class PhysicalDevice(object):
def __init__(self, channel=0):
self.channel = channel
self.physical_state = False
class LogicalDeviceBase(object):
def __init__(self):
self.logical_state = False
class LogicalDevice(LogicalDeviceBase, PhysicalDevice):
"""
combined class
"""
#__init__(...) would call each
class LogicalDeviceAlt(LogicalDeviceBase):
def __init__(self, physical_device=None, channel=0):
if physical_device == None:
self.physical_device = PhysicalDevice(channel=channel)
else:
self.physical_device = physical_device
def __setattr__(self, name, value):
"""
Proxy to the physical device
"""
if hasattr(self, 'physical_device') and hasattr(self.physical_device, name):
setattr(self.physical_device, name, value)
else:
object.__setattr__(self, name, value)
def __getattr__(self, name):
return getattr(self.physical_device, name)
d1 = LogicalDevice()
d2 = LogicalDeviceAlt()
#Desired API on devices:
d1.physical_state = True
d2.physical_state = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment