/usr/share/pyshared/grokcore/component/tests/subscriptions/ordered_multisubscriptions.py is in python-grokcore.component 2.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | """
>>> grok.testing.grok(__name__)
>>> cave = Cave('Tilburg cave')
>>> martijn = Mammoth('Martijn')
You can query a subscriptions using multiple components and sort them
using `grok.order` information:
>>> ordered_subscriptions = grok.queryOrderedMultiSubscriptions(
... (cave, martijn), IActivity)
>>> ordered_subscriptions
[<grokcore.component.tests.subscriptions.ordered_multisubscriptions.Cooking object at ...>,
<grokcore.component.tests.subscriptions.ordered_multisubscriptions.Gardening object at ...>,
<grokcore.component.tests.subscriptions.ordered_multisubscriptions.Cleaning object at ...>]
>>> _ = map(lambda a: a.do(), ordered_subscriptions)
Martijn is cooking in Tilburg cave!
Martijn is growing pumpkins in Tilburg cave!
Martijn is cleaning the Tilburg cave.
Or choose not to:
>>> subscriptions = grok.queryMultiSubscriptions(
... (cave, martijn), IActivity)
(still need to sort them on class name in order to have a working doctest)
>>> subscriptions = sorted(subscriptions, key=lambda s: s.__class__.__name__)
>>> subscriptions
[<grokcore.component.tests.subscriptions.ordered_multisubscriptions.Cleaning object at ...>,
<grokcore.component.tests.subscriptions.ordered_multisubscriptions.Cooking object at ...>,
<grokcore.component.tests.subscriptions.ordered_multisubscriptions.Gardening object at ...>]
>>> _ = map(lambda a: a.do(), subscriptions)
Martijn is cleaning the Tilburg cave.
Martijn is cooking in Tilburg cave!
Martijn is growing pumpkins in Tilburg cave!
"""
import grokcore.component as grok
from zope import interface
class Cave(grok.Context):
def __init__(self, name):
self.name = name
class Mammoth(grok.Context):
def __init__(self, name):
self.name = name
class IActivity(interface.Interface):
def do():
"""Do something.
"""
class DayTimeActivity(grok.MultiSubscription):
grok.provides(IActivity)
grok.adapts(Cave, Mammoth)
grok.baseclass()
def __init__(self, where, who):
self.where = where
self.who = who
def do(self):
print 'Doing nothing.'
class Cleaning(DayTimeActivity):
grok.order(99)
def do(self):
print '%s is cleaning the %s.' % (self.who.name, self.where.name)
class Cooking(DayTimeActivity):
grok.order(10)
def do(self):
print '%s is cooking in %s!' % (self.who.name, self.where.name)
class Gardening(DayTimeActivity):
grok.order(15)
def do(self):
print '%s is growing pumpkins in %s!' % (self.who.name, self.where.name)
|