Created
September 17, 2015 00:22
-
-
Save billstron/9e2efdfb65360e4ad527 to your computer and use it in GitHub Desktop.
Example of TRP with "fully formed objects" as provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, os | |
myPath = os.path.dirname(os.path.abspath(__file__)) | |
sys.path.insert(0, myPath + '/TranRunPy/') | |
import TranRunPy as trp | |
from TranRunPy.Provider import provide, deprive | |
import json | |
class MockHwh(): | |
cnt = 0 | |
hwh = None | |
def __init__(self, data): | |
self.data = data | |
self.id = MockHwh.cnt | |
MockHwh.cnt += 1 | |
print 'created MockHwh' | |
def getDeviceId(self): | |
return self.data[0]["deviceId"] | |
def getIds(self): | |
ids = [] | |
for item in self.data: | |
ids.append(item["deviceId"]) | |
return ids | |
def getCnt(self): | |
return MockHwh.cnt | |
@staticmethod | |
def get(data): | |
print "MockHwh.get()" | |
if MockHwh.hwh == None: | |
MockHwh.hwh = MockHwh(data) | |
return MockHwh.hwh | |
provide('geospring-model', MockHwh.get, args=[], kwargs={"data": [{"deviceId" : 10}, {"deviceId": 11}]}) | |
class Supervisor(trp.Task): | |
task_name = "supervisor" | |
inputs = [('hwh', 'geospring-model')] | |
cnt = 0 | |
def __init__(self, *args, **kwargs): | |
super(Supervisor, self).__init__(*args, **kwargs) | |
self.id = Supervisor.cnt | |
Supervisor.cnt += 1 | |
print 'created Supervisor' | |
def run_on_default(self): | |
hwh = self.input('hwh') | |
print "mock id = {}".format(hwh.getIds()) | |
print "device id = {}".format(hwh.getDeviceId()) | |
print "mock cnt = {}".format(hwh.getCnt()) | |
print "task id = {}".format(self.id) | |
def run_example(): | |
task_list = [] | |
for index in range(3): | |
if len(task_list) < 2: | |
task_list.append(trp.Task.create("supervisor")) | |
runner = trp.TaskRunner() | |
runner.run_tasks(task_list) | |
if __name__ == '__main__': | |
run_example() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment