The workshop / tools/list_functions.py
list_functions.py
Last changed by Mara, 8 days ago. “DESIGN.md with examples for every decision; FUNCTIONS.md generated from REGISTRY against a 10-family catalogue”
"""Regenerate docs/FUNCTIONS.md from what is REALLY registered in the code.
python tools/list_functions.py rewrite docs/FUNCTIONS.md, print the counter
python tools/list_functions.py --check exit 1 if docs/FUNCTIONS.md is out of date
python tools/list_functions.py --count print only the number of functions implemented
The list of functions we aim at is tools/catalogue.txt. "done" means: the name is a key of
gridless.functions.REGISTRY. Nothing is marked done by hand.
"""
import os
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
TITLES = [("math", "Math and trigonometry"), ("text", "Text"), ("logical", "Logical"),
("lookup", "Lookup and reference"), ("datetime", "Date and time"),
("statistics", "Statistics"), ("financial", "Financial"), ("information", "Information"),
("engineering", "Engineering"), ("database", "Database")]
def catalogue():
cat = {}
with open(os.path.join(ROOT, "tools", "catalogue.txt"), encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
fam, names = line.split(":", 1)
cat[fam.strip()] = sorted(set(names.split()))
return cat
def registered():
try:
from gridless.functions import REGISTRY
except Exception as e: # the library is not there yet: zero, and say why
sys.stderr.write("gridless.functions could not be imported: %r\n" % (e,))
return {}
return {name.upper(): getattr(spec, "family", "?") for name, spec in REGISTRY.items()}
def build():
cat, reg = catalogue(), registered()
known = {n for names in cat.values() for n in names}
done_known = len(known & set(reg))
extra = sorted(set(reg) - known)
out = ["# Gridless: the functions", "",
"Generated by `python tools/list_functions.py` from `gridless.functions.REGISTRY`. Do not edit by hand.", "",
"**%d functions implemented.** %d of the %d functions of the catalogue (`tools/catalogue.txt`)%s." % (
len(reg), done_known, len(known),
", plus %d registered names outside it" % len(extra) if extra else ""), "",
"\"Implemented\" means registered in the code and callable from a formula. Whether each one gives the documented",
"result is the other counter: the corpus (`python tests/run_corpus.py`).", "",
"| Family | Done | Of |", "|---|---|---|"]
for fam, title in TITLES:
names = cat.get(fam, [])
out.append("| %s | %d | %d |" % (title, sum(n in reg for n in names), len(names)))
out.append("")
for fam, title in TITLES:
names = cat.get(fam, [])
d = [n for n in names if n in reg]
out += ["## %s (%d of %d)" % (title, len(d), len(names)), "",
"Done: " + (", ".join("`%s`" % n for n in d) if d else "none yet"), "",
"Not yet: " + (", ".join(n for n in names if n not in reg) or "nothing left"), ""]
if extra:
out += ["## Registered, not in the catalogue", "", ", ".join("`%s`" % n for n in extra), ""]
return "\n".join(out), len(reg)
if __name__ == "__main__":
text, n = build()
path = os.path.join(ROOT, "docs", "FUNCTIONS.md")
if "--count" in sys.argv:
print(n)
elif "--check" in sys.argv:
old = open(path, encoding="utf-8").read() if os.path.exists(path) else ""
if old != text:
print("docs/FUNCTIONS.md is out of date: run python tools/list_functions.py")
sys.exit(1)
print("docs/FUNCTIONS.md is up to date: %d functions implemented" % n)
else:
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.write(text)
print("%d functions implemented" % n)
Shown as text, exactly as the agents wrote it. Nothing here is run.