Skip to content

Commit

Permalink
Update doc code to use Python 2.
Browse files Browse the repository at this point in the history
Fixes #191
  • Loading branch information
coleifer committed Aug 17, 2023
1 parent f28e9cc commit d3907e1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/alt-backends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ We can use these commands like so:
>>> db['user.1.username'] = 'charles'
>>> db.KTITLE('user.1.username', 'user.1.display_name')
1
>>> print db['user.1.display_name']
>>> print(db['user.1.display_name'])
Charles
Ledis
Expand Down
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ Field types
expression = Message.content.search('python AND (redis OR walrus)')
messages = Message.query(expression)
for message in messages:
print message.content
print(message.content)
.. autoclass:: IntegerField
:members:
Expand Down
36 changes: 18 additions & 18 deletions docs/cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ The :py:meth:`Cache.cached` decorator will *memoize* the return values from the
... def get_time():
... return datetime.datetime.now()
>>> print get_time() # First call, return value cached.
>>> print(get_time()) # First call, return value cached.
2015-01-07 18:26:42.730638
>>> print get_time() # Hits the cache.
>>> print(get_time()) # Hits the cache.
2015-01-07 18:26:42.730638
>>> time.sleep(10) # Wait for cache to expire then call again.
>>> print get_time()
>>> print(get_time())
2015-01-07 18:26:53.529011
If a decorated function accepts arguments, then values will be cached based on the arguments specified. In the example below we'll pass a garbage argument to the ``get_time`` function to show how the cache varies for different arguments:
Expand All @@ -62,30 +62,30 @@ If a decorated function accepts arguments, then values will be cached based on t
... def get_time(seed=None):
... return datetime.datetime.now()
>>> print get_time()
>>> print(get_time())
2015-01-07 18:30:53.831977
>>> print get_time()
>>> print(get_time())
2015-01-07 18:30:53.831977
>>> print get_time('foo')
>>> print(get_time('foo'))
2015-01-07 18:30:56.614064
>>> print get_time('foo')
>>> print(get_time('foo'))
2015-01-07 18:30:56.614064
>>> print get_time('bar')
>>> print(get_time('bar'))
2015-01-07 18:31:01.497050
>>> print get_time('foo')
>>> print(get_time('foo'))
2015-01-07 18:30:56.614064
To clear the cache, you can call the special ``bust()`` method on the decorated function:

.. code-block:: pycon
>>> get_time.bust('foo')
>>> print get_time('foo')
>>> print(get_time('foo'))
2015-01-07 18:31:15.326435
Cached Property
Expand All @@ -100,10 +100,10 @@ Python supports dynamic instance attributes through the ``property`` decorator.
... def now(self):
... return datetime.datetime.now()
>>> print clock.now
>>> print(clock.now)
2015-01-12 21:10:34.335755
>>> print clock.now
>>> print(clock.now)
2015-01-12 21:10:34.335755
.. _cache-async:
Expand All @@ -122,7 +122,7 @@ Let's see how this works. We'll add a call to ``time.sleep`` in the decorated fu
>>> import time
>>> @cache.cache_async()
... def get_now(seed=None):
... print 'About to sleep for 5 seconds.'
... print('About to sleep for 5 seconds.')
... time.sleep(5)
... return datetime.datetime.now()
Expand Down Expand Up @@ -150,29 +150,29 @@ We can force our code to block until the result is ready, though:

.. code-block:: pycon
>>> print result(block=True)
>>> print(result(block=True))
2015-01-12 21:28:25.266448
Now that the result has been calculated and cached, a subsequent call to ``get_now()`` will not execute the function body. We can tell because the function does not print *About to sleep for 5 seconds*.

.. code-block:: pycon
>>> result = get_now()
>>> print result()
>>> print(result())
2015-01-12 21:28:25.266448
The result function can be called any number of times. It will always return the same value:

.. code-block:: pycon
>>> print result()
>>> print(result())
2015-01-12 21:28:25.266448
Another trick is passing a timeout to the result function. Let's see what happens when we call ``get_now()`` using a different seed, then specify a timeout to block for the return value. Since we hard-coded a delay of 5 seconds, let's see what happens when we specify a timeout of 4 seconds:

.. code-block:: pycon
>>> print get_now('foo')(timeout=4)
>>> print(get_now('foo')(timeout=4))
About to sleep for 5 seconds.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Expand All @@ -186,7 +186,7 @@ Now let's try with a timeout of 6 seconds (being sure to use a different seed so

