-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutil.py
32 lines (26 loc) · 863 Bytes
/
util.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
import logging
import json
from urllib import request
def head(list):
return list[0] if list else None
def make_request(url, **kwargs):
req = request.Request(url=url, **kwargs)
response = request.urlopen(req)
data = response.read().decode("utf-8")
result = json.loads(data)
code = result.get("errcode")
message = result.get("errmsg")
if code and code != 0:
raise ValueError(message)
return result
def getLogger(name="main", level=logging.INFO):
logger = logging.getLogger(name)
fmt = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s")
logger.setLevel(level)
# stop multiple log
logger.propagate = False
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(fmt)
stream_handler.setLevel(level)
logger.addHandler(stream_handler)
return logger