mirror of https://github.com/tqdm/tqdm.git
fix py3 urllib examples
This commit is contained in:
parent
7f971d5008
commit
31d5b88668
|
@ -658,6 +658,7 @@ Here's an example with ``urllib``:
|
|||
|
||||
import urllib, os
|
||||
from tqdm import tqdm
|
||||
urllib = getattr(urllib, 'request', urllib)
|
||||
|
||||
class TqdmUpTo(tqdm):
|
||||
"""Provides `update_to(n)` which uses `tqdm.update(delta_n)`."""
|
||||
|
@ -719,12 +720,14 @@ down to:
|
|||
from tqdm import tqdm
|
||||
|
||||
eg_link = "https://caspersci.uk.to/matryoshka.zip"
|
||||
response = getattr(urllib, 'request', urllib).urlopen(eg_link)
|
||||
with tqdm.wrapattr(open(os.devnull, "wb"), "write",
|
||||
miniters=1, desc=eg_link.split('/')[-1]) as fout:
|
||||
for chunk in urllib.urlopen(eg_link):
|
||||
miniters=1, desc=eg_link.split('/')[-1],
|
||||
total=getattr(response, 'length', None)) as fout:
|
||||
for chunk in response:
|
||||
fout.write(chunk)
|
||||
|
||||
The ``requests`` equivalent is nearly identical, albeit with a ``total``:
|
||||
The ``requests`` equivalent is nearly identical:
|
||||
|
||||
.. code:: python
|
||||
|
||||
|
|
11
DEMO.ipynb
11
DEMO.ipynb
|
@ -690,6 +690,7 @@
|
|||
"source": [
|
||||
"import urllib, os\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"urllib = getattr(urllib, 'request', urllib)\n",
|
||||
"\n",
|
||||
"class TqdmUpTo(tqdm):\n",
|
||||
" \"\"\"Provides `update_to(n)` which uses `tqdm.update(delta_n)`.\"\"\"\n",
|
||||
|
@ -760,7 +761,7 @@
|
|||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"matryoshka.zip: 254kB [00:00, 334kB/s] \n"
|
||||
"matryoshka.zip: 100%|██████████| 254k/254k [00:00<00:00, 602kB/s] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
@ -769,9 +770,11 @@
|
|||
"from tqdm import tqdm\n",
|
||||
"\n",
|
||||
"eg_link = \"https://caspersci.uk.to/matryoshka.zip\"\n",
|
||||
"response = getattr(urllib, 'request', urllib).urlopen(eg_link)\n",
|
||||
"with tqdm.wrapattr(open(os.devnull, \"wb\"), \"write\",\n",
|
||||
" miniters=1, desc=eg_link.split('/')[-1]) as fout:\n",
|
||||
" for chunk in urllib.urlopen(eg_link):\n",
|
||||
" miniters=1, desc=eg_link.split('/')[-1],\n",
|
||||
" total=getattr(response, 'length', None)) as fout:\n",
|
||||
" for chunk in response:\n",
|
||||
" fout.write(chunk)"
|
||||
]
|
||||
},
|
||||
|
@ -779,7 +782,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The `requests` equivalent is nearly identical, albeit with a `total`:"
|
||||
"The `requests` equivalent is nearly identical:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -20,7 +20,10 @@ Options:
|
|||
The local file path in which to save the url [default: /dev/null].
|
||||
"""
|
||||
|
||||
import urllib
|
||||
try:
|
||||
from urllib import request as urllib
|
||||
except ImportError: # py2
|
||||
import urllib
|
||||
from os import devnull
|
||||
|
||||
from docopt import docopt
|
||||
|
@ -102,7 +105,9 @@ with TqdmUpTo(unit='B', unit_scale=True, unit_divisor=1024, miniters=1,
|
|||
t.total = t.n
|
||||
|
||||
# Even simpler progress by wrapping the output file's `write()`
|
||||
response = urllib.urlopen(eg_link)
|
||||
with tqdm.wrapattr(open(eg_out, "wb"), "write",
|
||||
miniters=1, desc=eg_file) as fout:
|
||||
for chunk in urllib.urlopen(eg_link):
|
||||
miniters=1, desc=eg_file,
|
||||
total=getattr(response, 'length', None)) as fout:
|
||||
for chunk in response:
|
||||
fout.write(chunk)
|
||||
|
|
Loading…
Reference in New Issue