[b40915]: / HomoAug / utils / misc.py

Download this file

112 lines (92 with data), 2.4 kB

  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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
import hashlib
import os
import signal
import subprocess
import sys
import time
def time2str(time_used):
gaps = [
('days', 86400000),
('h', 3600000),
('min', 60000),
('s', 1000),
('ms', 1)
]
time_used *= 1000
time_str = []
for unit, gap in gaps:
val = time_used // gap
if val > 0:
time_str.append('{}{}'.format(int(val), unit))
time_used -= val * gap
if len(time_str) == 0:
time_str.append('0ms')
return ' '.join(time_str)
def get_date():
return time.strftime('%Y-%m-%d', time.localtime(time.time()))
def get_time(t=None):
if t is None:
t = time.time()
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t))
def hash_seed(*items, width=32):
# width: range of seed: [0, 2**width)
sha = hashlib.sha256()
for item in items:
sha.update(str(item).encode('utf-8'))
return int(sha.hexdigest()[23:23+width//4], 16)
def execute(command, verbose=False, cmd_out_off=False):
if verbose:
print(command)
print()
if cmd_out_off:
popen = silent(subprocess.Popen)
else:
popen = subprocess.Popen
p = popen(command, shell=True)
try:
p.wait()
except KeyboardInterrupt:
try:
os.kill(p.pid, signal.SIGINT)
except OSError:
pass
p.wait()
def silent(func):
"""
Make function silent.
Useful for closing the output if you don't find a switch.
"""
def wrap_silent(*args, **kwargs):
fd = os.dup(1)
sys.stdout.flush()
os.close(1)
os.open(os.devnull, os.O_WRONLY)
res = func(*args, **kwargs)
os.close(1)
os.dup(fd)
os.close(fd)
return res
return wrap_silent
def with_time(func, pretty_time=False):
"""
Usage:
1. as a function decorator
``` python
@with_time
def func(...):
...
result, cost_in_seconds = func(...)
```
2. directly apply
``` python
result, cost_in_seconds = with_time(func)(...)
```
"""
def wrap_time(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
time_cost = time.time() - start
if pretty_time:
time_cost = time2str(time_cost)
return res, time_cost
return wrap_time