-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.py
61 lines (51 loc) · 1.9 KB
/
util.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
import os
import time
import urllib2
import logging
import threading
import datetime
import errno
def maybe_mkdirs(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def maybe_download(data_dir, source_url, filename):
if not os.path.exists(data_dir):
os.mkdir(data_dir)
filepath = os.path.join(data_dir, filename)
if os.path.exists(filepath):
logging.info("Using cached version of {}.".format(filepath))
else:
file_url = source_url + filename
logging.info("Downloading {}...".format(file_url))
response = urllib2.urlopen(file_url)
with open(filepath, "wb") as f:
f.write(response.read())
statinfo = os.stat(filepath)
logging.info("Succesfully downloaded {} ({} bytes).".format(file_url, statinfo.st_size))
return filepath
class Timer:
def __init__(self, name, active=True):
self.name = name if active else None
def __enter__(self):
self.start = time.time()
self.last_tick = self.start
return self
def __exit__(self, *args):
if self.name is not None:
logging.info("{} duration was {}.".format(self.name, self.readable(time.time() - self.start)))
def readable(self, seconds):
return str(datetime.timedelta(seconds=int(seconds)))
def tick(self, message):
current = time.time()
logging.info("{} took {} ({} since last tick).".format(message, self.readable(current - self.start), self.readable(current - self.last_tick)))
self.last_tick = current
class LoggingToFile(object):
def __init__(self, logdir, filename):
self.handler = logging.FileHandler(os.path.join(logdir, filename))
def __enter__(self):
logging.getLogger().addHandler(self.handler)
def __exit__(self, *args):
logging.getLogger().removeHandler(self.handler)