Spaces:
Sleeping
Sleeping
Download src/utils/common.py from Ducdata1808/customer-churn-backend: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/spaces/Ducdata1808/customer-churn-backend/resolve/main/src/utils/common.py
- Command line
-
hf download hf://spaces/Ducdata1808/customer-churn-backend/src/utils/common.py
-
curl -L -o common.py https://huggingface.co/spaces/Ducdata1808/customer-churn-backend/resolve/main/src/utils/common.py
3.48 kB
| 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 | |
| # 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 | |
| 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}") | |
| 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}") | |
| 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) | |
| 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}") | |
| 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 | |
| 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" |