liveBook · Manning

Data integrity

As explained in Python How-To

Data integrity is the preservation of data's correctness, validity, and consistency throughout its lifecycle.

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

How Python How-To covers it

How deep it goes
Explains setter validation and file metadata checks with code, but does not inspect file contents for integrity.
Examples
Uses Task.status assignments, allowed status values, experimental .dat files, size cutoffs, and modification timestamps.
What it assumes
Assumes basic classes and properties, while introducing setter syntax and file-statistic checks through practical examples.
What you can do after
Apply it via property setters and pathlib metadata checks to reject invalid values and flag suspicious files.
How it connects
Connects property decorators, getters, setters, read-only properties, file metadata, Path.stat(), and modification timestamps.

Also explained in

Compare how 18 books explain data integrity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.