Browse Source

typevar example (#25064)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
tags/1.8.1
Asuka Minato 1 month ago
parent
commit
462e764a3c
No account linked to committer's email address
1 changed files with 13 additions and 10 deletions
  1. 13
    10
      api/core/helper/position_helper.py

+ 13
- 10
api/core/helper/position_helper.py View File

import os import os
from collections import OrderedDict from collections import OrderedDict
from collections.abc import Callable from collections.abc import Callable
from typing import Any
from typing import TypeVar


from configs import dify_config from configs import dify_config
from core.tools.utils.yaml_utils import load_yaml_file from core.tools.utils.yaml_utils import load_yaml_file
return position_map return position_map




T = TypeVar("T")


def is_filtered( def is_filtered(
include_set: set[str], include_set: set[str],
exclude_set: set[str], exclude_set: set[str],
data: Any,
name_func: Callable[[Any], str],
data: T,
name_func: Callable[[T], str],
) -> bool: ) -> bool:
""" """
Check if the object should be filtered out. Check if the object should be filtered out.


def sort_by_position_map( def sort_by_position_map(
position_map: dict[str, int], position_map: dict[str, int],
data: list[Any],
name_func: Callable[[Any], str],
) -> list[Any]:
data: list[T],
name_func: Callable[[T], str],
):
""" """
Sort the objects by the position map. Sort the objects by the position map.
If the name of the object is not in the position map, it will be put at the end. If the name of the object is not in the position map, it will be put at the end.


def sort_to_dict_by_position_map( def sort_to_dict_by_position_map(
position_map: dict[str, int], position_map: dict[str, int],
data: list[Any],
name_func: Callable[[Any], str],
) -> OrderedDict[str, Any]:
data: list[T],
name_func: Callable[[T], str],
):
""" """
Sort the objects into a ordered dict by the position map. Sort the objects into a ordered dict by the position map.
If the name of the object is not in the position map, it will be put at the end. If the name of the object is not in the position map, it will be put at the end.
:return: an OrderedDict with the sorted pairs of name and object :return: an OrderedDict with the sorted pairs of name and object
""" """
sorted_items = sort_by_position_map(position_map, data, name_func) sorted_items = sort_by_position_map(position_map, data, name_func)
return OrderedDict([(name_func(item), item) for item in sorted_items])
return OrderedDict((name_func(item), item) for item in sorted_items)

Loading…
Cancel
Save