position_helper.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import os
  2. from collections import OrderedDict
  3. from collections.abc import Callable
  4. from typing import Any
  5. from core.tools.utils.yaml_utils import load_yaml_file
  6. def get_position_map(folder_path: str, *, file_name: str = "_position.yaml") -> dict[str, int]:
  7. """
  8. Get the mapping from name to index from a YAML file
  9. :param folder_path:
  10. :param file_name: the YAML file name, default to '_position.yaml'
  11. :return: a dict with name as key and index as value
  12. """
  13. position_file_name = os.path.join(folder_path, file_name)
  14. positions = load_yaml_file(position_file_name, ignore_error=True)
  15. position_map = {}
  16. index = 0
  17. for _, name in enumerate(positions):
  18. if name and isinstance(name, str):
  19. position_map[name.strip()] = index
  20. index += 1
  21. return position_map
  22. def sort_by_position_map(
  23. position_map: dict[str, int],
  24. data: list[Any],
  25. name_func: Callable[[Any], str],
  26. ) -> list[Any]:
  27. """
  28. Sort the objects by the position map.
  29. If the name of the object is not in the position map, it will be put at the end.
  30. :param position_map: the map holding positions in the form of {name: index}
  31. :param name_func: the function to get the name of the object
  32. :param data: the data to be sorted
  33. :return: the sorted objects
  34. """
  35. if not position_map or not data:
  36. return data
  37. return sorted(data, key=lambda x: position_map.get(name_func(x), float('inf')))
  38. def sort_to_dict_by_position_map(
  39. position_map: dict[str, int],
  40. data: list[Any],
  41. name_func: Callable[[Any], str],
  42. ) -> OrderedDict[str, Any]:
  43. """
  44. Sort the objects into a ordered dict by the position map.
  45. If the name of the object is not in the position map, it will be put at the end.
  46. :param position_map: the map holding positions in the form of {name: index}
  47. :param name_func: the function to get the name of the object
  48. :param data: the data to be sorted
  49. :return: an OrderedDict with the sorted pairs of name and object
  50. """
  51. sorted_items = sort_by_position_map(position_map, data, name_func)
  52. return OrderedDict([(name_func(item), item) for item in sorted_items])