init.py


Overview

The init.py file provides a set of utility functions designed to assist with common file handling, data verification, and timing operations within the InfiniFlow project. These utilities include:

This file acts as a foundational utility module that can be imported across the project to streamline common workflows related to file processing, data integrity checks, and synchronization/waiting mechanisms.


Detailed Documentation

Functions


encode_avatar(image_path)

Encodes an image file as a base64 string.

Parameters

Returns

Description

Reads the image file in binary mode, encodes the contents using base64 encoding, and returns the result as a UTF-8 string. This is useful for embedding images directly into data streams or JSON payloads.

Usage Example

from pathlib import Path

encoded_image = encode_avatar(Path("/path/to/avatar.png"))
print(encoded_image)  # Outputs a base64 string representing the image content

compare_by_hash(file1, file2, algorithm="sha256")

Compares two files by computing their cryptographic hash digests.

Parameters

Returns

Description

The function reads both files in chunks of 8192 bytes to efficiently handle large files without loading them entirely into memory. It calculates the hash digest of each file using the specified algorithm and compares the results.

Usage Example

result = compare_by_hash("file1.txt", "file2.txt")
if result:
    print("Files are identical.")
else:
    print("Files differ.")

wait_for(timeout=10, interval=1, error_msg="Timeout")

Decorator factory that creates a decorator to repeatedly call a function until it returns True or a timeout is reached.

Parameters

Returns

Description

The decorator repeatedly invokes the decorated function until it returns True. If the function does not return True before the timeout expires, it raises an AssertionError with the specified error message. This utility is useful for polling-style waits, for example waiting for resources or conditions to become available.

Usage Example

@wait_for(timeout=5, interval=0.5, error_msg="Condition not met in time")
def check_service_ready():
    # Return True when the service is ready, False otherwise
    return service.is_ready()

# This will keep checking until the service is ready or timeout occurs
check_service_ready()

is_sorted(data, field, descending=True)

Checks if a list of dictionaries is sorted by a specific field.

Parameters

Returns

Description

Extracts the values corresponding to field from each dictionary in data and verifies whether they are sorted either descending or ascending. It uses pairwise comparison with zip to check ordering efficiently.

Usage Example

records = [
    {"timestamp": 1620000000, "value": 10},
    {"timestamp": 1610000000, "value": 20},
    {"timestamp": 1600000000, "value": 5},
]

print(is_sorted(records, "timestamp"))  # True if descending order by timestamp
print(is_sorted(records, "timestamp", descending=False))  # False

Implementation Details & Algorithms


Interaction with Other Parts of the System

This file acts as a foundational utility layer supporting higher-level business logic and system workflows.


Mermaid Diagram

classDiagram
    class encode_avatar {
        +image_path: str or Path
        +return: str (base64 encoded)
    }
    class compare_by_hash {
        +file1: str or Path
        +file2: str or Path
        +algorithm: str = "sha256"
        +return: bool
        -_calc_hash(file_path)
    }
    class wait_for {
        +timeout: int = 10
        +interval: int = 1
        +error_msg: str = "Timeout"
        +__call__(func)
        +wrapper(*args, **kwargs)
    }
    class is_sorted {
        +data: list of dict
        +field: str
        +descending: bool = True
        +return: bool
    }

Summary

The init.py file is a concise utility module providing essential file encoding, file comparison, polling, and sorting verification functions. These utilities enable consistent, reusable operations across the InfiniFlow project, helping improve code modularity and maintainability.