-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_handler.py
More file actions
125 lines (96 loc) · 3.38 KB
/
io_handler.py
File metadata and controls
125 lines (96 loc) · 3.38 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
io_handler.py - Input/Output Module
Handles all input and output operations for the sorting application.
"""
import sys
from typing import List, Union, Any
def parse_input(input_str: str) -> List[Union[int, float, str]]:
if not input_str or not input_str.strip():
return []
data = []
for item in input_str.split():
try:
# Try integer first
data.append(int(item))
except ValueError:
try:
# Try float next
data.append(float(item))
except ValueError:
# Keep as string
data.append(item)
return data
def read_keyboard_input() -> List[Union[int, float, str]]:
try:
print("Enter items separated by spaces (or 'q' to quit):")
user_input = input(">>> ").strip()
if user_input.lower() == 'q':
return []
return parse_input(user_input)
except (EOFError, KeyboardInterrupt):
print("\nInput cancelled.")
return []
def read_file_input(filename: str) -> List[Union[int, float, str]]:
try:
with open(filename, 'r', encoding='utf-8') as file:
content = file.read().strip()
if not content:
print(f"Warning: File '{filename}' is empty.")
return []
return parse_input(content)
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
return []
except PermissionError:
print(f"Error: Permission denied reading '{filename}'.")
return []
except Exception as e:
print(f"Error reading file '{filename}': {e}")
return []
def write_screen_output(data: List[Any], label: str = "Result") -> None:
if not data:
print(f"{label}: (empty)")
else:
print(f"{label}: {' '.join(map(str, data))}")
def write_file_output(data: List[Any], filename: str) -> bool:
try:
with open(filename, 'w', encoding='utf-8') as file:
if data:
file.write(' '.join(map(str, data)) + '\n')
else:
file.write('(empty)\n')
print(f"Output written to '{filename}'")
return True
except PermissionError:
print(f"Error: Permission denied writing to '{filename}'.")
return False
except Exception as e:
print(f"Error writing to file '{filename}': {e}")
return False
def get_file_size(filename: str) -> int:
try:
import os
return os.path.getsize(filename)
except Exception:
return -1
def file_exists(filename: str) -> bool:
try:
import os
return os.path.isfile(filename)
except Exception:
return False
# Module-level test function
def test_io_module():
print("Testing I/O module...")
# Test parsing
test_input = "42 3.14 apple 99 zebra"
parsed = parse_input(test_input)
print(f"Parsed '{test_input}': {parsed}")
print(f"Types: {[type(x).__name__ for x in parsed]}")
# Test screen output
write_screen_output(parsed, "Parsed Data")
# Test empty input
empty_parsed = parse_input("")
print(f"Empty input result: {empty_parsed}")
if __name__ == "__main__":
test_io_module()