-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
63 lines (52 loc) · 1.8 KB
/
helper.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
from datetime import datetime
import re
g_default_time_format = '%Y-%m-%d %H:%M:%S'
# borrowed
def unicode_clean(data):
"""
This is to convert from unicode to str obj in python.!!!
"""
# if this is a unicode string, return its string representation
if isinstance(data, unicode):
return data.encode('utf-8')
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [unicode_clean(item) for item in data]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict):
return {
# CLEARFUl - data.iteritems() is not present in python v3
unicode_clean(key): unicode_clean(value)
for key, value in data.iteritems()
}
# if it's anything else, return it in its original form
return data
def date_long_to_str(long_time, to_format=None):
"""
function to parse a long time into a string format
"""
format = g_default_time_format if to_format is None else to_format
return datetime.fromtimestamp(long_time).strftime(format)
def date_from_str_to_long(str_time, from_format=None):
"""
function to parse from time in string to seconds long
"""
format = g_default_time_format if from_format is None else from_format
return int(datetime.strptime(str_time, format).strftime("%s"))
def str_to_num(string):
"""
Convert a numerical string represent into a number
"""
try:
return int(string)
except ValueError:
try:
return float(string)
except ValueError:
return None
def contains_num(string):
"""
check if a string contains some numbers
"""
return bool(re.search(r'\d', string.strip()))