Skip to content

Commit d933a97

Browse files
NicolasHugfacebook-github-bot
authored andcommitted
[fbsync] cleanup download utils (#8273)
Reviewed By: vmoens Differential Revision: D55062790 fbshipit-source-id: d85114aa859d025a467642ee53197ff1d88b1d1d
1 parent 7d6ddfd commit d933a97

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

torchvision/datasets/utils.py

+5-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import urllib.error
1313
import urllib.request
1414
import zipfile
15-
from typing import Any, Callable, Dict, IO, Iterable, Iterator, List, Optional, Tuple, TypeVar, Union
15+
from typing import Any, Callable, Dict, IO, Iterable, List, Optional, Tuple, TypeVar, Union
1616
from urllib.parse import urlparse
1717

1818
import numpy as np
@@ -24,24 +24,12 @@
2424
USER_AGENT = "pytorch/vision"
2525

2626

27-
def _save_response_content(
28-
content: Iterator[bytes],
29-
destination: Union[str, pathlib.Path],
30-
length: Optional[int] = None,
31-
) -> None:
32-
with open(destination, "wb") as fh, tqdm(total=length) as pbar:
33-
for chunk in content:
34-
# filter out keep-alive new chunks
35-
if not chunk:
36-
continue
37-
38-
fh.write(chunk)
39-
pbar.update(len(chunk))
40-
41-
4227
def _urlretrieve(url: str, filename: Union[str, pathlib.Path], chunk_size: int = 1024 * 32) -> None:
4328
with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": USER_AGENT})) as response:
44-
_save_response_content(iter(lambda: response.read(chunk_size), b""), filename, length=response.length)
29+
with open(filename, "wb") as fh, tqdm(total=response.length) as pbar:
30+
while chunk := response.read(chunk_size):
31+
fh.write(chunk)
32+
pbar.update(len(chunk))
4533

4634

4735
def calculate_md5(fpath: Union[str, pathlib.Path], chunk_size: int = 1024 * 1024) -> str:

0 commit comments

Comments
 (0)