-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
81 lines (65 loc) · 2.3 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import sys
import time
import ConfigParser
import logging
import logging.handlers
from functools import wraps
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
__author__ = "Wen-Hao Lee"
__email__ = "[email protected]"
__copyright__ = "Copyright 2014, Numnum"
base_path = os.path.dirname(os.path.abspath(__file__))
config = ConfigParser.ConfigParser()
config.read(os.path.join(base_path, 'config.ini'))
def get_logger(logger_name, file_name):
logger = logging.getLogger(logger_name)
logger.setLevel(logging.INFO)
script_name = os.path.splitext(os.path.basename(file_name))[0]
log_path = os.path.join(base_path, 'log')
if not os.path.exists(log_path):
os.makedirs(log_path)
fileHandler = logging.handlers.TimedRotatingFileHandler(
"%s/%s.log" % (log_path, script_name),
when='midnight',
backupCount=14)
fileHandler.suffix = "%Y-%m-%d"
fileFormatter = logging.Formatter(
'%(asctime)s %(name)-12s: %(levelname)-8s %(message)s')
fileHandler.setFormatter(fileFormatter)
fileHandler.setLevel(logging.INFO)
consoleHandler = logging.StreamHandler(sys.stdout)
consoleFormatter = logging.Formatter("%(levelname)s - %(message)s")
consoleHandler.setFormatter(consoleFormatter)
consoleHandler.setLevel(logging.WARNING)
logger.addHandler(fileHandler)
logger.addHandler(consoleHandler)
return logger
def get_session(env):
assert env in ['dev', 'stage', 'prod']
db_url = (
"mysql://{username}:{psswd}@{host}/{database}?charset=utf8"
.format(
host=config.get(env, 'host'),
username=config.get(env, 'username'),
psswd=config.get(env, 'psswd'),
database=config.get(env, 'database')))
engine = create_engine(db_url)
Session = sessionmaker(bind=engine)
return Session()
def timeit(logger=None):
def real_timeit(method):
@wraps(method)
def wrap(*args, **kwargs):
ts = time.time()
result = method(*args, **kwargs)
te = time.time()
msg = (
"%r (%r, %r) Spent %2.2f sec"
% (method.__name__, args, kwargs, te-ts))
if logger:
logger.info(msg)
return result
return wrap
return real_timeit