logicsame commited on
Commit
f4b830d
·
1 Parent(s): 9e64993

utils added

Browse files
src/benglasummarization/constants/__init__.py CHANGED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ CONFIG_FILE_PATH = Path('config/config.yaml')
4
+ PARAMS_FILE_PATH = Path('params.yaml')
src/benglasummarization/logging/__init__.py CHANGED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import logging
4
+
5
+ logging_str = "[%(asctime)s: %(levelname)s: %(module)s: %(message)s]"
6
+ log_dir = "logs"
7
+ log_filepath = os.path.join(log_dir,"running_logs.log")
8
+ os.makedirs(log_dir, exist_ok=True)
9
+
10
+
11
+
12
+ logging.basicConfig(
13
+ level= logging.INFO,
14
+ format= logging_str,
15
+
16
+ handlers=[
17
+ logging.FileHandler(log_filepath),
18
+ logging.StreamHandler(sys.stdout)
19
+ ]
20
+ )
21
+
22
+ logger = logging.getLogger("BengLasummarizationLogger")
src/benglasummarization/utils/common.py CHANGED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from box.exceptions import BoxValueError
3
+ import yaml
4
+ from src.benglasummarization.logging import logger
5
+ from ensure import ensure_annotations
6
+ from box import ConfigBox
7
+ from pathlib import Path
8
+ from typing import Any
9
+ import json
10
+
11
+
12
+ @ensure_annotations
13
+ def read_yaml(path_to_yaml: Path) -> ConfigBox:
14
+ """reads yaml file and returns
15
+
16
+ Args:
17
+ path_to_yaml (str): path like input
18
+
19
+ Raises:
20
+ ValueError: if yaml file is empty
21
+ e: empty file
22
+
23
+ Returns:
24
+ ConfigBox: ConfigBox type
25
+ """
26
+ try:
27
+ with open(path_to_yaml) as yaml_file:
28
+ content = yaml.safe_load(yaml_file)
29
+ logger.info(f"yaml file: {path_to_yaml} loaded successfully")
30
+ return ConfigBox(content)
31
+ except BoxValueError:
32
+ raise ValueError("yaml file is empty")
33
+ except Exception as e:
34
+ raise e
35
+
36
+
37
+ @ensure_annotations
38
+ def create_directories(path_to_directories: list, verbose=True):
39
+ """create list of directories
40
+
41
+ Args:
42
+ path_to_directories (list): list of path of directories
43
+ ignore_log (bool, optional): ignore if multiple dirs is to be created. Defaults to False.
44
+ """
45
+ for path in path_to_directories:
46
+ os.makedirs(path, exist_ok=True)
47
+ if verbose:
48
+ logger.info(f"created directory at: {path}")
49
+
50
+
51
+ @ensure_annotations
52
+ def save_json(path: Path, data: dict):
53
+ """save json data
54
+
55
+ Args:
56
+ path (Path): path to json file
57
+ data (dict): data to be saved in json file
58
+ """
59
+
60
+
61
+ with open(path, 'w') as f:
62
+ json.dump(data, f, indent=4)
63
+
64
+ logger.info(f'Json file saved at: {path}')
65
+
66
+
67
+ @ensure_annotations
68
+ def load_json(path: Path) -> ConfigBox:
69
+ """load json files data
70
+
71
+ Args:
72
+ path (Path): path to json file
73
+
74
+ Returns:
75
+ ConfigBox: data as class attributes instead of dict
76
+ """
77
+
78
+ with open(path, 'r') as f:
79
+ content = json.load(f)
80
+
81
+ logger.info(f"Json file loaded successfully from: {path}")
82
+ return ConfigBox
83
+