-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_execution.py
More file actions
90 lines (73 loc) · 3.04 KB
/
Copy pathtest_execution.py
File metadata and controls
90 lines (73 loc) · 3.04 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
#!/usr/bin/env python3
"""
Test script to extract and test Python code execution from Quarto files.
"""
import re
import sys
def extract_and_test_multiprocessing():
"""Extract and test key functions from multiprocessing chapter."""
print("Testing multiprocessing chapter execution...")
try:
with open('en/book/11-multiprocessing.qmd') as f:
content = f.read()
# Test the specific functions we know should work
test_functions = {
'square_number': 'square_number(5)',
'word_count_mapper': 'word_count_mapper("hello world hello")',
'word_count_reducer': 'word_count_reducer({"hello": 2}, {"world": 1})',
'square_mapper': 'square_mapper(5)',
'sum_reducer': 'sum_reducer(10, 20)',
}
# Find and extract all code blocks
pattern = r'```\{python\}(.*?)```'
blocks = re.findall(pattern, content, re.DOTALL)
print(f"Found {len(blocks)} Python code blocks")
# Create a test environment
test_env = {}
# Execute blocks that define functions (skip execution blocks)
for i, block in enumerate(blocks, 1):
block_code = block.strip()
if not block_code:
continue
# Skip blocks that have problematic execution patterns
skip_patterns = [
'pool.map(',
'ProcessPoolExecutor',
'executor.map',
'mp.Pool(',
'multiprocessing.Pool('
]
if any(skip in block_code for skip in skip_patterns):
print(f" Block {i}: SKIPPED (multiprocessing execution)")
continue
# For blocks with if __name__ == "__main__", extract function definitions
if 'if __name__ == "__main__"' in block_code:
# Split and take only the part before if __name__
main_split = block_code.split('if __name__ == "__main__"')[0]
block_code = main_split.strip()
try:
exec(block_code, test_env)
print(f" Block {i}: EXECUTED")
except Exception as e:
print(f" Block {i}: ERROR - {e}")
# Don't fail on execution errors for complex blocks
continue
# Test specific functions
print("\nTesting specific functions:")
for func_name, test_call in test_functions.items():
if func_name in test_env:
try:
result = eval(test_call, test_env)
print(f" {func_name}: OK (result: {result})")
except Exception as e:
print(f" {func_name}: ERROR - {e}")
else:
print(f" {func_name}: NOT FOUND")
print("\nMultiprocessing chapter code validation completed!")
return True
except Exception as e:
print(f"Error: {e}")
return False
if __name__ == "__main__":
success = extract_and_test_multiprocessing()
sys.exit(0 if success else 1)