forked from kuhaku/atango
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
93 lines (79 loc) · 2.38 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
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
# -*- coding: utf-8 -*-
import functools
import subprocess
import time
import numpy as np
from lib.logger import logger
def command(cmd, shell=False, allow_err=False):
'''
Args:
<tuple> command parameters
<bool> Popen shell option (default: False)
Returns:
(<bool> succeeded, <str> stdout, <str> stderr)
'''
out = ''
error = ''
proc = subprocess.Popen(cmd, shell=shell,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, error) = proc.communicate()
(out, error) = (out.decode('utf8'), error.decode('utf8'))
result = False if error else True
if not allow_err and not result:
message = 'Command: %s\nStdErr: %s' % (str(cmd), error)
raise Exception(message)
return (result, out, error)
def choice(iterable):
"""
Param:
<iterable> iterable
Return:
<object> content
"""
return iterable[np.random.randint(len(iterable))]
def nones(num, dimension=1):
"""
Param:
<int> num
Return:
<list> nones
"""
if num < 1 or dimension < 1:
raise ValueError('params must be more than 0')
if dimension == 1:
return [None for i in range(num)]
elif dimension > 1:
return [[None] * dimension for i in range(num)]
elif not isinstance(num, int) or not isinstance(dimension, int):
raise ValueError('params must be <int>')
def map_dict(method, d):
"""
Apply method to dict's values
Params:
<callable> method
<dict> d
Return:
<dict> d
"""
return dict(zip(d.keys(), map(method, d.values())))
def has_substr(iterable, substr):
return any(substr in item for item in iterable)
def is_mojie(text):
if '( ' in text or '@@@@' in text:
return True
return text.count(' ') > 4
def retry(num=1, interval=0.1, allow_null=True):
def receive_func(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
for i in range(num):
try:
result = func(*args, **kwargs)
assert result, ValueError('Null result is not allowed')
return result
except Exception as e:
logger.warn('%s: %s' % (type(e), str(e)))
time.sleep(interval)
return wrapper
return receive_func