position_helper.py 2.2 KB

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