-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_solution.py
More file actions
96 lines (74 loc) · 2.58 KB
/
extract_solution.py
File metadata and controls
96 lines (74 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""
Extract and print typing statistics from stats.txt file.
"""
import os
import re
from pathlib import Path
def extract_stats():
"""Read stats.txt and print all statistics to console."""
# Get the project root directory (where this script is located)
script_dir = Path(__file__).parent
stats_file = script_dir / 'client' / 'stats.txt'
# Check if stats file exists
if not stats_file.exists():
print(f"Error: stats.txt not found at {stats_file}")
return
# Read the stats file
try:
with open(stats_file, 'r', encoding='utf-8') as f:
content = f.read()
except Exception as e:
print(f"Error reading stats.txt: {e}")
return
# Parse and extract statistics
stats = {}
# Extract Total Errors Made
match = re.search(r'Total Errors Made:\s*(\d+)', content)
if match:
stats['total_errors'] = int(match.group(1))
# Extract Errors Left (Unfixed)
match = re.search(r'Errors Left \(Unfixed\):\s*(\d+)', content)
if match:
stats['errors_left'] = int(match.group(1))
# Extract Total Time
match = re.search(r'Total Time:\s*([\d.]+)\s*seconds', content)
if match:
stats['total_time'] = float(match.group(1))
# Extract Accuracy
match = re.search(r'Accuracy:\s*([\d.]+)%', content)
if match:
stats['accuracy'] = float(match.group(1))
# Extract Speed (WPM)
match = re.search(r'Speed:\s*([\d.]+)\s*words per minute', content)
if match:
stats['speed'] = float(match.group(1))
# Extract Generated timestamp
match = re.search(r'Generated:\s*(.+)', content)
if match:
stats['generated'] = match.group(1).strip()
# Print all statistics
print("Typing Statistics")
print("=" * 50)
print()
if 'total_errors' in stats:
print(f"Total Errors Made: {stats['total_errors']}")
if 'errors_left' in stats:
print(f"Errors Left (Unfixed): {stats['errors_left']}")
if 'total_time' in stats:
time_value = stats['total_time']
if time_value < 60:
print(f"Total Time: {time_value:.2f} seconds")
else:
minutes = int(time_value // 60)
seconds = time_value % 60
print(f"Total Time: {minutes}m {seconds:.2f}s")
if 'accuracy' in stats:
print(f"Accuracy: {stats['accuracy']:.2f}%")
if 'speed' in stats:
print(f"Speed: {stats['speed']:.2f} words per minute")
if 'generated' in stats:
print(f"Generated: {stats['generated']}")
print()
if __name__ == '__main__':
extract_stats()