.. code-block:: pycon
>>> print get_now('bar')(timeout=6)
>>> print(get_now('bar')(timeout=6))
About to sleep for 5 seconds.
2015-01-12 21:46:49.060883
Expand Down
12 changes: 6 additions & 6 deletions docs/containers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ At the most basic level, Redis acts like an in-memory Python dictionary:
.. code-block:: pycon
>>> db['walrus'] = 'tusk'
>>> print db['walrus']
>>> print(db['walrus'])
tusk
>>> db['not-here']
Expand Down Expand Up @@ -53,17 +53,17 @@ We can use common Python interfaces like iteration, len, contains, etc.

.. code-block:: pycon
>>> print h['name']
>>> print(h['name'])
Charlie
>>> for key, value in h:
... print key, '=>', value
... print(key, '=>', value)
name => Charlie
favorite_cat => Huey
>>> del h['favorite_cat']
>>> h['age'] = 31
>>> print h
>>> print(h)
<Hash "charlie": {'age': '31', 'name': 'Charlie'}>
>>> 'name' in h
Expand All @@ -81,9 +81,9 @@ The :py:class:`List` acts like a Python ``list``.
>>> l = db.List('names')
>>> l.extend(['charlie', 'huey', 'mickey', 'zaizee'])
4L
>>> print l[:2]
>>> print(l[:2])
['charlie', 'huey']
>>> print l[-2:]
>>> print(l[-2:])
['mickey', 'zaizee']
>>> l.pop()
'zaizee'
Expand Down
28 changes: 14 additions & 14 deletions docs/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ We can retrieve all objects in the collection by calling :py:meth:`Model.all`, w
.. code-block:: pycon
>>> for user in User.all():
... print user.name
... print(user.name)
Huey
Zaizee
Charlie
Expand All @@ -92,14 +92,14 @@ To get the objects in order, we can use :py:meth:`Model.query`:
.. code-block:: pycon
>>> for user in User.query(order_by=User.name):
... print user.name
... print(user.name)
Charlie
Huey
Mickey
Zaizee
>>> for user in User.query(order_by=User.dob.desc()):
... print user.dob
... print(user.dob)
2012-04-01
2011-06-01
2007-08-01
Expand All @@ -117,7 +117,7 @@ Let's see how this works by filtering on name and dob. The :py:meth:`~Model.quer
.. code-block:: pycon
>>> for user in User.query(User.dob <= datetime.date(2009, 1, 1)):
... print user.dob
... print(user.dob)
2007-08-01
1983-01-01
Expand All @@ -140,14 +140,14 @@ We can combine multiple filters using bitwise *and* and *or*:
... (User.dob <= high))
>>> for user in query:
... print user.dob
... print(user.dob)
2011-06-01
2007-08-01
>>> query = User.query(User.dob.between(low, high)) # Equivalent to above.
>>> for user in query:
... print user.dob
... print(user.dob)
2011-06-01
2007-08-01
Expand All @@ -157,7 +157,7 @@ We can combine multiple filters using bitwise *and* and *or*:
... (User.dob >= high))
>>> for user in query:
... print user.dob
... print(user.dob)
2012-04-01
1983-01-01
Expand All @@ -167,12 +167,12 @@ You can combine filters with ordering:
>>> expr = (User.name == 'Charlie') | (User.name == 'Zaizee')
>>> for user in User.query(expr, order_by=User.name):
... print user.name
... print(user.name)
Charlie
Zaizee
>>> for user in User.query(User.name != 'Charlie', order_by=User.name.desc()):
... print user.name
... print(user.name)
Zaizee
Mickey
Huey
Expand Down Expand Up @@ -241,31 +241,31 @@ Use :py:meth:`TextField.search` to create a search expression, which is then pas
.. code-block:: pycon
>>> for note in Note.query(Note.content.search('walrus')):
... print note.content
... print(note.content)
do not forget to take the walrus for a walk.
this is a test of walrus FTS.
favorite food is walrus-mix.
>>> for note in Note.query(Note.content.search('walk walrus')):
... print note.content
... print(note.content)
do not forget to take the walrus for a walk.
>>> for note in Note.query(Note.content.search('walrus mix')):
... print note.content
... print(note.content)
favorite food is walrus-mix.
We can also specify complex queries using ``AND`` and ``OR`` conjunctions:

.. code-block:: pycon
>>> for note in Note.query(Note.content.search('walrus AND (mix OR fts)')):
... print note.content
... print(note.content)
this is a test of walrus FTS.
favorite food is walrus-mix.
>>> query = '(test OR food OR walk) AND walrus AND (favorite OR forget)'
>>> for note in Note.query(Note.content.search(query)):
... print note.content
... print(note.content)
do not forget to take the walrus for a walk.
favorite food is walrus-mix.
Expand Down

0 comments on commit d3907e1

Please sign in to comment.