uf.organization#
Function grouping and organization for uf.
Provides tools for organizing functions into categories, groups, and hierarchies for better navigation in the UI.
Functions
|
Automatically group functions by their module. |
|
Automatically group functions by name prefix. |
|
Automatically group functions by a tag attribute. |
|
Create a uf app with grouped function navigation. |
Classes
|
Group of functions with metadata. |
Organize functions into groups and hierarchies. |
- class uf.organization.FunctionGroup(name, funcs=list(), description='', icon=None, order=0, collapsed=False)[source]#
Bases:
objectGroup of functions with metadata.
- funcs#
Functions in this group: callables (partials and callable instances are fine; they are named as
uf.specs.name_of_funcdoes), or a{name: callable}mapping to name them explicitly
Example
>>> def add(x: int, y: int) -> int: ... return x + y >>> def subtract(x: int, y: int) -> int: ... return x - y >>> math_funcs = FunctionGroup( ... 'Math', ... [add, subtract], ... description='Mathematical operations', ... icon='calculator' ... ) >>> list(math_funcs.named_funcs) ['add', 'subtract']
- add_function(func, *, name=None)[source]#
Add a function to this group.
- Parameters:
- Return type:
- Returns:
Self for method chaining
- Raises:
ValueError – If a different callable already has that name in the group
- class uf.organization.FunctionOrganizer[source]#
Bases:
objectOrganize functions into groups and hierarchies.
This class provides a fluent interface for building function organization structures that can be used to generate grouped navigation in the UI.
Example
>>> def user_create(name: str): ... >>> def user_delete(name: str): ... >>> def generate_report(): ... >>> organizer = FunctionOrganizer() >>> g = organizer.group('Admin', [user_create, user_delete], icon='shield') >>> g = organizer.group('Reports', [generate_report], icon='file') >>> [group.name for group in organizer.get_groups()] ['Admin', 'Reports']
- add_to_group(group_name, func)[source]#
Add a function to an existing group.
- Parameters:
- Return type:
- Returns:
Self for method chaining
- Raises:
ValueError – If group doesn’t exist
- add_ungrouped(func)[source]#
Add a function without a group.
- Parameters:
func (
Callable) – Function to add- Return type:
- Returns:
Self for method chaining
- get_groups()[source]#
Get all groups, sorted by order.
- Return type:
- Returns:
List of FunctionGroup objects sorted by order
- group(name, funcs=None, *, description='', icon=None, order=0, collapsed=False)[source]#
Create and add a function group.
- Parameters:
- Return type:
- Returns:
The created FunctionGroup
Example
>>> def save_record(record: dict): ... >>> def load_record(key: str): ... >>> organizer = FunctionOrganizer() >>> group = organizer.group( ... 'Database', ... [save_record, load_record], ... description='Database operations', ... icon='database' ... ) >>> group.icon, list(group.named_funcs) ('database', ['save_record', 'load_record'])
- to_dict()[source]#
Convert organization to dictionary format.
- Return type:
- Returns:
Dictionary representation suitable for JSON serialization
Example
>>> def ping(): ... >>> organizer = FunctionOrganizer() >>> g = organizer.group('Ops', [ping]) >>> organizer.to_dict()['groups'][0]['functions'] [{'name': 'ping', 'description': ''}]
- uf.organization.auto_group_by_module(funcs)[source]#
Automatically group functions by their module.
- Parameters:
- Return type:
- Returns:
FunctionOrganizer with module-based groups (a
functools.partialgoes with the module of the function it wraps)
Example
>>> def create(): ... >>> create.__module__ = 'myapp.user_ops' >>> def generate(): ... >>> generate.__module__ = 'myapp.report_ops' >>> organizer = auto_group_by_module([create, generate]) >>> [group.name for group in organizer.get_groups()] ['Report Ops', 'User Ops']
- uf.organization.auto_group_by_prefix(funcs, separator='_')[source]#
Automatically group functions by name prefix.
Groups functions based on the part of their name before the separator. For example, with separator=”_”:
user_create, user_delete → “user” group
report_generate, report_export → “report” group
- Parameters:
- Return type:
- Returns:
FunctionOrganizer with auto-generated groups
Example
>>> def user_create(): ... >>> def user_delete(): ... >>> def report_generate(): ... >>> def admin_reset(): ... >>> funcs = [user_create, user_delete, report_generate, admin_reset] >>> organizer = auto_group_by_prefix(funcs) >>> [group.name for group in organizer.get_groups()] ['Admin', 'Report', 'User']
- uf.organization.auto_group_by_tag(funcs, tag_attr='__uf_group__')[source]#
Automatically group functions by a tag attribute.
Functions can be tagged with a group name using an attribute.
- Parameters:
- Return type:
- Returns:
FunctionOrganizer with tag-based groups
Example
>>> def create_user(name: str): ... pass >>> create_user.__uf_group__ = 'Admin' >>> >>> def other_func(): ... >>> organizer = auto_group_by_tag([create_user, other_func]) >>> [group.name for group in organizer.get_groups()] ['Admin', 'Other']
- uf.organization.mk_grouped_app(groups, **mk_rjsf_app_kwargs)[source]#
Create a uf app with grouped function navigation.
- Parameters:
groups (
Iterable[FunctionGroup]) – Iterable of FunctionGroup objects**mk_rjsf_app_kwargs – Arguments passed to mk_rjsf_app
- Returns:
Configured web application with grouped navigation
Example
>>> def user_create(name: str): ... >>> def generate_report(): ... >>> admin_group = FunctionGroup('Admin', [user_create]) >>> reports_group = FunctionGroup('Reports', [generate_report]) >>> app = mk_grouped_app([admin_group, reports_group]) >>> type(app).__name__ 'FastAPI'