Spaces:
Sleeping
Sleeping
File size: 3,479 Bytes
c28ff66 | 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | import os
from box.exceptions import BoxValueError
import yaml
from src.utils.logger import logger
import json
import joblib
from ensure import ensure_annotations
from box import ConfigBox # convert yaml thành object class
from pathlib import Path
from typing import Any
@ensure_annotations # decorator để ép truyền đúng kiểu dữ liệu
def read_yaml(path_to_yaml: Path) -> ConfigBox:
"""Đọc file yaml và trả về ConfigBox.
Args:
path_to_yaml (Path): Đường dẫn tới file yaml.
Raises:
ValueError: Nếu tệp yaml trống.
e: Lỗi nếu file lỗi.
Returns:
ConfigBox: Đối tượng ConfigBox chứa cấu hình.
"""
try:
with open(path_to_yaml, encoding="utf-8") as yaml_file:
content = yaml.safe_load(yaml_file)
logger.info(f"Đọc file yaml thành công: {path_to_yaml}")
if content is None:
return ConfigBox({})
return ConfigBox(content)
except Exception as e:
logger.error(f"Lỗi khi đọc file yaml {path_to_yaml}: {e}")
raise e
@ensure_annotations
def create_directories(path_to_directories: list, verbose=True):
"""Tạo ra list các thư mục.
Args:
path_to_directories (list): Danh sách đường dẫn của các thư mục cần tạo.
verbose (bool, tùy chọn): In log khi tạo thư mục. Mặc định là True.
"""
for path in path_to_directories:
os.makedirs(path, exist_ok=True)
if verbose:
logger.info(f"Đã tạo thư mục tại: {path}")
@ensure_annotations
def save_json(path: Path, data: dict):
"""Lưu dữ liệu dưới dạng tệp JSON.
Args :
path (Path): Đường dẫn tới tệp JSON.
data (dict): Dữ liệu sẽ được lưu vào tệp JSON.
"""
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
logger.info(f"Đã lưu file JSON tại: {path}")
@ensure_annotations
def load_json(path: Path) -> ConfigBox:
"""Tải data từ file JSON.
Args:
path (Path): Đường dẫn tới tệp JSON.
Returns:
ConfigBox: Data ở dạng class attributes thay vì dict.
"""
with open(path, encoding="utf-8") as f:
content = json.load(f)
logger.info(f"Đã tải file JSON thành công từ: {path}")
return ConfigBox(content)
@ensure_annotations
def save_bin(data: Any, path: Path):
"""Lưu tệp nhị phân (binary file).
Args:
data (Any): Dữ liệu sẽ được lưu dưới dạng nhị phân.
path (Path): Đường dẫn tới tệp nhị phân.
"""
joblib.dump(value=data, filename=path)
logger.info(f"Đã lưu file nhị phân tại: {path}")
@ensure_annotations
def load_bin(path: Path) -> Any:
"""Tải dữ liệu nhị phân.
Args:
path (Path): Đường dẫn tới tệp nhị phân.
Returns:
Any: Đối tượng được lưu trong file.
"""
data = joblib.load(path)
logger.info(f"Đã tải dữ liệu từ file nhị phân: {path}")
return data
@ensure_annotations
def get_size(path: Path) -> str:
"""Lấy kích thước tệp theo KB.
Args:
path (Path): Đường dẫn tới tệp.
Returns:
str: Kích thước của tệp tính bằng KB.
"""
size_in_kb = round(os.path.getsize(path) / 1024)
return f"~ {size_in_kb} KB" |