From 31d5b88668022aad65d7fb45700c96a1499fed4e Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Wed, 10 Feb 2021 20:23:35 +0000 Subject: [PATCH] fix py3 urllib examples --- .meta/.readme.rst | 9 ++++++--- DEMO.ipynb | 11 +++++++---- examples/tqdm_wget.py | 11 ++++++++--- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/.meta/.readme.rst b/.meta/.readme.rst index 42090888..26347ef3 100644 --- a/.meta/.readme.rst +++ b/.meta/.readme.rst @@ -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 diff --git a/DEMO.ipynb b/DEMO.ipynb index 9b7ad089..b4ca48db 100644 --- a/DEMO.ipynb +++ b/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:" ] }, { diff --git a/examples/tqdm_wget.py b/examples/tqdm_wget.py index 695e5e5a..3cd5e740 100644 --- a/examples/tqdm_wget.py +++ b/examples/tqdm_wget.py @@ -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)