Skip to content

Commit 1ac91f3

Browse files
committed
Reformat spacing and update comments in doctests for clarity
1 parent e7ec019 commit 1ac91f3

File tree

6 files changed

+68
-7
lines changed

6 files changed

+68
-7
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pip install python-dispatch
2929

3030
```python
3131
>>> from pydispatch import Dispatcher
32+
3233
>>> class MyEmitter(Dispatcher):
3334
... # Events are defined in classes and subclasses with the '_events_' attribute
3435
... _events_ = ['on_state', 'new_data']
@@ -37,19 +38,22 @@ pip install python-dispatch
3738
... data = {'foo':'bar'}
3839
... # Then emit the change with optional positional and keyword arguments
3940
... self.emit('new_data', data=data)
40-
...
41+
4142
>>> # An observer - could inherit from Dispatcher or any other class
4243
>>> class MyListener(object):
4344
... def on_new_data(self, *args, **kwargs):
4445
... data = kwargs.get('data')
4546
... print('I got data: {}'.format(data))
4647
... def on_emitter_state(self, *args, **kwargs):
4748
... print('emitter state changed')
48-
...
49+
4950
>>> emitter = MyEmitter()
5051
>>> listener = MyListener()
52+
53+
>>> # Bind to the "on_state" and "new_data" events of emitter
5154
>>> emitter.bind(on_state=listener.on_emitter_state)
5255
>>> emitter.bind(new_data=listener.on_new_data)
56+
5357
>>> emitter.do_some_stuff()
5458
I got data: {'foo': 'bar'}
5559
>>> emitter.emit('on_state')
@@ -61,23 +65,30 @@ emitter state changed
6165

6266
```python
6367
>>> from pydispatch import Dispatcher, Property
68+
6469
>>> class MyEmitter(Dispatcher):
6570
... # Property objects are defined and named at the class level.
6671
... # They will become instance attributes that will emit events when their values change
6772
... name = Property()
6873
... value = Property()
69-
...
74+
7075
>>> class MyListener(object):
7176
... def on_name(self, instance, value, **kwargs):
7277
... print('emitter name is {}'.format(value))
7378
... def on_value(self, instance, value, **kwargs):
7479
... print('emitter value is {}'.format(value))
75-
...
80+
7681
>>> emitter = MyEmitter()
7782
>>> listener = MyListener()
83+
84+
>>> # Bind to the "name" and "value" properties of emitter
7885
>>> emitter.bind(name=listener.on_name, value=listener.on_value)
86+
87+
>>> # Set emitter.name property (triggering the on_name callback)
7988
>>> emitter.name = 'foo'
8089
emitter name is foo
90+
91+
>>> # Set emitter.value (triggering the on_value callback)
8192
>>> emitter.value = 42
8293
emitter value is 42
8394

doc/source/async.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ bind_async method
2626

2727
>>> import asyncio
2828
>>> from pydispatch import Dispatcher
29+
2930
>>> class MyEmitter(Dispatcher):
3031
... _events_ = ['on_state']
3132
... async def trigger(self):
3233
... await asyncio.sleep(1)
3334
... self.emit('on_state')
35+
3436
>>> class MyAsyncListener(object):
3537
... def __init__(self):
3638
... self.event_received = asyncio.Event()
@@ -39,11 +41,14 @@ bind_async method
3941
... self.event_received.set()
4042
... async def wait_for_event(self):
4143
... await self.event_received.wait()
44+
4245
>>> loop = asyncio.get_event_loop()
4346
>>> emitter = MyEmitter()
4447
>>> listener = MyAsyncListener()
48+
4549
>>> # Pass the event loop as first argument to "bind_async"
4650
>>> emitter.bind_async(loop, on_state=listener.on_emitter_state)
51+
4752
>>> loop.run_until_complete(emitter.trigger())
4853
>>> loop.run_until_complete(listener.wait_for_event())
4954
received on_state event
@@ -55,11 +60,13 @@ bind (with keyword argument)
5560

