From 397bcb3c99f91c3c9b5109a294da66831c01e6aa Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 31 Oct 2026 01:22:14 +0000 Subject: [PATCH] spark: add wordcount utility verified against wc output --- wizard-work/spark/test_sample.txt | 9 ++--- wizard-work/spark/verify_wordcount_test.py | 40 ++++++++++++++++++++++ wizard-work/spark/wordcount.py | 39 ++++++++++----------- 3 files changed, 61 insertions(+), 27 deletions(-) create mode 100644 wizard-work/spark/verify_wordcount_test.py diff --git a/wizard-work/spark/test_sample.txt b/wizard-work/spark/test_sample.txt index 812a421..ba442ae 100644 --- a/wizard-work/spark/test_sample.txt +++ b/wizard-work/spark/test_sample.txt @@ -1,6 +1,3 @@ -# Test file with known content -This is a test. -Multiple lines here. -And a third line. - -Total: 4 lines, 8 words, 37 chars including newlines. \ No newline at end of file +Hello world +This is a test file +With three lines \ No newline at end of file diff --git a/wizard-work/spark/verify_wordcount_test.py b/wizard-work/spark/verify_wordcount_test.py new file mode 100644 index 0000000..bbb8a3b --- /dev/null +++ b/wizard-work/spark/verify_wordcount_test.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +"""Verification test for wordcount.py - must match wc output exactly.""" +import subprocess +import sys +import tempfile +import os + +# Create test file with known content +test_content = "Hello world\nThis is a test file\nWith three lines\n" +test_file = "/root/troupe-standup/test_verification.txt" +with open(test_file, 'w') as f: + f.write(test_content) + +# Get wc output +wc_result = subprocess.run(['wc', test_file], capture_output=True, text=True) +wc_lines, wc_words, wc_chars = map(int, wc_result.stdout.strip().split()[:3]) + +# Get wordcount.py output +pyc_result = subprocess.run( + ['python3', '/root/.hermes/wizard-outputs/spark/wordcount.py', test_file], + capture_output=True, text=True +) + +if pyc_result.returncode != 0: + print(f"FAIL: wordcount.py returned {pyc_result.returncode}") + print(pyc_result.stderr) + sys.exit(1) + +parts = pyc_result.stdout.strip().split() +py_lines, py_words, py_chars = int(parts[0]), int(parts[1]), int(parts[2]) + +print(f"wc: {wc_lines} lines, {wc_words} words, {wc_chars} chars") +print(f"wordcount.py: {py_lines} lines, {py_words} words, {py_chars} chars") + +if (wc_lines == py_lines and wc_words == py_words and wc_chars == py_chars): + print("PASS: outputs match") + sys.exit(0) +else: + print("FAIL: outputs do not match") + sys.exit(1) \ No newline at end of file diff --git a/wizard-work/spark/wordcount.py b/wizard-work/spark/wordcount.py index 749def1..0c71814 100644 --- a/wizard-work/spark/wordcount.py +++ b/wizard-work/spark/wordcount.py @@ -1,31 +1,28 @@ #!/usr/bin/env python3 -"""Word count utility - counts words in a file and outputs JSON result.""" -import json +"""Simple word/line/char counter for files.""" import sys from pathlib import Path -def word_count(filepath): - """Count words in file and return stats.""" - path = Path(filepath) - if not path.exists(): - return {"error": f"File not found: {filepath}"} - - content = path.read_text() - words = content.split() - lines = content.splitlines() - + +def count_file(path: str) -> dict: + """Return line, word, and char counts for a file (wc-style).""" + content = Path(path).read_text() + # wc -l counts newline characters, not lines return { - "file": str(path), - "bytes": path.stat().st_size, - "lines": len(lines), - "words": len(words), - "characters": len(content) + 'lines': content.count('\n'), + 'words': len(content.split()), + 'chars': len(content) } -if __name__ == "__main__": + +def main(): if len(sys.argv) < 2: - print(json.dumps({"error": "Usage: wordcount.py "})) + print("Usage: wordcount.py ", file=sys.stderr) sys.exit(1) - result = word_count(sys.argv[1]) - print(json.dumps(result, indent=2)) \ No newline at end of file + result = count_file(sys.argv[1]) + print(f"{result['lines']} {result['words']} {result['chars']} {sys.argv[1]}") + + +if __name__ == '__main__': + main() \ No newline at end of file