From 7212b8151b9a9e76c7f93601b3202afb62053638 Mon Sep 17 00:00:00 2001 From: Titus Date: Tue, 24 Nov 2026 16:38:21 +0100 Subject: [PATCH] 0.150.0: most_linked_of, the most linked elements of each type, from a reader's graph A few of every type by the landing profile's degree, ties by id in code point order, never one ranking, so a door can offer what a world turns on without putting one hub first. A world's secret is often its hub, so this is named "the most linked" and not "where to start": where a world has a Guide, the Guide is the start. It reads world_graph's answer and folds nothing; degree 0 and known-of nodes are never picked; per_type is a whole number of 1 or more. A draft reading held to tests/fixtures/most-linked.fixture.json (18 cases, the same bytes as the npm SDK's copy, its sha256 in both suites) until the landing profile takes it. Co-Authored-By: Claude Opus 5.5 --- .gitattributes | 2 + CHANGELOG.md | 4 + src/onlyworlds/__init__.py | 5 +- src/onlyworlds/_version.py | 2 +- src/onlyworlds/landing.py | 66 +++++++ tests/fixtures/most-linked.fixture.json | 238 ++++++++++++++++++++++++ tests/test_most_linked.py | 81 ++++++++ 7 files changed, 396 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/most-linked.fixture.json create mode 100644 tests/test_most_linked.py diff --git a/.gitattributes b/.gitattributes index 8587835..a0dc726 100644 --- a/.gitattributes +++ b/.gitattributes @@ -30,3 +30,5 @@ tests/fixtures/name-finder.cases.json -text src/onlyworlds/convert/kanka.json -text # The known Pins reader's shared cases, the same bytes as the npm SDK's copy (both tests hold its sha256). tests/fixtures/pins-known-to.cases.json -text +# The most linked reader's shared cases, the same bytes as the npm SDK's copy (both tests hold its sha256). +tests/fixtures/most-linked.fixture.json -text diff --git a/CHANGELOG.md b/CHANGELOG.md index e97b5bb..ca0a39d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is ` ## Unreleased +## 0.150.0 (2026-11-24) + +- **Added: `most_linked_of(graph, *, per_type=None)`, the most linked elements of each type.** From the graph `world_graph` answers for a reader, so only what that reader is served is counted (map space, hidden, proposed and room elements stay out unless the graph brought them in). For each type, at most `per_type` (default 3) of its elements by the landing profile's degree, highest first, ties by id in code point order; a node of degree 0 and a known-of node are never picked, and a type with nothing to pick is left out. It answers `{"per_type", "by_type"}`, each entry `{"id", "name", "degree"}`, the name as the graph holds it or None. A few of every type, never one ranking; it is "the most linked", not "where to start": in a world with a secret, the secret is often the hub, so where a world has a Guide, the Guide is the start. A draft reading, held to the package's own fixture (`tests/fixtures/most-linked.fixture.json`, the same bytes as the npm SDK's) until the landing profile takes it. A `per_type` that is not a whole number of 1 or more is a ValueError naming it. The npm SDK's `mostLinkedOf` answers the same. + ## 0.149.0 (2026-11-24) - **Added: `calendar.format_steps(cfg, steps)`, a page of dates in one calendar.** The same words as `format_step` for every step, with the calendar's own work done once: its year shape, its eras in order, which months each day outside the months follows and the week's skipped days once for the call, each year's start and slots once per year. On a calendar of 64 eras, four leap rules and a year of 1,000 slots, 200 dates across the 32-bit range take about a sixth of the time `format_step` takes for them one by one; on two of the profile fixture's real calendars, about a quarter to a third. Held against `format_step` on every valid calendar of the fixture (the fixture's steps, a dense run inside a few years, a step in each of many years, the far edges); a step `format_step` refuses is refused the same way. `format_step` itself is unchanged. diff --git a/src/onlyworlds/__init__.py b/src/onlyworlds/__init__.py index dcee27d..7de3aa2 100644 --- a/src/onlyworlds/__init__.py +++ b/src/onlyworlds/__init__.py @@ -84,7 +84,7 @@ check_kinds, sanitize_payload, ) -from .landing import Landing, LandingCluster, LandingGap, LandingMember, landing_of +from .landing import Landing, LandingCluster, LandingGap, LandingMember, Linked, MostLinked, landing_of, most_linked_of from .layout_kind import ( AroundLayout, AroundPoint, @@ -243,11 +243,13 @@ "LayoutByLinks3d", "LensMail", "LensReading", + "Linked", "MapLayout", "MapSpace", "MapSpaceMap", "MapSpaceOutline", "MapSpacePin", + "MostLinked", "NameFinder", "NameFound", "NameShared", @@ -331,6 +333,7 @@ "layout_of", "lenses_in_use", "map_space", + "most_linked_of", "name_finder", "note_of", "openers_for", diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index 1a81118..3a25f30 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.149.0" +__version__ = "0.150.0" diff --git a/src/onlyworlds/landing.py b/src/onlyworlds/landing.py index 0354c52..7cc54ef 100644 --- a/src/onlyworlds/landing.py +++ b/src/onlyworlds/landing.py @@ -171,3 +171,69 @@ def landing_of( "unclustered": unclustered, "gaps": gaps, } + + +class Linked(TypedDict): + id: str + #: The name as the graph holds it, or None when it has none. + name: str | None + degree: int + + +class MostLinked(TypedDict): + #: The count per type it was read at. + per_type: int + #: By type, the types in code point order; a type with nothing to pick is left out. + by_type: dict[str, list[Linked]] + + +_SAFE = 2**53 - 1 + + +def most_linked_of(graph: Mapping[str, Any], *, per_type: int | None = None) -> MostLinked: + """The most linked elements of each type, from the graph ``world_graph`` answered for the asker, so it holds + exactly what that reader may see (map space, hidden, proposed and room elements are out unless the graph + brought them in). For each type, its most linked elements by the landing profile's degree (``degree_of``'s + count), highest first, ties by id in code point order, at most ``per_type`` of each (default 3): a few of every + type, never one ranking. It is "the most linked", never "where to start": where a world has a Guide, the Guide + is the start, and a door says so. A node of degree 0 and a known-of node are never picked. It reads the graph + it is given and folds nothing: a node's type and name are echoed as written. The same graph and options give + the same answer, byte for byte. A draft reading, held to the package's own fixture until the landing profile + takes it. ``per_type`` that is not a whole number of 1 or more (and at most 2**53 - 1, as JavaScript reads + one) is a ValueError naming it; None is its default. The npm SDK's ``mostLinkedOf`` gives the same answer on + the same graph.""" + if ( + not isinstance(graph, Mapping) + or not isinstance(graph.get("nodes"), list) + or not isinstance(graph.get("edges"), list) + ): + raise TypeError("most_linked_of: graph is what world_graph answers, a mapping with nodes and edges lists.") + per = 3 + if per_type is not None: + w = _whole(per_type) + if w is None or w < 1 or w > _SAFE: + raise ValueError(f"most_linked_of: per_type is a whole number, 1 or more; got {per_type!r}.") + per = w + + nodes: list[Mapping[str, Any]] = graph["nodes"] + degree: dict[str, int] = {n["id"]: 0 for n in nodes} + for e in graph["edges"]: + s, t = e["source"], e["target"] + if s in degree: + degree[s] += 1 + if t != s and t in degree: + degree[t] += 1 + + grouped: dict[str, list[Linked]] = {} + for n in nodes: + d = degree.get(n["id"], 0) + if d == 0 or n.get("known_of") is True: + continue + name = n.get("name") + grouped.setdefault(n["type"], []).append( + {"id": n["id"], "name": name if isinstance(name, str) else None, "degree": d} + ) + out: dict[str, list[Linked]] = {} + for t in sorted(grouped): + out[t] = sorted(grouped[t], key=lambda x: (-x["degree"], x["id"]))[:per] + return {"per_type": per, "by_type": out} diff --git a/tests/fixtures/most-linked.fixture.json b/tests/fixtures/most-linked.fixture.json new file mode 100644 index 0000000..6388bce --- /dev/null +++ b/tests/fixtures/most-linked.fixture.json @@ -0,0 +1,238 @@ +{ + "profile": "most-linked", + "status": "draft: the packages' own until the landing profile takes it as a section (a serial); held byte for byte in the SDK and the Python package", + "notes": "Each case is a graph as worldGraph answers it, the options, and the whole answer. Degree is the landing profile's: every edge counted once at each node it touches, a link to itself once. Options are written as the Python keyword (per_type); the SDK reads it as perType. A refusal names the option.", + "cases": [ + { + "name": "a few of each type, most linked first, ties by id", + "graph": { + "nodes": [ + {"id": "c1", "type": "character", "name": "Ada"}, + {"id": "c2", "type": "character", "name": "Brin"}, + {"id": "c3", "type": "character", "name": "Cass"}, + {"id": "c4", "type": "character", "name": "Dov"}, + {"id": "l1", "type": "location", "name": "Port"}, + {"id": "l2", "type": "location", "name": "Hill"}, + {"id": "e1", "type": "event", "name": "Feast"} + ], + "edges": [ + {"source": "e1", "field": "characters", "target": "c1", "index": 0}, + {"source": "e1", "field": "characters", "target": "c2", "index": 1}, + {"source": "e1", "field": "characters", "target": "c3", "index": 2}, + {"source": "c1", "field": "location", "target": "l1", "index": 0}, + {"source": "c2", "field": "location", "target": "l1", "index": 0}, + {"source": "c3", "field": "location", "target": "l2", "index": 0}, + {"source": "c4", "field": "friends", "target": "c1", "index": 0} + ] + }, + "options": {}, + "want": {"per_type": 3, "by_type": { + "character": [{"id": "c1", "name": "Ada", "degree": 3}, {"id": "c2", "name": "Brin", "degree": 2}, {"id": "c3", "name": "Cass", "degree": 2}], + "event": [{"id": "e1", "name": "Feast", "degree": 3}], + "location": [{"id": "l1", "name": "Port", "degree": 2}, {"id": "l2", "name": "Hill", "degree": 1}] + }} + }, + { + "name": "one of each type", + "graph": { + "nodes": [ + {"id": "c1", "type": "character", "name": "Ada"}, + {"id": "c2", "type": "character", "name": "Brin"}, + {"id": "c3", "type": "character", "name": "Cass"}, + {"id": "c4", "type": "character", "name": "Dov"}, + {"id": "l1", "type": "location", "name": "Port"}, + {"id": "l2", "type": "location", "name": "Hill"}, + {"id": "e1", "type": "event", "name": "Feast"} + ], + "edges": [ + {"source": "e1", "field": "characters", "target": "c1", "index": 0}, + {"source": "e1", "field": "characters", "target": "c2", "index": 1}, + {"source": "e1", "field": "characters", "target": "c3", "index": 2}, + {"source": "c1", "field": "location", "target": "l1", "index": 0}, + {"source": "c2", "field": "location", "target": "l1", "index": 0}, + {"source": "c3", "field": "location", "target": "l2", "index": 0}, + {"source": "c4", "field": "friends", "target": "c1", "index": 0} + ] + }, + "options": {"per_type": 1}, + "want": {"per_type": 1, "by_type": { + "character": [{"id": "c1", "name": "Ada", "degree": 3}], + "event": [{"id": "e1", "name": "Feast", "degree": 3}], + "location": [{"id": "l1", "name": "Port", "degree": 2}] + }} + }, + { + "name": "degree 0 is never picked, a type with nothing to pick is left out, a name as written or null", + "graph": { + "nodes": [ + {"id": "a", "type": "character", "name": " Ada "}, + {"id": "b", "type": "character"}, + {"id": "t", "type": "trait", "name": "Brave"} + ], + "edges": [ + {"source": "a", "field": "friends", "target": "b", "index": 0} + ] + }, + "options": {}, + "want": {"per_type": 3, "by_type": { + "character": [{"id": "a", "name": " Ada ", "degree": 1}, {"id": "b", "name": null, "degree": 1}] + }} + }, + { + "name": "a known-of node is never picked", + "graph": { + "nodes": [ + {"id": "g", "type": "character", "name": "Ghost", "known_of": true}, + {"id": "h", "type": "character", "name": "Hal"}, + {"id": "e", "type": "event", "name": "Wake"} + ], + "edges": [ + {"source": "e", "field": "characters", "target": "g", "index": 0}, + {"source": "e", "field": "characters", "target": "h", "index": 1} + ] + }, + "options": {}, + "want": {"per_type": 3, "by_type": { + "character": [{"id": "h", "name": "Hal", "degree": 1}], + "event": [{"id": "e", "name": "Wake", "degree": 2}] + }} + }, + { + "name": "a mirror pair is two edges, a link to itself counts once", + "graph": { + "nodes": [ + {"id": "p", "type": "character", "name": "P"}, + {"id": "q", "type": "character", "name": "Q"}, + {"id": "s", "type": "object", "name": "S"} + ], + "edges": [ + {"source": "p", "field": "friends", "target": "q", "index": 0}, + {"source": "q", "field": "friends", "target": "p", "index": 0}, + {"source": "s", "field": "parent_object", "target": "s", "index": 0} + ] + }, + "options": {}, + "want": {"per_type": 3, "by_type": { + "character": [{"id": "p", "name": "P", "degree": 2}, {"id": "q", "name": "Q", "degree": 2}], + "object": [{"id": "s", "name": "S", "degree": 1}] + }} + }, + { + "name": "ties by id in code point order, an id above U+FFFF after one below it", + "graph": { + "nodes": [ + {"id": "😀", "type": "character"}, + {"id": "~", "type": "character"}, + {"id": "a", "type": "character"}, + {"id": "B", "type": "character"}, + {"id": "x", "type": "event", "name": "Gathering"} + ], + "edges": [ + {"source": "x", "field": "characters", "target": "😀", "index": 0}, + {"source": "x", "field": "characters", "target": "~", "index": 1}, + {"source": "x", "field": "characters", "target": "a", "index": 2}, + {"source": "x", "field": "characters", "target": "B", "index": 3} + ] + }, + "options": {"per_type": 4}, + "want": {"per_type": 4, "by_type": { + "character": [{"id": "B", "name": null, "degree": 1}, {"id": "a", "name": null, "degree": 1}, {"id": "~", "name": null, "degree": 1}, {"id": "😀", "name": null, "degree": 1}], + "event": [{"id": "x", "name": "Gathering", "degree": 4}] + }} + }, + { + "name": "a secret is a hub: the cover-up comes first in its type (the lean a door must not call where to start)", + "graph": { + "nodes": [ + {"id": "cover", "type": "construct", "name": "The Cover-Up"}, + {"id": "song", "type": "construct", "name": "The Anthem"}, + {"id": "hero", "type": "character", "name": "Admiral"}, + {"id": "heir", "type": "character", "name": "Heir"}, + {"id": "war", "type": "event", "name": "The War"}, + {"id": "port", "type": "location", "name": "Port"} + ], + "edges": [ + {"source": "cover", "field": "characters", "target": "hero", "index": 0}, + {"source": "cover", "field": "characters", "target": "heir", "index": 1}, + {"source": "cover", "field": "events", "target": "war", "index": 0}, + {"source": "cover", "field": "locations", "target": "port", "index": 0}, + {"source": "song", "field": "characters", "target": "hero", "index": 0}, + {"source": "war", "field": "characters", "target": "hero", "index": 0}, + {"source": "heir", "field": "location", "target": "port", "index": 0} + ] + }, + "options": {}, + "want": {"per_type": 3, "by_type": { + "character": [{"id": "hero", "name": "Admiral", "degree": 3}, {"id": "heir", "name": "Heir", "degree": 2}], + "construct": [{"id": "cover", "name": "The Cover-Up", "degree": 4}, {"id": "song", "name": "The Anthem", "degree": 1}], + "event": [{"id": "war", "name": "The War", "degree": 2}], + "location": [{"id": "port", "name": "Port", "degree": 2}] + }} + }, + { + "name": "null is the default, and a whole number written 2.0 is 2", + "graph": { + "nodes": [ + {"id": "a", "type": "character"}, + {"id": "b", "type": "character"}, + {"id": "c", "type": "character"} + ], + "edges": [ + {"source": "a", "field": "friends", "target": "b", "index": 0}, + {"source": "a", "field": "friends", "target": "c", "index": 1} + ] + }, + "options": {"per_type": 2.0}, + "want": {"per_type": 2, "by_type": { + "character": [{"id": "a", "name": null, "degree": 2}, {"id": "b", "name": null, "degree": 1}] + }} + }, + { + "name": "per_type null reads as 3", + "graph": { + "nodes": [ + {"id": "a", "type": "character"}, + {"id": "b", "type": "character"} + ], + "edges": [ + {"source": "a", "field": "friends", "target": "b", "index": 0} + ] + }, + "options": {"per_type": null}, + "want": {"per_type": 3, "by_type": { + "character": [{"id": "a", "name": null, "degree": 1}, {"id": "b", "name": null, "degree": 1}] + }} + }, + { + "name": "an empty graph", + "graph": {"nodes": [], "edges": []}, + "options": {}, + "want": {"per_type": 3, "by_type": {}} + }, + { + "name": "a name that is not a string reads as null", + "graph": { + "nodes": [ + {"id": "a", "type": "character", "name": 7}, + {"id": "b", "type": "character", "name": null}, + {"id": "c", "type": "character", "name": ["Cass"]} + ], + "edges": [ + {"source": "a", "field": "friends", "target": "b", "index": 0}, + {"source": "c", "field": "friends", "target": "a", "index": 0} + ] + }, + "options": {}, + "want": {"per_type": 3, "by_type": { + "character": [{"id": "a", "name": null, "degree": 2}, {"id": "b", "name": null, "degree": 1}, {"id": "c", "name": null, "degree": 1}] + }} + }, + {"name": "refused: {\"lens\": \"x\"}", "graph": {"nodes": [], "edges": []}, "options": {"lens": "x"}, "want": {"refused": "lens"}}, + {"name": "refused: {\"per_type\": 0}", "graph": {"nodes": [], "edges": []}, "options": {"per_type": 0}, "want": {"refused": "per_type"}}, + {"name": "refused: {\"per_type\": -1}", "graph": {"nodes": [], "edges": []}, "options": {"per_type": -1}, "want": {"refused": "per_type"}}, + {"name": "refused: {\"per_type\": 1.5}", "graph": {"nodes": [], "edges": []}, "options": {"per_type": 1.5}, "want": {"refused": "per_type"}}, + {"name": "refused: {\"per_type\": \"3\"}", "graph": {"nodes": [], "edges": []}, "options": {"per_type": "3"}, "want": {"refused": "per_type"}}, + {"name": "refused: {\"per_type\": true}", "graph": {"nodes": [], "edges": []}, "options": {"per_type": true}, "want": {"refused": "per_type"}}, + {"name": "refused: {\"per_type\": 9007199254740992}", "graph": {"nodes": [], "edges": []}, "options": {"per_type": 9007199254740992}, "want": {"refused": "per_type"}} + ] +} diff --git a/tests/test_most_linked.py b/tests/test_most_linked.py new file mode 100644 index 0000000..0c84bca --- /dev/null +++ b/tests/test_most_linked.py @@ -0,0 +1,81 @@ +"""most_linked_of: the most linked elements of each type, a draft reading beside the landing profile. Every case +of tests/fixtures/most-linked.fixture.json, the same file byte for byte as the npm SDK's +test/fixtures/most-linked.fixture.json (both suites hold its sha256), answers written by hand.""" + +from __future__ import annotations + +import copy +import hashlib +import json +from pathlib import Path +from typing import Any + +import pytest + +from onlyworlds import most_linked_of, world_graph + +REPO = Path(__file__).resolve().parent.parent +FILE = REPO / "tests" / "fixtures" / "most-linked.fixture.json" +SHA256 = "9a7b27d849cd1c268d86b754bcdb7954cb23b3ede85178023332d61e497489c4" +RAW = FILE.read_bytes() +DOC = json.loads(RAW.decode("utf-8")) + + +def _dump(value: object) -> str: + # The SDK's JSON.stringify: no spaces, non-ASCII as written. + return json.dumps(value, ensure_ascii=False, separators=(",", ":")) + + +def test_the_fixture_is_the_shared_file() -> None: + assert hashlib.sha256(RAW).hexdigest() == SHA256 + assert DOC["profile"] == "most-linked" + assert len(DOC["cases"]) == 18 + + +@pytest.mark.parametrize("case", DOC["cases"], ids=[c["name"] for c in DOC["cases"]]) +def test_fixture_case(case: dict[str, Any]) -> None: + if "refused" in case["want"]: + with pytest.raises((TypeError, ValueError)) as err: + most_linked_of(case["graph"], **case["options"]) + assert case["want"]["refused"] in str(err.value) + return + # Byte for byte, key order included. + assert _dump(most_linked_of(case["graph"], **case["options"])) == _dump(case["want"]) + + +def test_no_options_is_the_default() -> None: + c = DOC["cases"][0] + assert _dump(most_linked_of(c["graph"])) == _dump(c["want"]) + + +def test_a_graph_that_is_not_one_is_refused() -> None: + bads: list[Any] = [None, {"nodes": []}, {"nodes": [], "edges": None}] + for bad in bads: + with pytest.raises(TypeError): + most_linked_of(bad) + + +def test_it_changes_nothing_in_the_graph() -> None: + g = DOC["cases"][6]["graph"] + before = copy.deepcopy(g) + most_linked_of(g) + assert g == before + + +def test_through_world_graph_a_hidden_element_and_map_space_are_not_counted() -> None: + elements = [ + {"id": "c1", "type": "character", "name": "Ada", "friends": ["c2"]}, + {"id": "c2", "type": "character", "name": "Brin"}, + {"id": "sec", "type": "event", "name": "The Secret", "characters": ["c1", "c2"], "hidden": True}, + {"id": "m1", "type": "map", "name": "Chart"}, + {"id": "p1", "type": "pin", "name": "Pin", "map": "m1", "element_type": "character", "element_id": "c1"}, + ] + got = most_linked_of(world_graph({"elements": elements}, {"rung": "owner"})) + assert got == { + "per_type": 3, + "by_type": {"character": [{"id": "c1", "name": "Ada", "degree": 1}, {"id": "c2", "name": "Brin", "degree": 1}]}, + } + # The control: a graph that brings the hidden element in counts it, so the case above can see it. + shown = most_linked_of(world_graph({"elements": elements}, {"rung": "owner"}, hidden=True)) + assert shown["by_type"]["event"] == [{"id": "sec", "name": "The Secret", "degree": 2}] + assert shown["by_type"]["character"][0]["degree"] == 2