Skip to content

Commit 3ad4cc6

Browse files
style: auto-fix lint and format
1 parent 279de1b commit 3ad4cc6

2 files changed

Lines changed: 111 additions & 25 deletions

File tree

autosearch/v2/judge.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,14 @@ def score_results(
7777
now = now or dt.datetime.now(dt.timezone.utc)
7878
total_results = len(results)
7979
unique_urls = {item.get("url") for item in results if item.get("url")}
80-
queries = {str(item.get("query", "")).strip() for item in results if str(item.get("query", "")).strip()}
81-
platforms = [str(item.get("source", "")).lower() for item in results if item.get("source")]
80+
queries = {
81+
str(item.get("query", "")).strip()
82+
for item in results
83+
if str(item.get("query", "")).strip()
84+
}
85+
platforms = [
86+
str(item.get("source", "")).lower() for item in results if item.get("source")
87+
]
8288
counts = Counter(platforms)
8389
query_words = {word.lower() for query in queries for word in WORD_RE.findall(query)}
8490

@@ -92,7 +98,10 @@ def score_results(
9298
match_count = 0
9399
for item in results:
94100
haystack_words = {
95-
word.lower() for word in WORD_RE.findall(f"{item.get('title', '')} {item.get('snippet', '')}")
101+
word.lower()
102+
for word in WORD_RE.findall(
103+
f"{item.get('title', '')} {item.get('snippet', '')}"
104+
)
96105
}
97106
if query_words and haystack_words.intersection(query_words):
98107
match_count += 1
@@ -121,13 +130,16 @@ def score_results(
121130
}
122131
weight_sum = sum(float(weights.get(name, 0.0)) for name in dimensions)
123132
total = (
124-
sum(dimensions[name] * float(weights.get(name, 0.0)) for name in dimensions) / weight_sum
133+
sum(dimensions[name] * float(weights.get(name, 0.0)) for name in dimensions)
134+
/ weight_sum
125135
if weight_sum > 0
126136
else 0.0
127137
)
128138
return {
129139
"total": max(0.0, min(total, 1.0)),
130-
"dimensions": {name: max(0.0, min(value, 1.0)) for name, value in dimensions.items()},
140+
"dimensions": {
141+
name: max(0.0, min(value, 1.0)) for name, value in dimensions.items()
142+
},
131143
"meta": {
132144
"total_results": total_results,
133145
"unique_urls": len(unique_urls),
@@ -144,7 +156,9 @@ def main() -> int:
144156
try:
145157
weights = json.loads(args.weights) if args.weights else load_default_weights()
146158
results = load_results(Path(args.evidence_file))
147-
payload = score_results(results, args.evidence_file, target=args.target, weights=weights)
159+
payload = score_results(
160+
results, args.evidence_file, target=args.target, weights=weights
161+
)
148162
except Exception as exc:
149163
print(str(exc), file=sys.stderr)
150164
return 1

autosearch/v2/tests/test_judge.py

Lines changed: 91 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@
1414

1515
def write_jsonl(path: Path, rows: list[object]) -> Path:
1616
path.write_text(
17-
"".join(json.dumps(row) + "\n" if isinstance(row, dict) else f"{row}\n" for row in rows),
17+
"".join(
18+
json.dumps(row) + "\n" if isinstance(row, dict) else f"{row}\n"
19+
for row in rows
20+
),
1821
encoding="utf-8",
1922
)
2023
return path
2124

2225

23-
def result(url: str, source: str, query: str, title: str = "", snippet: str = "", metadata: dict | None = None) -> dict:
26+
def result(
27+
url: str,
28+
source: str,
29+
query: str,
30+
title: str = "",
31+
snippet: str = "",
32+
metadata: dict | None = None,
33+
) -> dict:
2434
return {
2535
"url": url,
2636
"title": title,
@@ -33,7 +43,9 @@ def result(url: str, source: str, query: str, title: str = "", snippet: str = ""
3343

3444

3545
def score_file(path: Path, target: int = 30) -> dict:
36-
return judge.score_results(judge.load_results(path), str(path), target=target, now=NOW)
46+
return judge.score_results(
47+
judge.load_results(path), str(path), target=target, now=NOW
48+
)
3749

3850

3951
def test_empty_input(tmp_path: Path) -> None:
@@ -43,50 +55,91 @@ def test_empty_input(tmp_path: Path) -> None:
4355

4456

4557
def test_single_result(tmp_path: Path) -> None:
46-
row = result("https://example.com/1", "github", "python judge", title="Python judge")
58+
row = result(
59+
"https://example.com/1", "github", "python judge", title="Python judge"
60+
)
4761
score = score_file(write_jsonl(tmp_path / "single.jsonl", [row]))
4862
assert score["dimensions"]["quantity"] == pytest.approx(1 / 30)
4963
assert score["dimensions"]["diversity"] == 0.0
5064
assert score["dimensions"]["relevance"] == 1.0
5165

5266

5367
def test_multi_platform_diversity(tmp_path: Path) -> None:
54-
rows = [result(f"https://example.com/{idx}", source, "topic", title="topic") for idx, source in enumerate(
55-
["github"] * 4 + ["reddit"] * 3 + ["hackernews"] * 3,
56-
start=1,
57-
)]
68+
rows = [
69+
result(f"https://example.com/{idx}", source, "topic", title="topic")
70+
for idx, source in enumerate(
71+
["github"] * 4 + ["reddit"] * 3 + ["hackernews"] * 3,
72+
start=1,
73+
)
74+
]
5875
score = score_file(write_jsonl(tmp_path / "diverse.jsonl", rows))
5976
assert score["dimensions"]["diversity"] > 0.5
6077

6178

6279
def test_single_platform_low_diversity(tmp_path: Path) -> None:
63-
rows = [result(f"https://example.com/{idx}", "github", "topic", title="topic") for idx in range(10)]
80+
rows = [
81+
result(f"https://example.com/{idx}", "github", "topic", title="topic")
82+
for idx in range(10)
83+
]
6484
score = score_file(write_jsonl(tmp_path / "single-platform.jsonl", rows))
6585
assert score["dimensions"]["diversity"] == 0.0
6686

6787

6888
def test_relevance_all_match(tmp_path: Path) -> None:
69-
rows = [result(f"https://example.com/{idx}", "github", "agent search", title="Agent search guide") for idx in range(3)]
89+
rows = [
90+
result(
91+
f"https://example.com/{idx}",
92+
"github",
93+
"agent search",
94+
title="Agent search guide",
95+
)
96+
for idx in range(3)
97+
]
7098
score = score_file(write_jsonl(tmp_path / "all-match.jsonl", rows))
7199
assert score["dimensions"]["relevance"] == 1.0
72100

73101

74102
def test_relevance_none_match(tmp_path: Path) -> None:
75-
rows = [result(f"https://example.com/{idx}", "github", "agent search", title="database tuning", snippet="sql indexes") for idx in range(3)]
103+
rows = [
104+
result(
105+
f"https://example.com/{idx}",
106+
"github",
107+
"agent search",
108+
title="database tuning",
109+
snippet="sql indexes",
110+
)
111+
for idx in range(3)
112+
]
76113
score = score_file(write_jsonl(tmp_path / "none-match.jsonl", rows))
77114
assert score["dimensions"]["relevance"] == 0.0
78115

79116

80117
def test_freshness_all_recent(tmp_path: Path) -> None:
81118
recent = (NOW - dt.timedelta(days=30)).isoformat().replace("+00:00", "Z")
82-
rows = [result(f"https://example.com/{idx}", "reddit", "topic", metadata={"published_at": recent}) for idx in range(3)]
119+
rows = [
120+
result(
121+
f"https://example.com/{idx}",
122+
"reddit",
123+
"topic",
124+
metadata={"published_at": recent},
125+
)
126+
for idx in range(3)
127+
]
83128
score = score_file(write_jsonl(tmp_path / "fresh.jsonl", rows))
84129
assert score["dimensions"]["freshness"] == 1.0
85130

86131

87132
def test_freshness_all_old(tmp_path: Path) -> None:
88133
old = (NOW - dt.timedelta(days=300)).isoformat().replace("+00:00", "Z")
89-
rows = [result(f"https://example.com/{idx}", "reddit", "topic", metadata={"updated_at": old}) for idx in range(3)]
134+
rows = [
135+
result(
136+
f"https://example.com/{idx}",
137+
"reddit",
138+
"topic",
139+
metadata={"updated_at": old},
140+
)
141+
for idx in range(3)
142+
]
90143
score = score_file(write_jsonl(tmp_path / "old.jsonl", rows))
91144
assert score["dimensions"]["freshness"] == 0.0
92145

@@ -95,15 +148,25 @@ def test_freshness_unix_timestamp(tmp_path: Path) -> None:
95148
recent_ts = int((NOW - dt.timedelta(days=30)).timestamp())
96149
old_ts = int((NOW - dt.timedelta(days=300)).timestamp())
97150
rows = [
98-
result("https://example.com/1", "reddit", "topic", metadata={"created_utc": recent_ts}),
99-
result("https://example.com/2", "reddit", "topic", metadata={"created_utc": old_ts}),
151+
result(
152+
"https://example.com/1",
153+
"reddit",
154+
"topic",
155+
metadata={"created_utc": recent_ts},
156+
),
157+
result(
158+
"https://example.com/2", "reddit", "topic", metadata={"created_utc": old_ts}
159+
),
100160
]
101161
score = score_file(write_jsonl(tmp_path / "unix.jsonl", rows))
102162
assert score["dimensions"]["freshness"] == 0.5
103163

104164

105165
def test_custom_target(tmp_path: Path) -> None:
106-
rows = [result(f"https://example.com/{idx}", "github", f"query {idx}", title="query") for idx in range(10)]
166+
rows = [
167+
result(f"https://example.com/{idx}", "github", f"query {idx}", title="query")
168+
for idx in range(10)
169+
]
107170
score = score_file(write_jsonl(tmp_path / "target.jsonl", rows), target=10)
108171
assert score["dimensions"]["quantity"] == 1.0
109172

@@ -120,10 +183,19 @@ def test_invalid_json_lines(tmp_path: Path) -> None:
120183

121184

122185
def test_cli_exit_codes(tmp_path: Path) -> None:
123-
evidence = write_jsonl(tmp_path / "cli.jsonl", [result("https://example.com/1", "github", "judge", title="judge")])
186+
evidence = write_jsonl(
187+
tmp_path / "cli.jsonl",
188+
[result("https://example.com/1", "github", "judge", title="judge")],
189+
)
124190
script = Path(judge.__file__)
125-
success = subprocess.run([sys.executable, str(script), str(evidence)], capture_output=True, text=True)
126-
missing = subprocess.run([sys.executable, str(script), str(tmp_path / "missing.jsonl")], capture_output=True, text=True)
191+
success = subprocess.run(
192+
[sys.executable, str(script), str(evidence)], capture_output=True, text=True
193+
)
194+
missing = subprocess.run(
195+
[sys.executable, str(script), str(tmp_path / "missing.jsonl")],
196+
capture_output=True,
197+
text=True,
198+
)
127199
assert success.returncode == 0
128200
assert json.loads(success.stdout)["meta"]["total_results"] == 1
129201
assert missing.returncode == 1

0 commit comments

Comments
 (0)