-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
69 lines (51 loc) · 1.54 KB
/
functions.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
# encoding: utf-8
import os
import sys
import time
# notify function
def notify(title='', message='', url='', group='', execute=''):
t = '-title "%s"' % (title)
m = '-message "%s"' % (message)
u = '-open "%s"' % (url)
g = '-group "%s"' % (group)
e = '-execute "%s"' % (execute)
print ' '.join([m, t, u, g, e])
os.system(
'/usr/local/bin/terminal-notifier %s' % (' '.join([m, t, u, g, e])))
# if unicode char is chinese
def isChinese(uchar):
return True if uchar >= u'\u4e00' and uchar <= u'\u9fa5' else False
# log
def logger(name=__name__, msg='', print_out=True):
if print_out:
print(msg)
path = sys.path[0] + '/log/' + name + '.log'
f = open(path, 'a')
f.write(msg + '\n')
f.close()
# testA => test_a
def camelToUnderline(string):
result = ''
for char in string:
result += char if char.islower() else '_' + char.lower()
return result
# test_a => testA
def underlineToCamel(string):
result = ''
for substr in string.split('_'):
result += substr if result == '' else substr.capitalize()
return result
# in-class decorator
# it calculate total time cost by the function().
# need to specify a class attribute(accum)
def timerAccumulate(accum):
def timer(f):
def _func(*args, **kw):
s = time.time()
r = f(*args, **kw)
# arg[0] is self (instance)
t = getattr(args[0], accum) + time.time() - s
setattr(args[0], accum, t)
return r
return _func
return timer