File size: 2,344 Bytes
79e12fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from functools import wraps
import logging
import os
from time import time
import configparser

timer_functions = {}

# Loading configuration from config file
config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'config.ini'))


def timing(f):
    @wraps(f)
    def wrap(*args, **kw):
        ts = time()
        result = f(*args, **kw)
        te = time()
        if f.__name__ in timer_functions.keys():
            current_elapsed_time = timer_functions[f.__name__]
        else:
            current_elapsed_time = 0
        timer_functions[f.__name__] = current_elapsed_time + (te - ts)
        logging.debug('func:%r took: %2.4f sec' % \
                        (f.__name__, te - ts))
        return result
    return wrap


def f_read_config(path=None):
    """ read config file from specified file path
    
    :param path: file path
    :return: configparser object
    """
    # Loading configuration from config file
    config = configparser.ConfigParser()
    if path is None:
        path = os.path.join(os.path.dirname(__file__), 'config.ini')
    config.read(path, encoding='utf-8')
    return config

def f_setup_logger(level_sysout=logging.INFO, level_file=logging.DEBUG, folder_path="logs"):
    """Setup logger

    By default we display only INFO in console, and write everything in file

    Args:
        level_sysout: Level that is displayed in console (default INFO)
        level_file: Level that is written in file (default DEBUG)

    Returns:
        Nothing

    """
    if not os.path.isdir(folder_path):
        os.mkdir(folder_path)

    for handler in logging.root.handlers[:]:
        logging.root.removeHandler(handler)

    file_handler = logging.FileHandler(filename=os.path.join(folder_path, "amf_uce_nlp_{}.log".format(time())),
                                       encoding='utf-8')
    sysout_handler = logging.StreamHandler()
    file_handler.setLevel(level_file)
    sysout_handler.setLevel(level_sysout)
    logging.basicConfig(handlers=[file_handler, sysout_handler], level=logging.DEBUG,
                        format='%(asctime)s (%(levelname)s) %(message)s', datefmt='%m/%d/%y %I:%M:%S %p')


def get_model_full_path(model_name):
    path_models = config["DEFAULT"]["path_models"]
    return os.path.join(os.path.dirname(__file__), path_models, model_name)