export.py
The file math/export.py. 185 lines, 8 definitions.
Produces the JSON for the web site and the report: ../data/*.json.
Run with: uv run python export.py. No number on the site is entered by hand. They all come from here.
Code, comments and printed output are in Turkish. The explanations are translated.
Top of file: imports
"""Web sitesi ve rapor için JSON üretir: ../data/*.json.Çalıştırma: uv run python export.pySitedeki hiçbir sayı elle girilmez. Hepsi buradan gelir."""from __future__ import annotationsimport jsonfrom math import gcdfrom pathlib import Pathfrom tonnetz import ( INTERVALS_53, MAKAMS_53, TETS, Chord, Tonnetz, analyze, chain, chain_chords, chain_count_table, chain_index, chain_span, cycle, diameter, distances_from, formula_range, kernel_basis, moving_step, parse_steps, representatives, translation,)DATA
constantDATA = Path(__file__).resolve().parent.parent / "data"WORDS
constantWORDS = ("PL", "PR", "LR")AEU_N
constantAEU_N = 53AEU_LENGTH
constantAEU_LENGTH = 24chord_json
functiondef chord_json(c: Chord) -> dict: return {"kind": c.kind, "root": c.root}export_tonnetz
functiondef export_tonnetz() -> dict: out = {} for N in TETS: t = Tonnetz.tet(N) out[str(N)] = { "N": N, "a": t.a, "b": t.b, "fifth": t.fifth, "nodes": N, "edges": len(t.edges), "triangles": len(t.triangles), "euler": t.euler_characteristic, "steps": {op: moving_step(t, op) for op in "PLR"}, "translations": {w: translation(t, w) for w in WORDS}, "kernel_basis": [list(v) for v in kernel_basis(t)], "representatives": [list(v) for v in representatives(t)], } return outexport_cycles
functiondef export_cycles() -> dict: out = {} for N in TETS: t = Tonnetz.tet(N) divisors = {"PL": t.b, "PR": t.a, "LR": t.fifth} out[str(N)] = { w: { "length": len(cyc := cycle(t, w, Chord("M", 0))), "formula": 2 * N // gcd(N, divisors[w]), "hamiltonian": len(cyc) == 2 * N, "chords": [chord_json(c) for c in cyc], } for w in WORDS } return outexport_chain
functiondef export_chain() -> dict: out = {} for N in TETS: t = Tonnetz.tet(N) lo, hi = formula_range(t) out[str(N)] = { "span": chain_span(t), "formula_range": [lo, hi], "chain_order": chain(t, N, 0), "table": chain_count_table(t), } return outexport_aeu
functiondef export_aeu() -> dict: t = Tonnetz.tet(AEU_N) counts = { (sum(c.kind == "M" for c in cs), sum(c.kind == "m" for c in cs)) for cs in (chain_chords(t, AEU_LENGTH, k0) for k0 in range(t.N)) } assert len(counts) == 1, "Akor sayısı k0'a bağlı çıktı." major, minor = counts.pop() chords = chain_chords(t, AEU_LENGTH, 0) pitches = chain(t, AEU_LENGTH, 0) return { "status": "varsayım", "note": "V1: AEU'nun 24 perdesi 24 ardışık beşlilik bir zincirdir. " "Başlangıç perdesi (k0) kaynaktan doğrulanmadı. Sayılar k0'dan bağımsızdır.", "N": AEU_N, "length": AEU_LENGTH, "major": major, "minor": minor, "total": major + minor, "k0_independent": True, "example_k0": 0, "example_pitches": pitches, "example_chords": [chord_json(c) for c in chords], "example_chord_components": len(t.chord_components(chords)), "example_node_components": len(t.node_components(pitches)), }export_diameter
functiondef export_diameter() -> dict: out = {} for N in TETS: t = Tonnetz.tet(N) d = diameter(t) out[str(N)] = { "diameter": d, "hexagonal_bound": 3 * d * d + 3 * d + 1, "distances_from_0": distances_from(t, 0), } return outexport_makams
functiondef export_makams() -> dict: t = Tonnetz.tet(AEU_N) makams = {} for key, m in MAKAMS_53.items(): steps = parse_steps(m["steps"]) info = analyze(t, 0, steps) ks = info["chain_indices"] info.update( { "name": m["name"], "symbols": m["steps"].split(), "fits_in_chain_of_24": info["chain_arc_length"] <= AEU_LENGTH, } ) assert ks == [chain_index(t, p) for p in info["pitches"]] makams[key] = info shapes: dict[tuple, list[str]] = {} for key, info in makams.items(): shapes.setdefault(tuple(info["shape"]), []).append(key) return { "status": "varsayım", "note": "V2: Dizilerin koma adımları kaynaktan doğrulanmadı. Karar perdesi 0 alındı.", "N": AEU_N, "intervals": INTERVALS_53, "makams": makams, "shape_classes": sorted(shapes.values()), }main
functiondef main() -> None: DATA.mkdir(exist_ok=True) files = { "tonnetz.json": export_tonnetz(), "cycles.json": export_cycles(), "chain.json": export_chain(), "aeu.json": export_aeu(), "diameter.json": export_diameter(), "makams.json": export_makams(), } for name, content in files.items(): path = DATA / name path.write_text(json.dumps(content, ensure_ascii=False, indent="\t") + "\n", encoding="utf-8") print(f"yazıldı: {path.relative_to(DATA.parent)}")if __name__ == "__main__": main()