本文整理汇总了Python中observable.Observable.on方法的典型用法代码示例。如果您正苦于以下问题:Python Observable.on方法的具体用法?Python Observable.on怎么用?Python Observable.on使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类observable.Observable
的用法示例。
在下文中一共展示了Observable.on方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_on
# 需要导入模块: from observable import Observable [as 别名]
# 或者: from observable.Observable import on [as 别名]
def test_on():
"""test event registering with the on method"""
obs = Observable()
def on_test():
pass
obs.on("on_test", on_test)
assert on_test in obs._events["on_test"]
示例2: test_is_registered
# 需要导入模块: from observable import Observable [as 别名]
# 或者: from observable.Observable import on [as 别名]
def test_is_registered():
"""test is_registered() after registering an event"""
obs = Observable()
def some_test():
pass
assert not obs.is_registered("some_event", some_test)
obs.on("some_event", some_test)
assert obs.is_registered("some_event", some_test)
示例3: test_on_multiple_handlers
# 需要导入模块: from observable import Observable [as 别名]
# 或者: from observable.Observable import on [as 别名]
def test_on_multiple_handlers():
"""test event registering with the on method and multiple handlers"""
obs = Observable()
results = []
def some_test(*args, **kw):
results.append(1)
def some_test_2(*args, **kw):
results.append(2)
obs.on("some_test", some_test, some_test_2)
assert len(obs._events["some_test"]) == 2
obs.trigger("some_test")
assert results == [1, 2]
示例4: test_get_handlers
# 需要导入模块: from observable import Observable [as 别名]
# 或者: from observable.Observable import on [as 别名]
def test_get_handlers():
"""test get_handlers() after registering handlers for two events"""
obs = Observable()
def some_test():
pass
def other_test():
pass
assert not obs.get_handlers("some_event")
obs.on("some_event", some_test)
assert some_test in obs.get_handlers("some_event")
obs.on("other_event", other_test)
assert other_test in obs.get_handlers("other_event")
assert other_test not in obs.get_handlers("some_event")
示例5: __init__
# 需要导入模块: from observable import Observable [as 别名]
# 或者: from observable.Observable import on [as 别名]
class PopPi:
# Main application for pop machine
# State information
__acceptingFob = False
__acceptingFunds = False
__creditOnlyMode = False
__lastCreditButtonState = 0
__memberFob = None
__memberCredit = None
__deposited = None
__popCost = None
def __init__(self):
self.__obs = Observable()
conf = Configuration()
self.__rfid = RfidReader(conf.rfid.serialPort)
self.__acceptor = CoinAcceptor(conf.coinAcceptor.serialPort, conf.coinAcceptor.inhibitPin, conf.coinMap)
self.__memberService = MemberService(conf.db.username, conf.db.password, conf.db.hostname, conf.db.port, conf.db.database)
self.__popMachine = PopMachine(conf.popMachine.relayPin)
self.__webService = WebService(conf.web.bindAddress, conf.web.port)
self.__acceptor.inhibit(True)
self.__popCost = self.__popMachine.getPopCost()
self.__obs.on("CardSwiped", self.__onCardRead)
self.__obs.on("CoinAccepted", self.__onCoinAccepted)
self.__obs.on("MemberNotFound", self.__onMemberNotFound)
self.__obs.on("PopPaid", self.__onPopPaid)
self.__obs.on("CreditCompleted", self.__onCreditCompleted)
self.__creditOnlyPin = conf.general.creditOnlyPin
GPIO.setup(self.__creditOnlyPin, GPIO.IN)
def start(self):
self.__fobThread = threading.Thread(target=self.__readCardThread)
self.__fobThread.daemon = True
self.__fobThread.start()
self.__coinThread = threading.Thread(target=self.__readCoinThread)
self.__coinThread.daemon = True
self.__coinThread.start()
self.__creditOnlyThread = threading.Thread(target=self.__readCreditOnlyButton)
self.__creditOnlyThread.daemon = True
self.__creditOnlyThread.start()
self.__resetStates()
self.__acceptingFob = True
def stop(self):
if not self.__rfid is None:
self.__rfid.shutdown()
if not self.__acceptor is None:
self.__acceptor.shutdown()
if not self.__memberService is None:
self.__memberService.shutdown()
if not self.__popMachine is None:
self.__popMachine.shutdown()
if not self.__webService is None:
self.__webService.shutdown()
def __onCardRead(self, card):
if not self.__acceptingFob:
print("Fob swiped while not accepting fobs: Ignoring fob " + str(card))
return
self.__acceptingFob = False
self.__webService.onSwipeStart()
print("Fob " + str(card) + " swiped. Finding member...")
self.__memberCredit = self.__memberService.getCredit(card)
if self.__memberCredit is None:
self.__obs.trigger("MemberNotFound", card)
return
self.__memberFob = card
print("Member " + str(card) + " has " + str(self.__memberCredit) + " cents credit")
self.__acceptingFunds = True
self.__deposited = 0
self.__acceptor.inhibit(False)
self.__webService.onSwipe(self.__memberCredit, self.__popCost)
requiredFunds = self.__getRequiredFunds()
print("Member " + str(card) + " needs to supply " + str(requiredFunds) + " cents")
self.__checkRequiredFunds()
def __onCoinAccepted(self, coin):
if not self.__acceptingFunds:
print("Coin accepted while not accepting coins: Ignoring " + str(coin) + " cents")
return
self.__deposited += coin
self.__webService.onDeposit(coin)
print("Member inserted " + str(coin) + " cents")
self.__checkRequiredFunds()
def __onPopPaid(self):
self.__acceptor.inhibit(True)
self.__acceptingFunds = False
newCredit = (self.__memberCredit + self.__deposited) - self.__popCost
self.__memberService.setCredit(self.__memberFob, newCredit)
print("Member's new credit is " + str(newCredit) + " cents")
self.__webService.onPaid(newCredit)
self.__popMachine.awardPop()
print("Pop awarded to member")
self.__resetStates()
def __onCreditCompleted(self):
self.__acceptor.inhibit(True)
self.__acceptingFunds = False
#.........这里部分代码省略.........