init.py


Overview

The init.py file provides a collection of utility functions primarily focused on file handling, data encoding, comparison, timing control, and data validation. This file appears to serve as a helper module in the larger InfiniFlow project, offering reusable tools that can be imported into other parts of the system.

Key functionalities include:


Detailed Descriptions

Function: encode_avatar

def encode_avatar(image_path)

Purpose

Converts an image file into a base64-encoded string, commonly used for embedding images in text-based formats such as JSON or HTML.

Parameters

Returns

Usage Example

encoded_image = encode_avatar("/path/to/avatar.png")
print(encoded_image)  # Outputs a base64 string

Implementation Details


Function: compare_by_hash

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

Purpose

Determines whether two files are identical by comparing their cryptographic hash values.

Parameters

Returns

Usage Example

if compare_by_hash("fileA.txt", "fileB.txt"):
    print("Files are identical.")
else:
    print("Files differ.")

Implementation Details


Function: wait_for

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

Purpose

A decorator factory that retries the decorated function repeatedly until it returns True or a timeout is reached.

Parameters

Returns

Usage Example

@wait_for(timeout=5, interval=0.5, error_msg="Operation failed after 5 seconds")
def check_condition():
    # Return True when condition met, False otherwise
    return some_condition_met()

check_condition()

Implementation Details


Function: is_sorted

def is_sorted(data, field, descending=True)

Purpose

Checks if a list of dictionaries is sorted by a specified field in either descending or ascending order.

Parameters

Returns

Usage Example

logs = [
    {"timestamp": 1620000000},
    {"timestamp": 1619999990},
    {"timestamp": 1619999980},
]

print(is_sorted(logs, "timestamp"))  # True (descending)
print(is_sorted(logs, "timestamp", descending=False))  # False (not ascending)

Implementation Details


Important Implementation Details and Algorithms


Interactions with Other System Components


Diagram: Function Flowchart

flowchart TD
    A[encode_avatar(image_path)]
    B[compare_by_hash(file1, file2, algorithm)]
    B_inner[_calc_hash(file_path)]
    C[wait_for(timeout, interval, error_msg)]
    C_decorator[decorator(func)]
    C_wrapper[wrapper(*args, **kwargs)]
    D[is_sorted(data, field, descending)]

    B --> B_inner
    C --> C_decorator --> C_wrapper

Diagram Explanation:


Summary

This init.py file is a utility module providing core reusable functions for encoding images, verifying file identity, controlled waiting/retry logic, and data sorting verification. Its design emphasizes memory efficiency, modularity, and simplicity. It is intended to be imported and leveraged by other components within the InfiniFlow system that require these foundational capabilities.