5661
>>> import asyncio
5762
>>> from pydispatch import Dispatcher
63+
5864
>>> class MyEmitter(Dispatcher):
5965
... _events_ = ['on_state']
6066
... async def trigger(self):
6167
... await asyncio.sleep(1)
6268
... self.emit('on_state')
69+
6370
>>> class MyAsyncListener(object):
6471
... def __init__(self):
6572
... self.event_received = asyncio.Event()
@@ -68,11 +75,14 @@ bind (with keyword argument)
6875
... self.event_received.set()
6976
... async def wait_for_event(self):
7077
... await self.event_received.wait()
78+
7179
>>> loop = asyncio.get_event_loop()
7280
>>> emitter = MyEmitter()
7381
>>> listener = MyAsyncListener()
82+
7483
>>> # Pass the event loop using __aio_loop__
7584
>>> emitter.bind(on_state=listener.on_emitter_state, __aio_loop__=loop)
85+
7686
>>> loop.run_until_complete(emitter.trigger())
7787
>>> loop.run_until_complete(listener.wait_for_event())
7888
received on_state event
@@ -99,13 +109,15 @@ This can also be done with :any:`Property` objects
99109

100110
>>> import asyncio
101111
>>> from pydispatch import Dispatcher, Property
112+
102113
>>> class MyEmitter(Dispatcher):
103114
... value = Property()
104115
... async def change_values(self):
105116
... for i in range(5):
106117
... await asyncio.sleep(.1)
107118
... self.value = i
108119
... return 'done'
120+
109121
>>> class MyAsyncListener(object):
110122
... async def wait_for_values(self, emitter):
111123
... # Get the Event object for the Property
@@ -118,6 +130,7 @@ This can also be done with :any:`Property` objects
118130
... if value >= 4:
119131
... break
120132
... return 'done'
133+
121134
>>> loop = asyncio.get_event_loop()
122135
>>> emitter = MyEmitter()
123136
>>> listener = MyAsyncListener()

doc/source/dispatcher.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,28 @@ Usage
1010
.. doctest:: dispatcher_basic
1111

1212
>>> from pydispatch import Dispatcher
13+
1314
>>> class MyEmitter(Dispatcher):
1415
... _events_ = ['on_state', 'new_data']
16+
1517
>>> # An observer - could inherit from Dispatcher or any other class
1618
>>> class MyListener(object):
1719
... def on_new_data(self, *args, **kwargs):
1820
... data = kwargs.get('data')
1921
... print('I got data: {}'.format(data))
2022
... def on_emitter_state(self, *args, **kwargs):
2123
... print('emitter state changed')
24+
2225
>>> emitter = MyEmitter()
2326
>>> listener = MyListener()
27+
28+
>>> # Bind to the "on_state" and "new_data" events of emitter
2429
>>> emitter.bind(on_state=listener.on_emitter_state)
2530
>>> emitter.bind(new_data=listener.on_new_data)
31+
2632
>>> emitter.emit('new_data', data='foo')
2733
I got data: foo
34+
2835
>>> emitter.emit('on_state')
2936
emitter state changed
3037

doc/source/properties.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,35 @@ Usage
1616
.. doctest:: property_basic_dt
1717

1818
>>> from pydispatch import Dispatcher, Property
19+
1920
>>> class MyEmitter(Dispatcher):
2021
... name = Property()
2122
... value = Property()
23+
2224
>>> class MyListener(object):
2325
... def on_name(self, instance, value, **kwargs):
2426
... print('emitter name is {}'.format(value))
2527
... def on_value(self, instance, value, **kwargs):
2628
... print('emitter value is {}'.format(value))
29+
2730
>>> emitter = MyEmitter()
2831
>>> listener = MyListener()
32+
2933
>>> # Bind to the "name" and "value" properties of emitter
3034
>>> emitter.bind(name=listener.on_name, value=listener.on_value)
35+
3136
>>> # Set emitter.name property (triggering the on_name callback)
3237
>>> emitter.name = 'foo'
3338
emitter name is foo
39+
3440
>>> # Set emitter.value (triggering the on_value callback)
3541
>>> emitter.value = 42
3642
emitter value is 42
37-
>>> # If the property value assigned equals the current value:
43+
44+
>>> # If the property value assigned equals the current value,
45+
>>> # the event isn't triggered.
3846
>>> emitter.value = 42
39-
>>> # No event is dispatched
47+
4048
>>> emitter.value = 43
4149
emitter value is 43
4250

@@ -55,35 +63,48 @@ structure can trigger an event.
5563
.. doctest:: property_containers
5664

