#config data will be returned from the sql database #this will be managed by django config = [ {'ID':'CFL1', 'class':'Light', 'ontime':'09:00', 'offtime':'23:00'}, {'ID':'CFL2', 'class':'Light', 'ontime':'02:00', 'offtime':'20:00'} ] #this class will convert an item from the config into a Light object class Light(object): state = 0 def __init__(self, configItem): self.configItem = configItem def turnon(self): self.state = 1 def turnoff(self): self.state = 0 #this will take the config data and construct an object #from each device and add them all to a list def clusterDevices(config): cluster = [] for device in config: name = Light(device) cluster.append(name) return cluster alldev = clusterDevices(config) for devObj in alldev: if devObj.configItem['ontime'] == '09:00': devObj.turnon() print "Turning on %s" % devObj.configItem['ID'] if devObj.state == 1: print "%s light is on" % devObj.configItem['ID'] else: print "%s light is off" % devObj.configItem['ID'] print alldev[0].configItem['ontime']
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question