base_result.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. from .mixin import StrMixin, JsonMixin, ImgMixin
  16. class BaseResult(dict, JsonMixin, StrMixin):
  17. """Base class for result objects that can save themselves.
  18. This class inherits from dict and provides properties and methods for handling result.
  19. """
  20. def __init__(self, data: dict) -> None:
  21. """Initializes the BaseResult with the given data.
  22. Args:
  23. data (dict): The initial data.
  24. """
  25. super().__init__(data)
  26. self._save_funcs = []
  27. StrMixin.__init__(self)
  28. JsonMixin.__init__(self)
  29. def save_all(self, save_path: str) -> None:
  30. """Calls all registered save methods with the given save path.
  31. Args:
  32. save_path (str): The path to save the result to.
  33. """
  34. for func in self._save_funcs:
  35. signature = inspect.signature(func)
  36. if "save_path" in signature.parameters:
  37. func(save_path=save_path)
  38. else:
  39. func()