mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-08-05 03:07:42 +00:00
eee2b3c56a
* remove ruff comment in log.py * change log framework and fix llm_utils.py's logs * Some thoughts for logging * fix SingletonMeta's definition, maintain an instance dict for each class that inherits it * adjust log codes directory, add some tag for factor implementation logging * Update rdagent/core/conf.py * fix factor task app & log * fix log import * Streamlet framework * fix log tag to path logic * Add todos * Add example in docstring * add log tag for llm_utils.py * Capture lost content --------- Co-authored-by: Young <afe.young@gmail.com> Co-authored-by: you-n-g <you-n-g@users.noreply.github.com>
55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
import json
|
|
import pickle
|
|
from typing import Literal
|
|
|
|
from pathlib import Path
|
|
from datetime import datetime, timezone
|
|
from .base import Storage
|
|
|
|
class FileStorage(Storage):
|
|
"""
|
|
The info are logginged to the file systems
|
|
|
|
TODO: describe the storage format
|
|
"""
|
|
|
|
def __init__(self, path: str = "./log/") -> None:
|
|
self.path = Path(path)
|
|
self.path.mkdir(parents=True, exist_ok=True)
|
|
|
|
def log(self,
|
|
obj: object,
|
|
name: str = "",
|
|
save_type: Literal["json", "text", "pkl"] = "text",
|
|
timestamp: datetime | None = None,
|
|
) -> Path:
|
|
# TODO: We can remove the timestamp after we implement PipeLog
|
|
if timestamp is None:
|
|
timestamp = datetime.now(timezone.utc)
|
|
else:
|
|
timestamp = timestamp.astimezone(timezone.utc)
|
|
|
|
cur_p = self.path / name.replace(".", "/")
|
|
cur_p.mkdir(parents=True, exist_ok=True)
|
|
|
|
path = cur_p / f"{timestamp.strftime('%Y-%m-%d_%H-%M-%S-%f')}.log"
|
|
|
|
if save_type == "json":
|
|
path = path.with_suffix(".json")
|
|
with path.open("w") as f:
|
|
try:
|
|
json.dump(obj, f)
|
|
except TypeError:
|
|
json.dump(json.loads(str(obj)), f)
|
|
return path
|
|
elif save_type == "pkl":
|
|
path = path.with_suffix(".pkl")
|
|
with path.open("wb") as f:
|
|
pickle.dump(obj, f)
|
|
return path
|
|
elif save_type == "text":
|
|
obj = str(obj)
|
|
with path.open("w") as f:
|
|
f.write(obj)
|
|
return path
|