forked from yosinski/deep-visualization-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
62 lines (45 loc) · 1.52 KB
/
misc.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
#! /usr/bin/env python
import os
import time
import errno
import re
class WithTimer:
def __init__(self, title = '', quiet = False):
self.title = title
self.quiet = quiet
def elapsed(self):
return time.time() - self.wall, time.clock() - self.proc
def enter(self):
'''Manually trigger enter'''
self.__enter__()
def __enter__(self):
self.proc = time.clock()
self.wall = time.time()
return self
def __exit__(self, *args):
if not self.quiet:
titlestr = (' ' + self.title) if self.title else ''
print 'Elapsed%s: wall: %.06f, sys: %.06f' % ((titlestr,) + self.elapsed())
def mkdir_p(path):
# From https://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def combine_dicts(dicts_tuple):
'''Combines multiple dictionaries into one by adding a prefix to keys'''
ret = {}
for prefix,dictionary in dicts_tuple:
for key in dictionary.keys():
ret['%s%s' % (prefix, key)] = dictionary[key]
return ret
def tsplit(string, no_empty_strings, *delimiters):
# split string using multiple delimiters
pattern = '|'.join(map(re.escape, delimiters))
strings = re.split(pattern, string)
if no_empty_strings:
strings = filter(None, strings)
return strings