-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from chrisroadmap/utils
Add utilities
- Loading branch information
Showing
5 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Utility functions.""" | ||
|
||
import errno | ||
import os | ||
import urllib.request | ||
|
||
from tqdm import tqdm | ||
|
||
|
||
# This example is adapted from | ||
# https://stackoverflow.com/questions/15644964/python-progress-bar-and-downloads | ||
class _DownloadProgressBar(tqdm): | ||
def update_to(self, blocks=1, blocksize=1, totalsize=None): | ||
"""Update download progress bar. | ||
Inputs | ||
------ | ||
blocks : int, optional | ||
Number of blocks transferred so far [default: 1]. | ||
blocksize : int, optional | ||
Size of each block (in tqdm units) [default: 1]. | ||
totalsize : int, optional | ||
Total size (in tqdm units). If [default: None] remains unchanged. | ||
""" | ||
if totalsize is not None: | ||
self.total = totalsize | ||
self.update(blocks * blocksize - self.n) | ||
|
||
|
||
def check_and_download(url, filepath, clobber=False): | ||
"""Check prescence of a file and downloads if not present. | ||
Inputs | ||
------ | ||
url : | ||
url to download from | ||
filepath : str | ||
filename to download to | ||
clobber : bool | ||
False if download should not overwrite existing file, True if it should | ||
""" | ||
if clobber or not os.path.isfile(filepath): | ||
with _DownloadProgressBar( | ||
unit="B", unit_scale=True, miniters=1, desc=url.split("/")[-1] | ||
) as progress: | ||
urllib.request.urlretrieve( | ||
url, filename=filepath, reporthook=progress.update_to | ||
) | ||
|
||
|
||
# I got this from Stack Overflow, but can't find where now. | ||
def mkdir_p(path): | ||
"""Check to see if directory exists, and if not, create it. | ||
Inputs | ||
------ | ||
path : str | ||
directory to create | ||
Raises | ||
------ | ||
OSError: | ||
if directory cannot be created | ||
""" | ||
try: | ||
os.makedirs(path) | ||
except OSError as exc: | ||
if exc.errno == errno.EEXIST and os.path.isdir(path): | ||
pass | ||
else: | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import os | ||
|
||
from climateforcing.utils import check_and_download, mkdir_p | ||
|
||
|
||
def test_mkdir_p(): | ||
mkdir_p("tests/testdata/dummy") | ||
os.rmdir("tests/testdata/dummy") | ||
|
||
|
||
def test_check_and_download(): | ||
check_and_download( | ||
"http://homepages.see.leeds.ac.uk/~mencsm/images/ta_q_kernel.png", | ||
"tests/testdata/test_image.png", | ||
) | ||
check_and_download( | ||
"http://homepages.see.leeds.ac.uk/~mencsm/images/ta_q_kernel.png", | ||
"tests/testdata/test_image.png", | ||
clobber=True, | ||
) | ||
os.remove("tests/testdata/test_image.png") |