-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
45 lines (33 loc) · 1.1 KB
/
utilities.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
import logging
import os
import random
import subprocess
import time
import uuid
from typing import Callable, Any, Optional, Tuple
import numpy as np
def get_tmp_file(content: str, extension: str = '') -> str:
filename = uuid.uuid4().hex + extension
with open(filename, 'w') as f:
f.write(content)
return filename
def get_tmp_path() -> str:
filename = uuid.uuid4().hex
return filename
def run_command(command: str, stdin: Optional[str] = None) -> Tuple[str, str]:
output = subprocess.run(command.split(), capture_output=True, text=True, input=stdin)
return output.stdout, output.stderr
def deterministic(seed: int):
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed) # Numpy module.
random.seed(seed) # Python random module.
def timeit(func: Callable) -> Any:
def wrapped(*args, **kwargs):
func_name = func.__name__
logging.info(f'Running {func_name}')
t0 = time.time()
res = func(*args, **kwargs)
t1 = time.time()
logging.info(f'Run {func_name} in {t1 - t0}s')
return res
return wrapped