mirror of https://github.com/mahmoud/boltons.git
Update ioutils documentation
* Fix some typos * Remove duplicated code from the zipfile download example * Update the zipfile download example to use requests context manager
This commit is contained in:
parent
1c315de4ea
commit
5d87249fe0
|
@ -7,7 +7,7 @@ Spooled Temporary Files
|
||||||
-----------------------
|
-----------------------
|
||||||
Spooled Temporary Files are file-like objects that start out mapped to
|
Spooled Temporary Files are file-like objects that start out mapped to
|
||||||
in-memory objects, but automatically roll over to a temporary file once they
|
in-memory objects, but automatically roll over to a temporary file once they
|
||||||
reach a certain (configurable) threshhold. Unfortunately the built-in
|
reach a certain (configurable) threshold. Unfortunately the built-in
|
||||||
SpooledTemporaryFile class in Python does not implement the exact API that some
|
SpooledTemporaryFile class in Python does not implement the exact API that some
|
||||||
common classes like StringIO do. SpooledTemporaryFile also spools all of it's
|
common classes like StringIO do. SpooledTemporaryFile also spools all of it's
|
||||||
in-memory files as cStringIO instances. cStringIO instances cannot be
|
in-memory files as cStringIO instances. cStringIO instances cannot be
|
||||||
|
@ -35,7 +35,7 @@ Examples
|
||||||
--------
|
--------
|
||||||
It's not uncommon to find excessive usage of StringIO in older Python code. A
|
It's not uncommon to find excessive usage of StringIO in older Python code. A
|
||||||
SpooledTemporaryFile would be a nice replacement if one wanted to reduce memory
|
SpooledTemporaryFile would be a nice replacement if one wanted to reduce memory
|
||||||
overhead, but unfortunately it's api differs too much. This is a good candidate
|
overhead, but unfortunately its api differs too much. This is a good candidate
|
||||||
for :ref:`spooledbytesio` as it is api compatible and thus may be used as a
|
for :ref:`spooledbytesio` as it is api compatible and thus may be used as a
|
||||||
drop-in replacement.
|
drop-in replacement.
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ Old Code::
|
||||||
|
|
||||||
Updated::
|
Updated::
|
||||||
|
|
||||||
from boltions.ioutils import SpooledBytesIO
|
from boltons.ioutils import SpooledBytesIO
|
||||||
|
|
||||||
flo = SpooledBytesIO()
|
flo = SpooledBytesIO()
|
||||||
flo.write(gigantic_string)
|
flo.write(gigantic_string)
|
||||||
|
@ -64,12 +64,9 @@ Here is a simple example using the requests library to download a zip file::
|
||||||
import requests
|
import requests
|
||||||
from boltons import ioutils
|
from boltons import ioutils
|
||||||
|
|
||||||
s = requests.Session()
|
# Using a context manager with stream=True ensures the connection is closed. See:
|
||||||
r = s.get("http://127.0.0.1/test_file.zip", stream=True)
|
# http://docs.python-requests.org/en/master/user/advanced/#body-content-workflow
|
||||||
if r.status_code == 200:
|
with requests.get("http://127.0.0.1/test_file.zip", stream=True) as r:
|
||||||
flo = SpooledBytesIO()
|
|
||||||
|
|
||||||
r = self._get(url, stream=True)
|
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
with ioutils.SpooledBytesIO() as flo:
|
with ioutils.SpooledBytesIO() as flo:
|
||||||
for chunk in r.iter_content(chunk_size=64000):
|
for chunk in r.iter_content(chunk_size=64000):
|
||||||
|
@ -80,4 +77,4 @@ Here is a simple example using the requests library to download a zip file::
|
||||||
zip_doc = ZipFile(flo)
|
zip_doc = ZipFile(flo)
|
||||||
|
|
||||||
# Print all the files in the zip
|
# Print all the files in the zip
|
||||||
print zip_doc.namelist()
|
print(zip_doc.namelist())
|
||||||
|
|
Loading…
Reference in New Issue