12 fix docs (#13)

* fix dependencies uv.lock

* refactor test markers for clarity

* refactor: clean up imports and remove unused files

* refactor: remove unused agent files and clean up market API instructions

* refactor: enhance wrapper initialization with keyword arguments and clean up tests

* refactor: remove PublicBinanceAgent

* refactor: aggregator
- simplified MarketDataAggregator and related models to functions

* refactor: update README and .env.example to reflect the latest changes to the project

* refactor: simplify product info and price creation in YFinanceWrapper

* refactor: remove get_all_products method from market API wrappers and update documentation

* fix: environment variable assertions

* refactor: remove status attribute from ProductInfo and update related methods to use timestamp_ms

* feat: implement aggregate_history_prices function to calculate hourly price averages

* refactor: update docker-compose and app.py for improved environment variable handling and compatibility

* feat: add detailed market instructions and improve error handling in price aggregation methods

* feat: add aggregated news retrieval methods for top headlines and latest news

* refactor: improve error messages in WrapperHandler for better clarity

* fix: correct quote currency extraction in create_product_info and remove debug prints from tests
This commit was merged in pull request #13.
This commit is contained in:
Giacomo Bertolazzi
2025-10-02 01:40:59 +02:00
committed by GitHub
parent a8755913d8
commit d2fbc0ceea
41 changed files with 726 additions and 1553 deletions

View File

@@ -1,3 +1,4 @@
import inspect
import time
import traceback
from typing import TypeVar, Callable, Generic, Iterable, Type
@@ -45,17 +46,24 @@ class WrapperHandler(Generic[W]):
Raises:
Exception: If all wrappers fail after retries.
"""
log_info(f"{inspect.getsource(func).strip()} {inspect.getclosurevars(func).nonlocals}")
iterations = 0
while iterations < len(self.wrappers):
wrapper = self.wrappers[self.index]
wrapper_name = wrapper.__class__.__name__
try:
wrapper = self.wrappers[self.index]
log_info(f"Trying wrapper: {wrapper} - function {func}")
log_info(f"try_call {wrapper_name}")
result = func(wrapper)
log_info(f"{wrapper_name} succeeded")
self.retry_count = 0
return result
except Exception as e:
self.retry_count += 1
log_warning(f"{wrapper} failed {self.retry_count}/{self.retry_per_wrapper}: {WrapperHandler.__concise_error(e)}")
error = WrapperHandler.__concise_error(e)
log_warning(f"{wrapper_name} failed {self.retry_count}/{self.retry_per_wrapper}: {error}")
if self.retry_count >= self.retry_per_wrapper:
self.index = (self.index + 1) % len(self.wrappers)
@@ -64,7 +72,7 @@ class WrapperHandler(Generic[W]):
else:
time.sleep(self.retry_delay)
raise Exception(f"All wrappers failed after retries")
raise Exception(f"All wrappers failed, latest error: {error}")
def try_call_all(self, func: Callable[[W], T]) -> dict[str, T]:
"""
@@ -78,28 +86,33 @@ class WrapperHandler(Generic[W]):
Raises:
Exception: If all wrappers fail.
"""
log_info(f"{inspect.getsource(func).strip()} {inspect.getclosurevars(func).nonlocals}")
results = {}
log_info(f"All wrappers: {[wrapper.__class__ for wrapper in self.wrappers]} - function {func}")
for wrapper in self.wrappers:
wrapper_name = wrapper.__class__.__name__
try:
result = func(wrapper)
log_info(f"{wrapper_name} succeeded")
results[wrapper.__class__] = result
except Exception as e:
log_warning(f"{wrapper} failed: {WrapperHandler.__concise_error(e)}")
error = WrapperHandler.__concise_error(e)
log_warning(f"{wrapper_name} failed: {error}")
if not results:
raise Exception("All wrappers failed")
raise Exception(f"All wrappers failed, latest error: {error}")
return results
@staticmethod
def __check(wrappers: list[W]) -> bool:
return all(w.__class__ is type for w in wrappers)
@staticmethod
def __concise_error(e: Exception) -> str:
last_frame = traceback.extract_tb(e.__traceback__)[-1]
return f"{e} [\"{last_frame.filename}\", line {last_frame.lineno}]"
@staticmethod
def build_wrappers(constructors: Iterable[Type[W]], try_per_wrapper: int = 3, retry_delay: int = 2) -> 'WrapperHandler[W]':
def build_wrappers(constructors: Iterable[Type[W]], try_per_wrapper: int = 3, retry_delay: int = 2, kwargs: dict | None = None) -> 'WrapperHandler[W]':
"""
Builds a WrapperHandler instance with the given wrapper constructors.
It attempts to initialize each wrapper and logs a warning if any cannot be initialized.
@@ -108,6 +121,7 @@ class WrapperHandler(Generic[W]):
constructors (Iterable[Type[W]]): An iterable of wrapper classes to instantiate. e.g. [WrapperA, WrapperB]
try_per_wrapper (int): Number of retries per wrapper before switching to the next.
retry_delay (int): Delay in seconds between retries.
kwargs (dict | None): Optional dictionary with keyword arguments common to all wrappers.
Returns:
WrapperHandler[W]: An instance of WrapperHandler with the initialized wrappers.
Raises:
@@ -118,7 +132,7 @@ class WrapperHandler(Generic[W]):
result = []
for wrapper_class in constructors:
try:
wrapper = wrapper_class()
wrapper = wrapper_class(**(kwargs or {}))
result.append(wrapper)
except Exception as e:
log_warning(f"{wrapper_class} cannot be initialized: {e}")