From a6200d2809d42878dbbfd741b9c8c60780f69a2a Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Mon, 15 Sep 2025 13:11:31 +0900 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AClib=E3=82=92UP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +++- README.md | 11 +++++++- requirements.txt | 20 ++++++++++++++ src/lib/custom_logger.py | 56 ++++++++++++++++++++++++++++++++++++++++ src/lib/singleton.py | 20 ++++++++++++++ 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 requirements.txt create mode 100644 src/lib/custom_logger.py create mode 100644 src/lib/singleton.py diff --git a/.gitignore b/.gitignore index 0dbf2f2..9c2641a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +data/ +!.gitkeep + # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ @@ -15,7 +18,7 @@ dist/ downloads/ eggs/ .eggs/ -lib/ +# lib/ lib64/ parts/ sdist/ diff --git a/README.md b/README.md index 957f581..803771e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ # colect-data-pipe-line -データ収集と蓄積までの検討及び調査 \ No newline at end of file +データ収集と蓄積までの検討及び調査 + +## Develop + +```sh +python -m venv venv +# windows +venv\Scripts\activate +pip install -r requirements.txt +``` \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fb4ee6e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,20 @@ +duckdb==1.3.2 +numpy==2.3.3 +pandas==2.3.2 + +python-dotenv + +# RSS +feedparser==6.0.12 +# model +pydantic==2.11.9 + +# deepl +deepl + +# transelate argo +#argostranslate + +# transformers +# sentencepiece +# torch \ No newline at end of file diff --git a/src/lib/custom_logger.py b/src/lib/custom_logger.py new file mode 100644 index 0000000..9137b2f --- /dev/null +++ b/src/lib/custom_logger.py @@ -0,0 +1,56 @@ +import logging +import functools +from .singleton import Singleton + +class CustomLogger(Singleton): + """ + Singleton logger class that initializes a logger with a specified name and log file. + It provides a method to log entry and exit of functions. + """ + + def __init__(self, name='main', log_file=None, level=logging.INFO): + if hasattr(self, '_initialized') and self._initialized: + return # すでに初期化済みなら何もしない + # self.logger.setLevel(level) + + self.logger = logging.getLogger(name) + self.logger.setLevel(level) + self.logger.propagate = False + + formatter = logging.Formatter( + '%(asctime)s %(levelname)s [%(filename)s:%(lineno)3d]: %(message)s' + ) + + # Console handler + ch = logging.StreamHandler() + ch.setFormatter(formatter) + self.logger.addHandler(ch) + + # File handler + if log_file: + fh = logging.FileHandler(log_file, encoding='utf-8') + fh.setFormatter(formatter) + self.logger.addHandler(fh) + + self._initialized = True + + + def get_logger(self): + return self.logger + + def log_entry_exit(self, func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + self.logger.info(f"Enter: {func.__qualname__}") + result = func(*args, **kwargs) + self.logger.info(f"Exit: {func.__qualname__}") + return result + return wrapper + + + + + +def get_logger(name='main', log_file=None, level=logging.INFO): + custom_logger = CustomLogger(name, log_file, level) + return custom_logger.get_logger() diff --git a/src/lib/singleton.py b/src/lib/singleton.py new file mode 100644 index 0000000..cc8cb16 --- /dev/null +++ b/src/lib/singleton.py @@ -0,0 +1,20 @@ +"""Singleton pattern implementation in Python. +This implementation is thread-safe and ensures that only one instance of the class is created. + +Singleton が提供するのは「同じインスタンスを返す仕組み」 +* __init__() は毎回呼ばれる(多くの人が意図しない動作) +* __init__の2回目は_initialized というフラグは 使う側で管理する必要がある。 +""" + +import threading + +class Singleton(object): + _instances = {} + _lock = threading.Lock() + + def __new__(cls, *args, **kwargs): + if cls not in cls._instances: + with cls._lock: + if cls not in cls._instances: # ダブルチェック + cls._instances[cls] = super(Singleton, cls).__new__(cls) + return cls._instances[cls]