base_result.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import inspect
  15. class BaseResult(dict):
  16. """Base class for result objects that can save themselves.
  17. This class inherits from dict and provides properties and methods for handling result.
  18. """
  19. def __init__(self, data: dict) -> None:
  20. """Initializes the BaseResult with the given data.
  21. Args:
  22. data (dict): The initial data.
  23. """
  24. super().__init__(data)
  25. self._save_funcs = []
  26. def save_all(self, save_path: str) -> None:
  27. """Calls all registered save methods with the given save path.
  28. Args:
  29. save_path (str): The path to save the result to.
  30. """
  31. for func in self._save_funcs:
  32. signature = inspect.signature(func)
  33. if "save_path" in signature.parameters:
  34. func(save_path=save_path)
  35. else:
  36. func()