本文整理匯總了Python中xatro.world.World.receiverFor方法的典型用法代碼示例。如果您正苦於以下問題:Python World.receiverFor方法的具體用法?Python World.receiverFor怎麽用?Python World.receiverFor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類xatro.world.World
的用法示例。
在下文中一共展示了World.receiverFor方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_emit_receiveOnce
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiverFor [as 別名]
def test_emit_receiveOnce(self):
"""
Events should only be received by each object once.
"""
world = World(MagicMock())
obj1 = world.create('foo')['id']
obj2 = world.create('foo')['id']
obj1_received = []
world.receiveFor(obj1, obj1_received.append)
obj2_received = []
world.receiveFor(obj2, obj2_received.append)
# obj1 emits everything it receives
world.receiveFor(obj1, world.emitterFor(obj1))
# obj2 emits everything it receives
world.receiveFor(obj2, world.emitterFor(obj2))
# obj2 receives emissions from obj1
world.subscribeTo(obj1, world.receiverFor(obj2))
# obj1 receives emissions from obj2
world.subscribeTo(obj2, world.receiverFor(obj1))
# we have a nice loop set up. When this test fails, it is likely to
# continue spinning forever.
world.emit('echo', obj1)
self.assertEqual(obj1_received, ['echo'], "Should have received the "
"message once")
self.assertEqual(obj2_received, ['echo'], "Should have received the "
"message once")
示例2: test_receiverFor_same
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiverFor [as 別名]
def test_receiverFor_same(self):
"""
You should get the same function each time you ask for a receiver for
the same object.
"""
world = World(MagicMock())
self.assertEqual(world.receiverFor('foo'), world.receiverFor('foo'))
示例3: test_emit_oneEventAtATime
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiverFor [as 別名]
def test_emit_oneEventAtATime(self):
"""
Only one event should be emitted at a time. If an event emission
causes another event to be emitted, it should be appended to a queue
and be emitted after the current event is finished.
"""
world = World(MagicMock())
obj1 = world.create('foo')['id']
obj2 = world.create('foo')['id']
obj3 = world.create('foo')['id']
obj1_received = []
world.receiveFor(obj1, obj1_received.append)
obj2_received = []
world.receiveFor(obj2, obj2_received.append)
obj3_received = []
world.receiveFor(obj3, obj3_received.append)
# obj1 will emit to
# obj2 and obj3
world.subscribeTo(obj1, world.receiverFor(obj2))
world.subscribeTo(obj1, world.receiverFor(obj3))
# obj2 will emit to
# obj1 and obj3
world.subscribeTo(obj2, world.receiverFor(obj1))
world.subscribeTo(obj2, world.receiverFor(obj3))
# 1_
# /|\
# / \
# |/_ _\|
# 3 <---- 2
# obj2 will emit "obj2" every time he receives an event
def noisy(_):
world.emit('obj2', obj2)
world.receiveFor(obj2, noisy)
# If things are working properly, then obj3 will receive
# the 'obj2' emissions AFTER it receives the event from obj1
world.emit('obj1', obj1)
self.assertEqual(obj1_received, ['obj1', 'obj2'],
"obj1 should receive obj1 from self, then obj2")
self.assertEqual(obj2_received, ['obj1', 'obj2'],
"obj2 should receive obj1, then obj2 from self")
self.assertEqual(obj3_received, ['obj1', 'obj2'],
"obj3 should receiver obj1 first, then obj2")
示例4: test_destroy_disableSubscribers
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiverFor [as 別名]
def test_destroy_disableSubscribers(self):
"""
When an object is destroyed, things subscribed to its events will
no longer receive events.
"""
world = World(MagicMock())
thing = world.create('foo')
received = []
world.receiveFor(thing['id'], received.append)
emitted = []
world.subscribeTo(thing['id'], emitted.append)
receiver = world.receiverFor(thing['id'])
emitter = world.emitterFor(thing['id'])
world.destroy(thing['id'])
received.pop()
emitted.pop()
receiver('foo')
self.assertEqual(received, [])
self.assertEqual(emitted, [])
emitter('foo')
self.assertEqual(received, [])
self.assertEqual(emitted, [])
示例5: test_receiverFor
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiverFor [as 別名]
def test_receiverFor(self):
"""
You can get a function that will call eventReceived for a given object.
"""
world = World(MagicMock())
obj = world.create('foo')
called = []
world.receiveFor(obj['id'], called.append)
receiver = world.receiverFor(obj['id'])
receiver('hey')
self.assertEqual(called, ['hey'])