recapi 0.4.7__tar.gz → 0.4.8__tar.gz
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.
- {recapi-0.4.7/recapi.egg-info → recapi-0.4.8}/PKG-INFO +2 -2
- {recapi-0.4.7 → recapi-0.4.8}/README.md +1 -1
- {recapi-0.4.7 → recapi-0.4.8}/pyproject.toml +1 -1
- {recapi-0.4.7 → recapi-0.4.8}/recapi/client.py +1 -1
- {recapi-0.4.7 → recapi-0.4.8}/recapi/history.py +20 -7
- {recapi-0.4.7 → recapi-0.4.8/recapi.egg-info}/PKG-INFO +2 -2
- {recapi-0.4.7 → recapi-0.4.8}/tests/test_client_vectors.py +32 -0
- {recapi-0.4.7 → recapi-0.4.8}/LICENSE +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/recapi/__init__.py +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/recapi/async_client.py +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/recapi.egg-info/SOURCES.txt +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/recapi.egg-info/dependency_links.txt +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/recapi.egg-info/top_level.txt +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/setup.cfg +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/tests/test_async_client.py +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/tests/test_client.py +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/tests/test_history.py +0 -0
- {recapi-0.4.7 → recapi-0.4.8}/tests/test_stores.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: recapi
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.8
|
|
4
4
|
Summary: Python client for the recapi recommendation API: per-user history, event batching, retries, popular fallback.
|
|
5
5
|
License: MIT
|
|
6
6
|
Requires-Python: >=3.11
|
|
@@ -37,7 +37,7 @@ Your recapi API serves the same library, for servers that can't reach PyPI
|
|
|
37
37
|
(the dashboard's quick start shows the line with your API's address):
|
|
38
38
|
|
|
39
39
|
```sh
|
|
40
|
-
pip install https://api.<your-domain>/sdk/python/recapi-0.4.
|
|
40
|
+
pip install https://api.<your-domain>/sdk/python/recapi-0.4.8-py3-none-any.whl
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
To work on the library itself, install it from a checkout of this repository
|
|
@@ -27,7 +27,7 @@ Your recapi API serves the same library, for servers that can't reach PyPI
|
|
|
27
27
|
(the dashboard's quick start shows the line with your API's address):
|
|
28
28
|
|
|
29
29
|
```sh
|
|
30
|
-
pip install https://api.<your-domain>/sdk/python/recapi-0.4.
|
|
30
|
+
pip install https://api.<your-domain>/sdk/python/recapi-0.4.8-py3-none-any.whl
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
To work on the library itself, install it from a checkout of this repository
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "recapi"
|
|
7
|
-
version = "0.4.
|
|
7
|
+
version = "0.4.8"
|
|
8
8
|
description = "Python client for the recapi recommendation API: per-user history, event batching, retries, popular fallback."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -17,6 +17,8 @@ TOP_K = 50
|
|
|
17
17
|
RECENT_SIZE = 20
|
|
18
18
|
REJECTED_SIZE = 200
|
|
19
19
|
MAX_ID_BYTES = 256
|
|
20
|
+
# The API answers 400 above this, which fails the list: a damaged score is sent as this.
|
|
21
|
+
MAX_REQUEST_WEIGHT = 1e6
|
|
20
22
|
|
|
21
23
|
_RATE = math.log(2) / HALF_LIFE_SECONDS
|
|
22
24
|
# Go's unicode.IsControl, which the API applies to every id: C0, DEL and C1.
|
|
@@ -112,8 +114,12 @@ class History:
|
|
|
112
114
|
self._prune_rejected()
|
|
113
115
|
|
|
114
116
|
def ranked(self, now: float) -> list[tuple[str, float, float]]:
|
|
115
|
-
"""Return (item, weight at ``now``, t) by weight desc, ties by item id asc.
|
|
116
|
-
|
|
117
|
+
"""Return (item, weight at ``now``, t) by weight desc, ties by item id asc.
|
|
118
|
+
|
|
119
|
+
A time ahead of ``now`` reads as ``now``: a clock that ran ahead must not
|
|
120
|
+
grow the weight past what the API takes, or overflow.
|
|
121
|
+
"""
|
|
122
|
+
entries = [(item, s * _decay(max(0, now - t)), t) for item, (s, t) in self.top.items()]
|
|
117
123
|
entries.sort(key=lambda e: (-e[1], e[0]))
|
|
118
124
|
return entries
|
|
119
125
|
|
|
@@ -123,7 +129,10 @@ class History:
|
|
|
123
129
|
|
|
124
130
|
def _prune(self, now: float) -> None:
|
|
125
131
|
if len(self.top) > TOP_K:
|
|
126
|
-
|
|
132
|
+
# Read at the newest entry when an event arrives out of order: at the old
|
|
133
|
+
# event's time ranked() would flatten the newer entries to their score.
|
|
134
|
+
at = max(now, *(t for _, t in self.top.values()))
|
|
135
|
+
self.top = {item: self.top[item] for item, _, _ in self.ranked(at)[:TOP_K]}
|
|
127
136
|
|
|
128
137
|
def _prune_rejected(self) -> None:
|
|
129
138
|
if len(self.rejected) > REJECTED_SIZE:
|
|
@@ -133,12 +142,16 @@ class History:
|
|
|
133
142
|
def wire(self, now: float) -> tuple[list[list[Any]], list[str], list[str]]:
|
|
134
143
|
"""Return the request's ``history`` and ``recent``, and the rejected items.
|
|
135
144
|
|
|
136
|
-
Weights are decayed to ``now
|
|
137
|
-
JS ``Math.round`` (Python's ``round`` rounds half
|
|
138
|
-
whole seconds, never negative.
|
|
145
|
+
Weights are decayed to ``now``, at most what the API accepts, and rounded
|
|
146
|
+
half up to 4 decimals like JS ``Math.round`` (Python's ``round`` rounds half
|
|
147
|
+
to even); ages are whole seconds, never negative.
|
|
139
148
|
"""
|
|
140
149
|
history = [
|
|
141
|
-
[
|
|
150
|
+
[
|
|
151
|
+
item,
|
|
152
|
+
math.floor(min(weight, MAX_REQUEST_WEIGHT) * 1e4 + 0.5) / 1e4,
|
|
153
|
+
max(0, math.floor((now - t) / 1000)),
|
|
154
|
+
]
|
|
142
155
|
for item, weight, t in self.ranked(now)[:TOP_K]
|
|
143
156
|
]
|
|
144
157
|
recent = [item for item, _ in self.recent[-RECENT_SIZE:]]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: recapi
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.8
|
|
4
4
|
Summary: Python client for the recapi recommendation API: per-user history, event batching, retries, popular fallback.
|
|
5
5
|
License: MIT
|
|
6
6
|
Requires-Python: >=3.11
|
|
@@ -37,7 +37,7 @@ Your recapi API serves the same library, for servers that can't reach PyPI
|
|
|
37
37
|
(the dashboard's quick start shows the line with your API's address):
|
|
38
38
|
|
|
39
39
|
```sh
|
|
40
|
-
pip install https://api.<your-domain>/sdk/python/recapi-0.4.
|
|
40
|
+
pip install https://api.<your-domain>/sdk/python/recapi-0.4.8-py3-none-any.whl
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
To work on the library itself, install it from a checkout of this repository
|
|
@@ -329,6 +329,38 @@ class ClientVectors(APITest):
|
|
|
329
329
|
sent = [e.get("recommendation_id") for b in self.api.events() for e in b]
|
|
330
330
|
self.assertEqual(sent, [case["attributed"], kept[1]])
|
|
331
331
|
|
|
332
|
+
def test_a_stored_time_ahead_or_a_huge_weight_keeps_the_list(self) -> None:
|
|
333
|
+
vectors = VECTORS["stored_state"]
|
|
334
|
+
self.clock = vectors["now_ms"]
|
|
335
|
+
|
|
336
|
+
def not_json(name: str) -> None:
|
|
337
|
+
self.fail(f"the state saved {name}, which isn't JSON")
|
|
338
|
+
|
|
339
|
+
for case in vectors["history_cases"]:
|
|
340
|
+
with self.subTest(case["name"]):
|
|
341
|
+
self.api.calls.clear()
|
|
342
|
+
store = recapi.MemoryStore()
|
|
343
|
+
client = self.client(store=store)
|
|
344
|
+
key = client._key("u", None)
|
|
345
|
+
top = [case["entry"], *vectors["rest"]]
|
|
346
|
+
history = {**vectors["history"], "top": top}
|
|
347
|
+
store.set(key, json.dumps({"history": history, "shown": [], "restored": True}))
|
|
348
|
+
self.assertFalse(client.recommend(user_id="u").fallback)
|
|
349
|
+
self.assertEqual(self.api.paths(), ["/v1/recommendations"])
|
|
350
|
+
sent = self.api.calls[0].body["history"]
|
|
351
|
+
self.assertEqual(len(sent), len(case["sent"]))
|
|
352
|
+
for got, want in zip(sent, case["sent"], strict=True):
|
|
353
|
+
self.assertEqual(got[:2], want[:2])
|
|
354
|
+
self.assertLessEqual(got[1], vectors["max_request_weight"])
|
|
355
|
+
if want[2] is None:
|
|
356
|
+
self.assertGreaterEqual(got[2], 0)
|
|
357
|
+
else:
|
|
358
|
+
self.assertEqual(got[2], want[2])
|
|
359
|
+
client.track("d", 3, user_id="u")
|
|
360
|
+
client.track("h", 3, user_id="u")
|
|
361
|
+
saved = json.loads(store.get(key), parse_constant=not_json)
|
|
362
|
+
self.assertIn("o", [entry[0] for entry in saved["history"]["top"]])
|
|
363
|
+
|
|
332
364
|
def test_identify(self) -> None:
|
|
333
365
|
vectors = VECTORS["identify"]
|
|
334
366
|
now = vectors["now_ms"]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|