-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
69 lines (57 loc) · 2 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
"""
General purpose utility functions
> mk_dir moved from rx_util.py to here.
Last update: 14 May 2023 14:08
"""
import os
import time
def TicTocGenerator(): # noqa
"""
This will be the main function through which we define both tic() and toc()
TicToc = TicTocGenerator() # create an instance of the TicTocGen generator
see : https://stackoverflow.com/a/26695514
"""
# Generator that returns time differences
ti = 0 # initial time
tf = time.time() # final time
while True:
ti = tf
tf = time.time()
yield tf - ti # returns the time difference
def toc(tic_toc, temp_bool=True):
# Prints the time difference yielded by generator instance TicToc
temp_time_interval = next(tic_toc)
if temp_bool:
# print("Elapsed time: %f seconds.\n" % tempTimeInterval)
return temp_time_interval
def tic(tic_toc):
# Records a time in TicToc, marks the beginning of a time interval
toc(tic_toc, False)
def check_path_base(full_path):
"""
check existence of given base directory (support nested directories)
if missing then create directories
"""
if full_path[-1] != '/':
# get rid of the last item and check base path
directory = "/".join(full_path.split('/')[:-1])
else:
# full path is already target to a directory, no need to remove filename
directory = full_path
# create base path
os.makedirs(directory, exist_ok=True)
def mk_dir(path):
"""
Automatically create directories for the specified path if any are missing,
Remember to add a trailing '/' backslash at the end of the input path.
"""
assert path[-1] == '/', 'please add "/" at the end of path, To make sure that the input is not a file!'
# check existence of each sub path
if path[0] == '/':
tp = '' # tp: temporarily path
else:
tp = '.'
for dr in path.split('/'):
tp += '/' + dr
if not os.path.isdir(tp):
os.mkdir(tp)