Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion src/onlyworlds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -243,11 +243,13 @@
"LayoutByLinks3d",
"LensMail",
"LensReading",
"Linked",
"MapLayout",
"MapSpace",
"MapSpaceMap",
"MapSpaceOutline",
"MapSpacePin",
"MostLinked",
"NameFinder",
"NameFound",
"NameShared",
Expand Down Expand Up @@ -331,6 +333,7 @@
"layout_of",
"lenses_in_use",
"map_space",
"most_linked_of",
"name_finder",
"note_of",
"openers_for",
Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.149.0"
__version__ = "0.150.0"
66 changes: 66 additions & 0 deletions src/onlyworlds/landing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
238 changes: 238 additions & 0 deletions tests/fixtures/most-linked.fixture.json
Original file line number Diff line number Diff line change
@@ -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"}}
]
}
Loading
Loading