stouputils.collections package#

This module provides utilities for collection manipulation:

  • Registry - Dictionary that registers any object by decorator

  • unique_list() - Remove duplicates from a list while preserving order using object id, hash or str

  • at_least_n() - Check if at least n elements in an iterable satisfy a given predicate

  • sort_dict_keys() - Sort dictionary keys using a given order list (ascending or descending)

  • affine_permutation_generator() - Generate a memory-efficient pseudo-random permutation of [0, n)

  • feistel_permutation_generator() - Memory-efficient pseudo-random permutation of [0, n) from a Feistel network

  • upsert_in_dataframe() - Insert or update a row in a Polars DataFrame based on primary keys

import stouputils as stp

# Remove duplicates, keeping the first occurrence of each value
stp.info(stp.unique_list([3, 1, 3, 2, 1]))

# Are at least 2 of these files Python files?
def is_python_file(name: str) -> bool:
        return name.endswith(".py")

stp.info(stp.at_least_n(["a.py", "b.txt", "c.py"], is_python_file, n=2))

# Put the keys of a dictionary in a chosen order
settings = {"lr": 0.1, "seed": 42, "model": "resnet"}
stp.info(stp.sort_dict_keys(settings, order=["model", "lr", "seed"]))

# Shuffle the numbers 0 to 9, the same way every time for a given seed
stp.info(list(stp.affine_permutation_generator(10, seed=7)))

# Register functions under their name, then call one by its name
MODELS = stp.Registry()

@MODELS.register
def resnet() -> str:
        return "ResNet-50"

stp.info(MODELS["resnet"]())
Terminal output of the example

Submodules#