示例#1
0
class Product:
    def __init__(self):
        self.name = ''
        self.afterResetEvent = Event()
        self.parent = None

    def setName(self, v):
        if v == self.name:
            return

        self.name = v

    def setParent(self, product):
        self.parent = product

    def reset(self):
        self.setName('')
        self.afterResetEvent.fire()

    @staticmethod
    def cfgr(builder):
        """
    by default, the Runtime.add_type(<Type>) method will look for a static cfgr method.
    """
        builder.addInput('name').string(lambda v, obj: obj.setName(v))
        #builder.addInput('reset').apply(lambda inp,obj: inp.event.subscribe(obj.reset))
        builder.addInput('reset').signal_to_method(lambda obj: obj.reset)
        builder.addOutput('afterReset').from_event(
            lambda obj: obj.afterResetEvent)

        builder.addInput('parent').object(lambda v, obj: obj.setParent(v))
示例#2
0
文件: app.py 项目: markkorput/pycfgr
class App:
  """
  The cfgr.app's default app component
  """
  @staticmethod
  def cfgr(builder):
    # inputs
    builder.addInput('stop').signal_to_method(lambda obj: obj.stop)
    builder.addInput('sleep').float_to_method(lambda obj: obj.setSleep)
    # outputs
    builder.addOutput('started').from_event(lambda obj: obj.startedEvent)
    builder.addOutput('update').from_event(lambda obj: obj.updateEvent)
    builder.addOutput('pid').from_event(lambda obj: obj.pidEvent)
    builder.addOutput('stopped').from_event(lambda obj: obj.stoppedEvent)

  def __init__(self):
    self.startedEvent = Event()
    self.pidEvent = Event()
    self.updateEvent = Event()
    
    self.sleeptime = 0.001
    self.isActive = False

    self.stoppedEvent = Event()

  def start(self):
    pid = os.getpid()
    self.pidEvent(os.getpid())
    self.isActive = True
    self.startedEvent.fire()

  def update(self):
    self.updateEvent()
    # print('update')
    time.sleep(self.sleeptime)

  def setSleep(self, val):
    # print('setsleep: {}'.format(val))
    self.sleeptime = val

  def stop(self, *args, **kwargs):
    self.isActive = False
    self.stoppedEvent()