Spaces:
Runtime error
Runtime error
| # utils/helpers.py | |
| """์ ํธ๋ฆฌํฐ ํจ์๋ค""" | |
| import os | |
| from typing import Optional | |
| def ensure_dir(directory: str): | |
| """ | |
| ๋๋ ํ ๋ฆฌ๊ฐ ์์ผ๋ฉด ์์ฑ | |
| Args: | |
| directory: ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก | |
| """ | |
| if not os.path.exists(directory): | |
| os.makedirs(directory) | |
| print(f"๐ ๋๋ ํ ๋ฆฌ ์์ฑ: {directory}") | |
| def format_file_size(size_bytes: int) -> str: | |
| """ | |
| ํ์ผ ํฌ๊ธฐ๋ฅผ ์ฝ๊ธฐ ์ฌ์ด ํ์์ผ๋ก ๋ณํ | |
| Args: | |
| size_bytes: ๋ฐ์ดํธ ๋จ์ ํฌ๊ธฐ | |
| Returns: | |
| str: ํฌ๋งท๋ ํฌ๊ธฐ (์: "1.5 MB") | |
| """ | |
| for unit in ['B', 'KB', 'MB', 'GB']: | |
| if size_bytes < 1024.0: | |
| return f"{size_bytes:.1f} {unit}" | |
| size_bytes /= 1024.0 | |
| return f"{size_bytes:.1f} TB" | |
| def truncate_text(text: str, max_length: int = 100, suffix: str = "...") -> str: | |
| """ | |
| ํ ์คํธ๋ฅผ ์ง์ ๋ ๊ธธ์ด๋ก ์๋ฅด๊ธฐ | |
| Args: | |
| text: ์๋ณธ ํ ์คํธ | |
| max_length: ์ต๋ ๊ธธ์ด | |
| suffix: ์๋ฆฐ ๊ฒฝ์ฐ ์ถ๊ฐํ ์ ๋ฏธ์ฌ | |
| Returns: | |
| str: ์๋ฆฐ ํ ์คํธ | |
| """ | |
| if len(text) <= max_length: | |
| return text | |
| return text[:max_length - len(suffix)] + suffix | |
| def safe_get(dictionary: dict, key: str, default: Optional[any] = None): | |
| """ | |
| ์์ ํ๊ฒ ๋์ ๋๋ฆฌ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ | |
| Args: | |
| dictionary: ๋์ ๋๋ฆฌ | |
| key: ํค | |
| default: ๊ธฐ๋ณธ๊ฐ | |
| Returns: | |
| ๊ฐ ๋๋ ๊ธฐ๋ณธ๊ฐ | |
| """ | |
| try: | |
| return dictionary.get(key, default) | |
| except: | |
| return default | |