prediction-market-agent-tooling 0.56.2.dev129__py3-none-any.whl → 0.56.2.dev137__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ import hashlib
2
2
  import inspect
3
3
  import json
4
4
  from datetime import date, timedelta
5
- from functools import wraps
5
+ from functools import partial, wraps
6
6
  from typing import (
7
7
  Any,
8
8
  Callable,
@@ -15,13 +15,14 @@ from typing import (
15
15
  )
16
16
 
17
17
  from pydantic import BaseModel
18
- from sqlalchemy import Column, Engine
18
+ from sqlalchemy import Column
19
19
  from sqlalchemy.dialects.postgresql import JSONB
20
20
  from sqlmodel import Field, Session, SQLModel, create_engine, desc, select
21
21
 
22
22
  from prediction_market_agent_tooling.config import APIKeys
23
23
  from prediction_market_agent_tooling.loggers import logger
24
24
  from prediction_market_agent_tooling.tools.datetime_utc import DatetimeUTC
25
+ from prediction_market_agent_tooling.tools.pickle_utils import InitialiseNonPickable
25
26
  from prediction_market_agent_tooling.tools.utils import utcnow
26
27
 
27
28
  FunctionT = TypeVar("FunctionT", bound=Callable[..., Any])
@@ -40,9 +41,6 @@ class FunctionCache(SQLModel, table=True):
40
41
  created_at: DatetimeUTC = Field(default_factory=utcnow, index=True)
41
42
 
42
43
 
43
- DB_CACHE_ENGINE: None | Engine = None
44
-
45
-
46
44
  @overload
47
45
  def db_cache(
48
46
  func: None = None,
@@ -93,6 +91,18 @@ def db_cache(
93
91
  return decorator
94
92
 
95
93
  api_keys = api_keys if api_keys is not None else APIKeys()
94
+ wrapped_engine = InitialiseNonPickable(
95
+ partial(
96
+ create_engine,
97
+ api_keys.sqlalchemy_db_url.get_secret_value(),
98
+ # Use custom json serializer and deserializer, because otherwise, for example `datetime` serialization would fail.
99
+ json_serializer=json_serializer,
100
+ json_deserializer=json_deserializer,
101
+ )
102
+ )
103
+
104
+ if api_keys.ENABLE_CACHE:
105
+ SQLModel.metadata.create_all(wrapped_engine.get_value())
96
106
 
97
107
  @wraps(func)
98
108
  def wrapper(*args: Any, **kwargs: Any) -> Any:
@@ -100,16 +110,7 @@ def db_cache(
100
110
  if not api_keys.ENABLE_CACHE:
101
111
  return func(*args, **kwargs)
102
112
 
103
- global DB_CACHE_ENGINE
104
- if DB_CACHE_ENGINE is None:
105
- DB_CACHE_ENGINE = create_engine(
106
- api_keys.sqlalchemy_db_url.get_secret_value(),
107
- # Use custom json serializer and deserializer, because otherwise, for example `datetime` serialization would fail.
108
- json_serializer=json_serializer,
109
- json_deserializer=json_deserializer,
110
- )
111
- # Create table if it doesn't exist
112
- SQLModel.metadata.create_all(DB_CACHE_ENGINE)
113
+ engine = wrapped_engine.get_value()
113
114
 
114
115
  # Convert *args and **kwargs to a single dictionary, where we have names for arguments passed as args as well.
115
116
  signature = inspect.signature(func)
@@ -155,7 +156,7 @@ def db_cache(
155
156
  if return_type is not None and contains_pydantic_model(return_type):
156
157
  is_pydantic_model = True
157
158
 
158
- with Session(DB_CACHE_ENGINE) as session:
159
+ with Session(engine) as session:
159
160
  # Try to get cached result
160
161
  statement = (
161
162
  select(FunctionCache)
@@ -208,7 +209,7 @@ def db_cache(
208
209
  result=computed_result,
209
210
  created_at=utcnow(),
210
211
  )
211
- with Session(DB_CACHE_ENGINE) as session:
212
+ with Session(engine) as session:
212
213
  logger.info(f"Saving {cache_entry} into database.")
213
214
  session.add(cache_entry)
214
215
  session.commit()
@@ -0,0 +1,31 @@
1
+ import typing as t
2
+
3
+ InitialisedValue = t.TypeVar("InitialisedValue")
4
+
5
+
6
+ class InitialiseNonPickable(t.Generic[InitialisedValue]):
7
+ """
8
+ Use this class to wrap values that you want to be shared within a thread,
9
+ but they are re-initialised for a new processes.
10
+
11
+ Initialiser for the value still needs to be pickable.
12
+ """
13
+
14
+ def __init__(self, initialiser: t.Callable[[], InitialisedValue]) -> None:
15
+ self.value: InitialisedValue | None = None
16
+ self.initialiser = initialiser
17
+
18
+ def __getstate__(self) -> dict[str, t.Any]:
19
+ # During pickling, always return `value` as just None, which is pickable and this class will re-initialise it in `get_value` when called.
20
+ return {"value": None, "initialiser": self.initialiser}
21
+
22
+ def __setstate__(self, d: dict[str, t.Any]) -> None:
23
+ self.value = d["value"]
24
+ self.initialiser = d["initialiser"]
25
+
26
+ def get_value(self) -> InitialisedValue:
27
+ """Use this function to get the wrapped value, which will be initialised if necessary."""
28
+ if self.value is None:
29
+ self.value = self.initialiser()
30
+
31
+ return self.value
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: prediction-market-agent-tooling
3
- Version: 0.56.2.dev129
3
+ Version: 0.56.2.dev137
4
4
  Summary: Tools to benchmark, deploy and monitor prediction market agents.
5
5
  Author: Gnosis
6
6
  Requires-Python: >=3.10,<3.12
@@ -71,7 +71,7 @@ prediction_market_agent_tooling/tools/betting_strategies/market_moving.py,sha256
71
71
  prediction_market_agent_tooling/tools/betting_strategies/minimum_bet_to_win.py,sha256=-FUSuQQgjcWSSnoFxnlAyTeilY6raJABJVM2QKkFqAY,438
72
72
  prediction_market_agent_tooling/tools/betting_strategies/stretch_bet_between.py,sha256=THMXwFlskvzbjnX_OiYtDSzI8XVFyULWfP2525_9UGc,429
73
73
  prediction_market_agent_tooling/tools/betting_strategies/utils.py,sha256=kpIb-ci67Vc1Yqqaa-_S4OUkbhWSIYog4_Iwp69HU_k,97
74
- prediction_market_agent_tooling/tools/caches/db_cache.py,sha256=s8MKNWa3EFIRMZbi5YqgOd4S1boMPkUrGFgtW8qBNlw,12844
74
+ prediction_market_agent_tooling/tools/caches/db_cache.py,sha256=vKxNnAPm8zZBGRVPyNKiWaGvQxJx44RdZQ3CiWVDtoQ,12869
75
75
  prediction_market_agent_tooling/tools/caches/inmemory_cache.py,sha256=tGHHd9HCiE_hCCtPtloHZQdDfBuiow9YsqJNYi2Tx_0,499
76
76
  prediction_market_agent_tooling/tools/contract.py,sha256=s3yo8IbXTcvAJcPfLM0_NbgaEsWwLsPmyVnOgyjq_xI,20919
77
77
  prediction_market_agent_tooling/tools/costs.py,sha256=EaAJ7v9laD4VEV3d8B44M4u3_oEO_H16jRVCdoZ93Uw,954
@@ -89,6 +89,7 @@ prediction_market_agent_tooling/tools/langfuse_.py,sha256=jI_4ROxqo41CCnWGS1vN_A
89
89
  prediction_market_agent_tooling/tools/langfuse_client_utils.py,sha256=B0PhAQyviFnVbtOCYMxYmcCn66cu9nbqAOIAZcdgiRI,5771
90
90
  prediction_market_agent_tooling/tools/omen/reality_accuracy.py,sha256=M1SF7iSW1gVlQSTskdVFTn09uPLST23YeipVIWj54io,2236
91
91
  prediction_market_agent_tooling/tools/parallelism.py,sha256=6Gou0hbjtMZrYvxjTDFUDZuxmE2nqZVbb6hkg1hF82A,1022
92
+ prediction_market_agent_tooling/tools/pickle_utils.py,sha256=PUNRJGdURUnLlzbZoSCiOrikRbEC596hi--Z3q8Wt84,1144
92
93
  prediction_market_agent_tooling/tools/relevant_news_analysis/data_models.py,sha256=95l84aztFaxcRLLcRQ46yKJbIlOEuDAbIGLouyliDzA,1316
93
94
  prediction_market_agent_tooling/tools/relevant_news_analysis/relevant_news_analysis.py,sha256=CddJem7tk15NAudJDwmuL8znTycbR-YI8kTNtd3LzG8,5474
94
95
  prediction_market_agent_tooling/tools/relevant_news_analysis/relevant_news_cache.py,sha256=2yxtBIDyMT_6CsTpZyuIv_2dy2B9WgEOaTT1fSloBu0,3223
@@ -99,8 +100,8 @@ prediction_market_agent_tooling/tools/tavily/tavily_models.py,sha256=5ldQs1pZe6u
99
100
  prediction_market_agent_tooling/tools/tavily/tavily_search.py,sha256=Kw2mXNkMTYTEe1MBSTqhQmLoeXtgb6CkmHlcAJvhtqE,3809
100
101
  prediction_market_agent_tooling/tools/utils.py,sha256=1VvunbTmzGzpIlRukFhArreFNxJPbsg4lLtQNk0r2bY,7185
101
102
  prediction_market_agent_tooling/tools/web3_utils.py,sha256=44W8siSLNQxeib98bbwAe7V5C609NHNlUuxwuWIRDiY,11838
102
- prediction_market_agent_tooling-0.56.2.dev129.dist-info/LICENSE,sha256=6or154nLLU6bELzjh0mCreFjt0m2v72zLi3yHE0QbeE,7650
103
- prediction_market_agent_tooling-0.56.2.dev129.dist-info/METADATA,sha256=uWLV8rw76xcE9yFjv1CYRp3mpM_HbYBFho1qpIyeoCc,8113
104
- prediction_market_agent_tooling-0.56.2.dev129.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
105
- prediction_market_agent_tooling-0.56.2.dev129.dist-info/entry_points.txt,sha256=m8PukHbeH5g0IAAmOf_1Ahm-sGAMdhSSRQmwtpmi2s8,81
106
- prediction_market_agent_tooling-0.56.2.dev129.dist-info/RECORD,,
103
+ prediction_market_agent_tooling-0.56.2.dev137.dist-info/LICENSE,sha256=6or154nLLU6bELzjh0mCreFjt0m2v72zLi3yHE0QbeE,7650
104
+ prediction_market_agent_tooling-0.56.2.dev137.dist-info/METADATA,sha256=wmx1u8Ys3t_JqAl_9TYjNAR3NWxLYJhqqwBXF539yTY,8113
105
+ prediction_market_agent_tooling-0.56.2.dev137.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
106
+ prediction_market_agent_tooling-0.56.2.dev137.dist-info/entry_points.txt,sha256=m8PukHbeH5g0IAAmOf_1Ahm-sGAMdhSSRQmwtpmi2s8,81
107
+ prediction_market_agent_tooling-0.56.2.dev137.dist-info/RECORD,,