Skip to content

type_bridge.generator.utils.common

common

Common utility functions for code generation.

topological_sort

topological_sort(items, get_parent)

Sort items topologically so parents come before children.

This is a generic topological sort that works for any inheritance hierarchy (attributes, entities, relations) by accepting a callback to get the parent.

Parameters:

Name Type Description Default
items dict[str, T]

Dictionary mapping names to items

required
get_parent Callable[[T], str | None]

Callable that returns the parent name for an item, or None

required

Returns:

Type Description
list[str]

List of names sorted so parents come before children

Example

Sort entities by inheritance

sorted_names = topological_sort( schema.entities, lambda entity: entity.parent )

Source code in type_bridge/generator/utils/common.py
def topological_sort[T](
    items: dict[str, T],
    get_parent: Callable[[T], str | None],
) -> list[str]:
    """Sort items topologically so parents come before children.

    This is a generic topological sort that works for any inheritance hierarchy
    (attributes, entities, relations) by accepting a callback to get the parent.

    Args:
        items: Dictionary mapping names to items
        get_parent: Callable that returns the parent name for an item, or None

    Returns:
        List of names sorted so parents come before children

    Example:
        # Sort entities by inheritance
        sorted_names = topological_sort(
            schema.entities,
            lambda entity: entity.parent
        )
    """
    result: list[str] = []
    visited: set[str] = set()

    def visit(name: str) -> None:
        if name in visited or name not in items:
            return
        visited.add(name)

        item = items[name]
        parent = get_parent(item)
        if parent and parent in items:
            visit(parent)

        result.append(name)

    for name in items:
        visit(name)

    return result

group_and_sort_by_value

group_and_sort_by_value(mapping)

Group keys by their values and sort each group alphabetically.

Useful for grouping attributes by their subkey groups, etc.

Parameters:

Name Type Description Default
mapping dict[str, str]

Dictionary mapping keys to group values

required

Returns:

Type Description
dict[str, list[str]]

Dictionary mapping group values to sorted lists of keys

Example

group_and_sort_by_value({"name": "group1", "age": "group1", "id": "group2"})

Source code in type_bridge/generator/utils/common.py
def group_and_sort_by_value(mapping: dict[str, str]) -> dict[str, list[str]]:
    """Group keys by their values and sort each group alphabetically.

    Useful for grouping attributes by their subkey groups, etc.

    Args:
        mapping: Dictionary mapping keys to group values

    Returns:
        Dictionary mapping group values to sorted lists of keys

    Example:
        >>> group_and_sort_by_value({"name": "group1", "age": "group1", "id": "group2"})
        {"group1": ["age", "name"], "group2": ["id"]}
    """
    groups: dict[str, list[str]] = {}
    for key, group_name in mapping.items():
        groups.setdefault(group_name, []).append(key)

    # Sort each group
    for group in groups:
        groups[group] = sorted(groups[group])

    return groups

dedupe_preserve_order

dedupe_preserve_order(items)

Remove duplicates from a list while preserving order.

Parameters:

Name Type Description Default
items list[Any]

List that may contain duplicates

required

Returns:

Type Description
list[Any]

List with duplicates removed, original order preserved

Source code in type_bridge/generator/utils/common.py
def dedupe_preserve_order(items: list[Any]) -> list[Any]:
    """Remove duplicates from a list while preserving order.

    Args:
        items: List that may contain duplicates

    Returns:
        List with duplicates removed, original order preserved
    """
    seen: set[Any] = set()
    result: list[Any] = []
    for item in items:
        if item not in seen:
            seen.add(item)
            result.append(item)
    return result