5765
>>> from pydispatch import Dispatcher, ListProperty, DictProperty
66+
5867
>>> class MyEmitter(Dispatcher):
5968
... values = ListProperty()
6069
... data = DictProperty()
70+
6171
>>> class MyListener(object):
6272
... def on_values(self, instance, value, **kwargs):
6373
... print('emitter.values = {}'.format(value))
6474
... def on_data(self, instance, value, **kwargs):
6575
... print('emitter.data = {}'.format(value))
76+
6677
>>> emitter = MyEmitter()
6778
>>> listener = MyListener()
79+
80+
>>> # Bind to the "values" (list) and "data" (dict) properties of emitter
6881
>>> emitter.bind(values=listener.on_values, data=listener.on_data)
82+
6983
>>> emitter.values.append('foo')
7084
emitter.values = ['foo']
85+
7186
>>> emitter.values.extend(['bar', 'baz'])
7287
emitter.values = ['foo', 'bar', 'baz']
88+
7389
>>> # The property can be assigned directly
7490
>>> emitter.data = {'foo':'bar'}
7591
emitter.data = {'foo': 'bar'}
92+
7693
>>> # or using item assignment
7794
>>> emitter.data['foo'] = 'baz'
7895
emitter.data = {'foo': 'baz'}
96+
7997
>>> # also through the update() method
8098
>>> emitter.data.update({'spam':'eggs'})
8199
emitter.data = {'foo': 'baz', 'spam': 'eggs'}
100+
82101
>>> emitter.data.clear()
83102
emitter.data = {}
103+
84104
>>> # Create a nested dict
85105
>>> emitter.data['fruit'] = {'apple':'red'}
86106
emitter.data = {'fruit': {'apple': 'red'}}
107+
87108
>>> # changes to the inner dict are propagated and dispatched
88109
>>> emitter.data['fruit']['banana'] = 'yellow'
89110
emitter.data = {'fruit': {'apple': 'red', 'banana': 'yellow'}}

pydispatch/dispatch.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class Foo(Dispatcher):
161161
162162
>>> class Foo(Dispatcher):
163163
... _events_ = ['test_event']
164+
164165
>>> class Bar(object):
165166
... def __init__(self):
166167
... self.got_foo_event = asyncio.Event()
@@ -169,11 +170,13 @@ class Foo(Dispatcher):
169170
... print('got foo!')
170171
... async def on_foo_test_event(self, *args, **kwargs):
171172
... self.got_foo_event.set()
173+
174+
>>> loop = asyncio.get_event_loop()
172175
>>> foo = Foo()
173176
>>> bar = Bar()
174-
>>> loop = asyncio.get_event_loop()
175177
>>> foo.bind(test_event=bar.on_foo_test_event, __aio_loop__=loop)
176178
>>> fut = asyncio.ensure_future(bar.wait_for_foo())
179+
177180
>>> foo.emit('test_event')
178181
>>> loop.run_until_complete(fut)
179182
got foo!
@@ -270,8 +273,10 @@ def emission_lock(self, name):
270273
271274
>>> class Foo(Dispatcher):
272275
... _events_ = ['my_event']
276+
273277
>>> def on_my_event(value):
274278
... print(value)
279+
275280
>>> foo = Foo()
276281
>>> foo.bind(my_event=on_my_event)
277282
>>> with foo.emission_lock('my_event'):

pydispatch/properties.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
.. doctest:: properties_module
77
88
>>> from pydispatch import Dispatcher, Property
9+
910
>>> class Foo(Dispatcher):
1011
... name = Property()
1112
... value = Property()
1213
... def __str__(self):
1314
... return self.__class__.__name__
15+
1416
>>> class Listener(object):
1517
... def on_foo_name(self, instance, value, **kwargs):
1618
... print("{}'s name is {}".format(instance, value))
1719
... def on_foo_value(self, instance, value, **kwargs):
1820
... print('{} = {}'.format(instance, value))
21+
1922
>>> foo_obj = Foo()
2023
>>> listener_obj = Listener()
2124
>>> foo_obj.bind(name=listener_obj.on_foo_name, value=listener_obj.on_foo_value)
25+
2226
>>> foo_obj.name = 'bar'
2327
Foo's name is bar
2428
>>> foo_obj.value = 42

0 commit comments

Comments
 (0)