3 Formatting, and a corpus

just setup

Code in one style is easier to read and to change, and a count of three words shows little. So ruff, a formatter and linter, comes in as a tool for development. uv add --dev ruff wrote it into the group dev of pyproject.toml, and into uv.lock, and just setup installs it. ruff also gets a line length in pyproject.toml.

And just setup now makes an artifact, data/corpus.txt, a text of 2000 words, with scripts/corpus.py. An artifact is a file that the project makes and git does not hold. .gitignore names data/, so git leaves the corpus alone, and it stays when the code changes.

uv run ruff format --check
head -3 data/corpus.txt
uv run tally data/corpus.txt | sort -rn | head -5
just setup

The second just setup prints data/corpus.txt is there, and leaves the corpus alone.

Edit the code, as a learner would. This command adds a comment at the end of src/tally/__init__.py:

printf '\n# an edit made in class\n' >> src/tally/__init__.py

What changed

  • .gitignore: git does not hold the corpus. 3 lines added. The file.
    • look at data/
  • justfile: just setup runs the script. 1 line added. The file.
    • look at uv run python scripts/corpus.py data/corpus.txt
  • pyproject.toml: the group dev, with ruff, and the line length. 8 lines added. The file.
    • look at line-length = 100
  • scripts/corpus.py: writes the same 2000 words each time, and leaves a corpus that is there alone. New, 25 lines. The file.
  • uv.lock: the exact version of ruff. 35 lines added. The file.
    • look at name = "ruff"
The diff of .gitignore
@@ -8,3 +8,6 @@ wheels/
 
 # Virtual environments
 .venv
+
+# Artifacts that just setup makes: they are not in git
+data/
The diff of justfile
@@ -5,6 +5,7 @@ default:
 # Make the environment of this step: the first command of every step
 setup:
     uv sync
+    uv run python scripts/corpus.py data/corpus.txt
 
 # Count the words of a file
 run file:
The diff of pyproject.toml
@@ -12,3 +12,11 @@ tally = "tally:main"
 [build-system]
 requires = ["uv_build>=0.12.13,<0.13.0"]
 build-backend = "uv_build"
+
+[dependency-groups]
+dev = [
+    "ruff>=0.17.0",
+]
+
+[tool.ruff]
+line-length = 100
The diff of scripts/corpus.py
@@ -0,0 +1,25 @@
+"""Write data/corpus.txt, a text of 2000 words to count, the same every time. Leave it alone if it is there."""
+
+import sys
+from pathlib import Path
+
+WORDS = "the a word count text tally test step move commit tag note slide class shell file line code".split()
+
+
+def main() -> None:
+    target = Path(sys.argv[1] if len(sys.argv) > 1 else "data/corpus.txt")
+    if target.exists():
+        print(f"{target} is there")
+        return
+    seed, words = 7, []
+    for _ in range(2000):
+        seed = (seed * 1103515245 + 12345) % 2**31
+        words.append(WORDS[seed % len(WORDS)])
+    target.parent.mkdir(parents=True, exist_ok=True)
+    lines = (" ".join(words[i : i + 12]) for i in range(0, len(words), 12))
+    target.write_text("\n".join(lines) + "\n")
+    print(f"wrote {target}: {len(words)} words")
+
+
+if __name__ == "__main__":
+    main()

This change on GitHub. The code after it.