Skip to content

Commit 5fa3fe5

Browse files
authored
Prefer Python 3 syntax in examples (#1325)
As Python 3 is the future of the language, when the docs need to make a syntax choice, use the Python 3 version.
1 parent 76eebce commit 5fa3fe5

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

README.rst

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Getting Started
4848
>>> r.set('foo', 'bar')
4949
True
5050
>>> r.get('foo')
51-
'bar'
51+
b'bar'
5252
5353
By default, all responses are returned as `bytes` in Python 3 and `str` in
5454
Python 2. The user is responsible for decoding to Python 3 strings or Python 2
@@ -406,7 +406,7 @@ Pipelines are quite simple to use:
406406
>>> # the EXECUTE call sends all buffered commands to the server, returning
407407
>>> # a list of responses, one for each command.
408408
>>> pipe.execute()
409-
[True, 'baz']
409+
[True, b'baz']
410410
411411
For ease of use, all commands being buffered into the pipeline return the
412412
pipeline object itself. Therefore calls can be chained like:
@@ -534,11 +534,11 @@ instance.
534534
.. code-block:: pycon
535535
536536
>>> p.get_message()
537-
{'pattern': None, 'type': 'subscribe', 'channel': 'my-second-channel', 'data': 1L}
537+
{'pattern': None, 'type': 'subscribe', 'channel': b'my-second-channel', 'data': 1}
538538
>>> p.get_message()
539-
{'pattern': None, 'type': 'subscribe', 'channel': 'my-first-channel', 'data': 2L}
539+
{'pattern': None, 'type': 'subscribe', 'channel': b'my-first-channel', 'data': 2}
540540
>>> p.get_message()
541-
{'pattern': None, 'type': 'psubscribe', 'channel': 'my-*', 'data': 3L}
541+
{'pattern': None, 'type': 'psubscribe', 'channel': b'my-*', 'data': 3}
542542
543543
Every message read from a `PubSub` instance will be a dictionary with the
544544
following keys.
@@ -565,9 +565,9 @@ Let's send a message now.
565565
>>> r.publish('my-first-channel', 'some data')
566566
2
567567
>>> p.get_message()
568-
{'channel': 'my-first-channel', 'data': 'some data', 'pattern': None, 'type': 'message'}
568+
{'channel': b'my-first-channel', 'data': b'some data', 'pattern': None, 'type': 'message'}
569569
>>> p.get_message()
570-
{'channel': 'my-first-channel', 'data': 'some data', 'pattern': 'my-*', 'type': 'pmessage'}
570+
{'channel': b'my-first-channel', 'data': b'some data', 'pattern': b'my-*', 'type': 'pmessage'}
571571
572572
Unsubscribing works just like subscribing. If no arguments are passed to
573573
[p]unsubscribe, all channels or patterns will be unsubscribed from.
@@ -577,11 +577,11 @@ Unsubscribing works just like subscribing. If no arguments are passed to
577577
>>> p.unsubscribe()
578578
>>> p.punsubscribe('my-*')
579579
>>> p.get_message()
580-
{'channel': 'my-second-channel', 'data': 2L, 'pattern': None, 'type': 'unsubscribe'}
580+
{'channel': b'my-second-channel', 'data': 2, 'pattern': None, 'type': 'unsubscribe'}
581581
>>> p.get_message()
582-
{'channel': 'my-first-channel', 'data': 1L, 'pattern': None, 'type': 'unsubscribe'}
582+
{'channel': b'my-first-channel', 'data': 1, 'pattern': None, 'type': 'unsubscribe'}
583583
>>> p.get_message()
584-
{'channel': 'my-*', 'data': 0L, 'pattern': None, 'type': 'punsubscribe'}
584+
{'channel': b'my-*', 'data': 0, 'pattern': None, 'type': 'punsubscribe'}
585585
586586
redis-py also allows you to register callback functions to handle published
587587
messages. Message handlers take a single argument, the message, which is a
@@ -597,11 +597,11 @@ handled.
597597
.. code-block:: pycon
598598
599599
>>> def my_handler(message):
600-
... print 'MY HANDLER: ', message['data']
600+
... print('MY HANDLER: ', message['data'])
601601
>>> p.subscribe(**{'my-channel': my_handler})
602602
# read the subscribe confirmation message
603603
>>> p.get_message()
604-
{'pattern': None, 'type': 'subscribe', 'channel': 'my-channel', 'data': 1L}
604+
{'pattern': None, 'type': 'subscribe', 'channel': b'my-channel', 'data': 1}
605605
>>> r.publish('my-channel', 'awesome data')
606606
1
607607
# for the message handler to work, we need tell the instance to read data.
@@ -611,7 +611,7 @@ handled.
611611
MY HANDLER: awesome data
612612
# note here that the my_handler callback printed the string above.
613613
# `message` is None because the message was handled by our handler.
614-
>>> print message
614+
>>> print(message)
615615
None
616616
617617
If your application is not interested in the (sometimes noisy)
@@ -628,7 +628,7 @@ application.
628628
>>> r.publish('my-channel', 'my data')
629629
1
630630
>>> p.get_message()
631-
{'channel': 'my-channel', 'data': 'my data', 'pattern': None, 'type': 'message'}
631+
{'channel': b'my-channel', 'data': b'my data', 'pattern': None, 'type': 'message'}
632632
633633
There are three different strategies for reading messages.
634634

@@ -710,11 +710,11 @@ supported:
710710
.. code-block:: pycon
711711
712712
>>> r.pubsub_channels()
713-
['foo', 'bar']
713+
[b'foo', b'bar']
714714
>>> r.pubsub_numsub('foo', 'bar')
715-
[('foo', 9001), ('bar', 42)]
715+
[(b'foo', 9001), (b'bar', 42)]
716716
>>> r.pubsub_numsub('baz')
717-
[('baz', 0)]
717+
[(b'baz', 0)]
718718
>>> r.pubsub_numpat()
719719
1204
720720
@@ -835,7 +835,7 @@ operations).
835835
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
836836
>>> master.set('foo', 'bar')
837837
>>> slave.get('foo')
838-
'bar'
838+
b'bar'
839839
840840
The master and slave objects are normal Redis instances with their
841841
connection pool bound to the Sentinel instance. When a Sentinel backed client
@@ -865,7 +865,7 @@ that return Python iterators for convenience: `scan_iter`, `hscan_iter`,
865865
>>> for key, value in (('A', '1'), ('B', '2'), ('C', '3')):
866866
... r.set(key, value)
867867
>>> for key in r.scan_iter():
868-
... print key, r.get(key)
868+
... print(key, r.get(key))
869869
A 1
870870
B 2
871871
C 3

redis/sentinel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class Sentinel(object):
135135
>>> master.set('foo', 'bar')
136136
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
137137
>>> slave.get('foo')
138-
'bar'
138+
b'bar'
139139
140140
``sentinels`` is a list of sentinel nodes. Each node is represented by
141141
a pair (hostname, port).

0 commit comments

Comments
 (0)