How can a program prevent invalid values and detect incomplete files before processing? Data integrity means preserving valid, consistent data by checking values at assignment time and screening files with metadata. section 8.3.4 section 11.5.2
For class attributes, a property setter provides a controlled assignment point. The setter accepts only approved values, while a read-only property prevents changes entirely. Use properties for validation or read-only behavior; otherwise, direct attribute access keeps Python code simpler. section 8.3.4 section 8.6
Listing 8.9 Creating a setter for a property
class Task:
# __init__ stays the same
@property
def status(self):
return self._status
@status.setter
def status(self, value):
allowed_values = ["created", "started", "completed", "suspended"]
if value in allowed_values:
self._status = value
print(f"task status set to {value}")
else:
print(f"invalid status: {value}") #1
Validate Task.status assignments with @status.setter, accepting created, started, completed, or suspended and rejecting other values. section 8.3.4
Rejecting an invalid task status
Assigning task.status = "completed" reports "task status set to completed". Assigning task.status = "random" reports "invalid status: random" instead of storing the invalid value. section 8.3.4
For experimental data, file size can provide a quick integrity check before opening or processing files. Path.stat().st_size supplies the byte count, which can be compared with an accepted range. Modification time, available through st_mtime and time.ctime(), supplies another screening signal. section 11.5.2
Listing 11.19 Creating a function to screen file sizes
def process_data_using_size_cutoff(min_size, max_size):
data_folder = Path("data")
for dat_path in data_folder.glob("*.dat"):
filename = dat_path.name
size = dat_path.stat().st_size #1
if min_size < size < max_size: #2
print(f"{filename}, Good; {size}, within
[{min_size}, {max_size}]")
else:
print(f"{filename}, Bad; {size}, outside
[{min_size}, {max_size}]")
Screen .dat files by comparing each file's byte size with minimum and maximum cutoffs, reporting each file as Good or Bad. section 11.5.2

















