forked from kakao-arena/shopping-classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
72 lines (59 loc) · 2.22 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
# -*- coding: utf-8 -*-
# Copyright 2017 Kakao, Recommendation Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def get_logger(name=__file__):
import logging
import logging.handlers
logger = logging.getLogger(name)
if logger.handlers:
return logger
logger.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(levelname)-8s] %(asctime)s [%(filename)s] [%(funcName)s:%(lineno)d] %(message)s', '%Y-%m-%d %H:%M:%S')
sh.setFormatter(formatter)
logger.addHandler(sh)
return logger
class Option(dict):
def __init__(self, *args, **kwargs):
import json
import six
args = [arg if isinstance(arg, dict) else json.loads(open(arg).read())
for arg in args]
super(Option, self).__init__(*args, **kwargs)
for arg in args:
if isinstance(arg, dict):
for k, v in six.iteritems(arg):
if isinstance(v, dict):
self[k] = Option(v)
else:
self[k] = v
if kwargs:
for k, v in six.iteritems(kwargs):
if isinstance(v, dict):
self[k] = Option(v)
else:
self[k] = v
def __getattr__(self, attr):
return self.get(attr)
def __setattr__(self, key, value):
self.__setitem__(key, value)
def __setitem__(self, key, value):
super(Option, self).__setitem__(key, value)
self.__dict__.update({key: value})
def __delattr__(self, item):
self.__delitem__(item)
def __delitem__(self, key):
super(Option, self).__delitem__(key)
del self.__dict__[key]