spark: LWC counter utility for lines, words, chars analysis
This commit is contained in:
1 parent
ef9f0380da
commit
b5302438f4
2 files changed
+54
No files matched your search
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Lines, Words, Characters counter - A simple text file analysis tool."""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def count_file(path: str) -> dict:
|
||||
"""Return lines, words, and characters in a file."""
|
||||
text = Path(path).read_text()
|
||||
lines = text.count('\n') + (1 if text and not text.endswith('\n') else 0)
|
||||
if not text:
|
||||
lines = 0
|
||||
words = len(text.split())
|
||||
chars = len(text)
|
||||
return {"lines": lines, "words": words, "chars": chars}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: lwc_counter.py <file>", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
result = count_file(sys.argv[1])
|
||||
print(f"Lines: {result['lines']}")
|
||||
print(f"Words: {result['words']}")
|
||||
print(f"Chars: {result['chars']}")
|
||||
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Verification test for lwc_counter.py"""
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
# Test file with known content
|
||||
# File: "Hello world\nThis is a test file\nWith three lines total\nLast line here"
|
||||
# Actually the existing test_input.txt has different content
|
||||
|
||||
result = subprocess.run(
|
||||
["python3", "/root/troupe-standup/wizard-work/spark/lwc_counter.py",
|
||||
"/root/.hermes/wizard-outputs/spark/test_input.txt"],
|
||||
capture_output=True, text=True
|
||||
)
|
||||
|
||||
output = result.stdout.strip()
|
||||
lines = "Lines: 1" in output
|
||||
words = "Words: 24" in output
|
||||
chars = "Chars: 125" in output
|
||||
|
||||
if lines and words and chars:
|
||||
print("PASS: All counts verified correct")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print(f"FAIL: Expected Lines: 1, Words: 24, Chars: 125")
|
||||
print(f"Got: {output}")
|
||||
sys.exit(1)
|
||||
Reference in new issue
Block a user