-
Notifications
You must be signed in to change notification settings - Fork 849
/
utils.py
93 lines (76 loc) · 2.49 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Sebastian Raschka 2014-2020
# contributor: Vahid Mirjalili
# mlxtend Machine Learning Library Extensions
#
# A counter class for printing the progress of an iterator.
# Author: Sebastian Raschka <sebastianraschka.com>
#
# License: BSD 3 clause
import os
import tarfile
import zipfile
import bz2
import imageio
def check_exists(path):
path = os.path.expanduser(path)
return os.path.exists(path)
def makedir(path):
path = os.path.expanduser(path)
if not check_exists(path):
os.makedirs(path)
def listdir(path, extensions=''):
path = os.path.expanduser(path)
if check_exists(path):
return [f for f in os.listdir(path) if f.endswith(extensions)]
else:
raise FileNotFoundError
def read_image(filename, path=None):
if path is not None:
path = os.path.expanduser(path)
filename = os.path.join(path, filename)
if check_exists(filename):
return imageio.imread(filename)
else:
raise FileNotFoundError
def download_url(url, save_path):
from six.moves import urllib
save_path = os.path.expanduser(save_path)
if not check_exists(save_path):
makedir(save_path)
filename = url.rpartition('/')[2]
filepath = os.path.join(save_path, filename)
try:
print('Downloading '+url+' to '+filepath)
urllib.request.urlretrieve(url, filepath)
except ValueError:
raise Exception('Failed to download! Check URL: ' + url +
' and local path: ' + save_path)
def extract_file(path, to_directory=None):
path = os.path.expanduser(path)
if path.endswith('.zip'):
opener, mode = zipfile.ZipFile, 'r'
elif path.endswith(('.tar.gz', '.tgz')):
opener, mode = tarfile.open, 'r:gz'
elif path.endswith(('tar.bz2', '.tbz')):
opener, mode = tarfile.open, 'r:bz2'
elif path.endswith('.bz2'):
opener, mode = bz2.BZ2File, 'rb'
with open(path[:-4], 'wb') as fp_out, opener(path, 'rb') as fp_in:
for data in iter(lambda: fp_in.read(100 * 1024), b''):
fp_out.write(data)
return
else:
raise (ValueError,
"Could not extract `{}` as no extractor is found!".format(path))
if to_directory is None:
to_directory = os.path.abspath(os.path.join(path, os.path.pardir))
cwd = os.getcwd()
os.chdir(to_directory)
try:
file = opener(path, mode)
try:
file.extractall()
finally:
file.close()
finally:
os.chdir(cwd)