Skip to content

Commit

Permalink
Add usage example/doctest to ByteBuffer docstring
Browse files Browse the repository at this point in the history
This only passes as a doctest in Python 3 because of the bytestring
literals for some of the expected values. In Python 2, those values are
simply regular strings and doctest seems to work by comparing the string
representations of the values rather than the values themselves, so
those lines fail because they don't have the `b` prefix.
  • Loading branch information
Dan Lidral-Porter committed Apr 16, 2019
1 parent 164339a commit c604a78
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion smart_open/bytebuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,31 @@ class ByteBuffer(object):
The bytes are stored in a bytestring, and previously-read bytes are freed
when the buffer is next filled (by slicing the bytestring into a smaller
copy)."""
copy).
Example:
>>> buf = ByteBuffer(chunk_size = 8)
>>> message_bytes = iter([b'Hello, W', b'orld!'])
>>> buf.fill(message_bytes)
8
>>> len(buf) # only chunk_size bytes are filled
8
>>> buf.peek()
b'Hello, W'
>>> len(buf) # peek() does not change read position
8
>>> buf.read(6)
b'Hello,'
>>> len(buf) # read() does change read position
2
>>> buf.fill(message_bytes)
5
>>> buf.read()
b' World!'
>>> len(buf)
0
"""

def __init__(self, chunk_size=io.DEFAULT_BUFFER_SIZE):
"""Create a ByteBuffer instance that reads chunk_size bytes when filled.
Expand Down

0 comments on commit c604a78

Please sign in to comment.