4 One dependency less
Build it yourself. Read the tutorial is the other way to read this part.
If you start at this part, make your clone as Get the project says, and then run this command. It puts in your clone the files that this part starts from. A file that the project deleted before this part stays, so delete it if git check lists it:
git rm -rq --cached --ignore-unmatch -- :/ && git restore --source 25f4f751628b --staged --worktree -- :/just setuptally uses tabulate for one table, of two columns. A few lines of its own draw the same table, and the project needs one package less.
The code before this part. The code after it.
4.1 tally draws its own table
A function table draws the header, the rule of dashes, and the rows, with the same widths as tabulate.
Apply the patch to src/tally/__init__.py, from the top of the project:
curl -fsSL https://github.com/rahuldave/tally/commit/72b6c553d362423f55d6cf57aefbd53b5588beba.diff | git apply --include=src/tally/__init__.pyThe patch removes from tabulate import tabulate, adds the function table above main, and calls it in place of
tabulate.
uv run tally --top 3 --table data/corpus.txtIt prints the same table as tabulate printed. It has the header word and times, a rule of dashes, and three
rows.
Check your files against this move. The command prints nothing when they match, and otherwise shows what differs:
git check 72b6c553d362If you are stuck, this command puts the files of this move in place of yours. A file that you made yourself stays:
git restore --source 25f4f751628b --staged -- :/ && git restore --source 72b6c553d362 --staged --worktree -- :/What changed
src/tally/__init__.py: the functiontable, in place of tabulate. 10 lines added, 2 removed. The file.- look at
def table(rows: list[tuple[str, int]]) -> str:
- look at
The patch to src/tally/__init__.py
@@ -2,7 +2,6 @@
import sys
-from tabulate import tabulate
from talib import top
@@ -14,6 +13,15 @@ def count(text: str) -> dict[str, int]:
return counts
+def table(rows: list[tuple[str, int]]) -> str:
+ "The rows under a header and a rule: the words on the left, the counts on the right."
+ left = max([len("word") + 2, *(len(word) for word, _ in rows)])
+ right = max([len("times") + 2, *(len(str(times)) for _, times in rows)])
+ lines = [f"{'word':<{left}} {'times':>{right}}", f"{'-' * left} {'-' * right}"]
+ lines += [f"{word:<{left}} {times:>{right}}" for word, times in rows]
+ return "\n".join(lines)
+
+
def main() -> None:
"Count the words of a file, or of the standard input. Options: --top N, and --table."
args = sys.argv[1:]
@@ -29,7 +37,7 @@ def main() -> None:
counts = count(text)
rows = top(counts, n) if n else sorted(counts.items())
if as_table:
- print(tabulate(rows, headers=["word", "times"]))
+ print(table(rows))
return
for word, times in rows:
print(times, word)4.2 tabulate goes
Nothing imports tabulate now, but the project still depends on it. uv remove takes it out of pyproject.toml
and uv.lock:
The author ran uv remove tabulate here. It writes pyproject.toml and uv.lock. On a later day, it can write newer versions of packages than the author's, and then git check finds that your files differ. So do not run it. Run this command in its place, from the top of the project. It takes each file as the author has it from the project, and installs exactly the versions that uv.lock names:
git restore --source 818eaf0e0f7c -- pyproject.toml uv.lock && uv sync --locked --all-groups --all-packagesuv run python -m unittest discover -s tests -qAll four tests pass, without tabulate.
Check your files against this move. The command prints nothing when they match, and otherwise shows what differs:
git check 818eaf0e0f7cIf you are stuck, this command puts the files of this move in place of yours. A file that you made yourself stays:
git restore --source 72b6c553d362 --staged -- :/ && git restore --source 818eaf0e0f7c --staged --worktree -- :/What changed
pyproject.toml: tabulate is gone. 0 lines added, 1 removed. It comes fromuv remove tabulate, and you take it from the project. The file.uv.lock: tabulate is gone. 1 line added, 14 removed. It comes fromuv remove tabulate, and you take it from the project. The file.
The diff that uv remove tabulate makes in pyproject.toml, which you take from the project
@@ -5,7 +5,6 @@ description = "Count the words in a text"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
- "tabulate>=0.9,<0.10",
"talib",
]