m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB

6 months ago
  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. # Licensed under the MIT License.
  3. import os
  4. import pathlib
  5. import typing
  6. def path_match_suffix_ignore_case(path: typing.Union[pathlib.Path, str], suffix: str) -> bool:
  7. """
  8. Returns whether `path` ends in `suffix`, ignoring case.
  9. """
  10. if not isinstance(path, str):
  11. path = str(path)
  12. return path.casefold().endswith(suffix.casefold())
  13. def files_from_file_or_dir(
  14. file_or_dir_path: typing.Union[pathlib.Path, str], predicate: typing.Callable[[pathlib.Path], bool] = lambda _: True
  15. ) -> typing.List[pathlib.Path]:
  16. """
  17. Gets the files in `file_or_dir_path` satisfying `predicate`.
  18. If `file_or_dir_path` is a file, the single file is considered. Otherwise, all files in the directory are
  19. considered.
  20. :param file_or_dir_path: Path to a file or directory.
  21. :param predicate: Predicate to determine if a file is included.
  22. :return: A list of files.
  23. """
  24. if not isinstance(file_or_dir_path, pathlib.Path):
  25. file_or_dir_path = pathlib.Path(file_or_dir_path)
  26. selected_files = []
  27. def process_file(file_path: pathlib.Path):
  28. if predicate(file_path):
  29. selected_files.append(file_path)
  30. if file_or_dir_path.is_dir():
  31. for root, _, files in os.walk(file_or_dir_path):
  32. for file in files:
  33. file_path = pathlib.Path(root, file)
  34. process_file(file_path)
  35. else:
  36. process_file(file_or_dir_path)
  37. return selected_files