type_bridge.generator.utils.common¶
common
¶
Common utility functions for code generation.
topological_sort
¶
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
group_and_sort_by_value
¶
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
dedupe_preserve_order
¶
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 |