Skip to content

Commit 6c3440e

Browse files
Refactor error handling and improve code readability in example scripts
Getting ready for setup.py
1 parent 08f2e8f commit 6c3440e

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

example/hwrng_example.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@
1414
import hashlib
1515
import os
1616
import secrets
17-
import sys
1817
import time
1918
from concurrent.futures import ThreadPoolExecutor
2019

21-
# Add the parent directory to sys.path to import pyCTools
22-
# This is necessary for local testing, but once pyCTools becomes a package, this can be removed.
23-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
2420
from pyCTools import MaxRNG
2521

2622

@@ -194,20 +190,20 @@ def threaded_rng_demo() -> None:
194190
def worker_function(thread_id, size):
195191
"""Worker function that generates random bytes in a thread."""
196192
try:
197-
start_time = time.time()
193+
start_time_ = time.time()
198194
data = rng.maxrng_threadsafe(size)
199-
elapsed = time.time() - start_time
195+
elapsed = time.time() - start_time_
200196
return {
201197
"thread_id": thread_id,
202198
"data": data,
203199
"size": size,
204200
"time": elapsed,
205201
"success": True
206202
}
207-
except Exception as e:
203+
except Exception as e_:
208204
return {
209205
"thread_id": thread_id,
210-
"error": str(e),
206+
"error": str(e_),
211207
"success": False
212208
}
213209

@@ -411,15 +407,13 @@ def main() -> None:
411407
try:
412408
if os.path.exists("random_data.bin"):
413409
os.remove("random_data.bin")
414-
except:
410+
except Exception:
415411
pass
416412

417413

418414
if __name__ == "__main__":
419415
try:
420-
import ctypes # Import here for the error handling demo
421-
422416
main()
423-
except Exception as e:
424-
print(f"\n❌ Unhandled exception: {e}")
417+
except Exception as err:
418+
print(f"\n❌ Unhandled exception: {err}")
425419
print("Demonstration terminated.")

example/process_inspect_example.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
4. Interpreting and displaying the collected metrics
99
5. Proper error handling and best practices
1010
"""
11-
import os
12-
import sys
13-
import time
1411
import json
12+
import os
1513
import random
14+
import time
1615
from typing import Dict, Any
1716

18-
# Add the parent directory to sys.path to import pyCTools
19-
# This is necessary for local testing, but once pyCTools becomes a package, this can be removed.
20-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
2117
from pyCTools import ProcessMetrics
2218

2319

@@ -41,6 +37,7 @@ def simulate_workload(intensity: int = 1) -> None:
4137

4238
# Memory-intensive work
4339
print(f"Simulating memory-intensive work (level {intensity})...")
40+
# noinspection PyUnusedLocal
4441
big_list = [random.random() for _ in range(intensity * 10**5)]
4542

4643
# I/O-intensive work (file operations)
@@ -308,7 +305,6 @@ def demo_other_process_monitoring() -> None:
308305
import psutil
309306
processes_available = True
310307
except ImportError:
311-
processes_available = False
312308
print("psutil not available. Install with 'pip install psutil' to see this demo")
313309
return
314310

0 commit comments

Comments
 (0)