testsAndMisc/linux_configuration/scripts/single_use/utils/fast_count.py
Krzysztof kuhy Rudnicki 2e1e370c0f refactor: extract all inline Python from shell scripts into proper .py files
Move every multi-line python heredoc/`-c` block into a dedicated .py file so
ruff, mypy, pylint, bandit, and pytest can apply to it:
- linux_configuration/zsh/calc-live.zsh → python_pkg/live_calc/calc_eval.py (100% branch cov, 46 tests)
- meta/scripts/check_ai_evidence.sh → meta/scripts/validate_evidence.py
- meta/scripts/check_agent_contract.sh → meta/scripts/validate_contract.py
- phone_focus_mode/lib/monitor.sh → phone_focus_mode/lib/monitor_report.py
- phone_focus_mode/deploy.sh → phone_focus_mode/strip_workout_hosts.py
- linux_configuration/.../analyze_repo.sh → fast_count.py

Also: add zsh-syntax pre-commit hook (zsh -n); exclude zsh from shellcheck;
add tests for all 4 non-python_pkg helpers; update CLAUDE.md Shell Style with
the no-inline-Python rule.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-06 10:31:48 +02:00

34 lines
1004 B
Python
Executable File

#!/usr/bin/env python3
"""Print the most common whole-line entries read from stdin.
Reads lines from stdin, counts identical (trailing-whitespace-stripped) lines,
and writes the ``argv[1]`` most common as ``<count> <line>`` rows to stdout. Used
by ``analyze_repo.sh`` as the fallback word counter when the Rust ``counts`` tool
is unavailable.
Kept as a standalone module (not inline ``python3 -c`` in the shell script) so the
repository's Python tooling applies; see CLAUDE.md "Shell Style".
"""
from __future__ import annotations
from collections import Counter
import sys
_DEFAULT_TOP_N = 50
def main() -> int:
"""Count stdin lines and print the top-N most frequent to stdout."""
args = sys.argv[1:]
top_n = int(args[0]) if args else _DEFAULT_TOP_N
counter = Counter(line.rstrip() for line in sys.stdin)
sys.stdout.write(
"".join(f"{count} {word}\n" for word, count in counter.most_common(top_n)),
)
return 0
if __name__ == "__main__":
sys.exit(main())