1. import abc
  2. class FormalParserInterface(metaclass=abc.ABCMeta):
  3. @classmethod
  4. def __subclasshook__(cls, subclass):
  5. return (hasattr(subclass, 'load_data_source') and
  6. callable(subclass.load_data_source) and
  7. hasattr(subclass, 'extract_text') and
  8. callable(subclass.extract_text) or
  9. NotImplemented)
  10. @abc.abstractmethod
  11. def load_data_source(self, path: str, file_name: str):
  12. """Load in the data set"""
  13. raise NotImplementedError
  14. @abc.abstractmethod
  15. def extract_text(self, full_file_path: str):
  16. """Extract text from the data set"""
  17. raise NotImplementedError
  18. class PdfParserNew(FormalParserInterface):
  19. """Extract text from a PDF."""
  20. def load_data_source(self, path: str, file_name: str) -> str:
  21. """Overrides FormalParserInterface.load_data_source()"""
  22. print("i am pdfparser")
  23. pass
  24. def extract_text(self, full_file_path: str) -> dict:
  25. """Overrides FormalParserInterface.extract_text()"""
  26. pass
  27. class EmlParserNew(FormalParserInterface):
  28. """Extract text from an email."""
  29. def load_data_source(self, path: str, file_name: str) -> str:
  30. """Overrides FormalParserInterface.load_data_source()"""
  31. print("i amd eml parser")
  32. pass
  33. def extract_text(self, full_file_path: str) -> dict:
  34. """A method defined only in EmlParser.
  35. Does not override FormalParserInterface.extract_text()
  36. """
  37. pass
  38. my_pdf_parser = PdfParserNew()
  39. my_eml_parser = EmlParserNew()
  40. parser_list = [my_pdf_parser, my_eml_parser]
  41. for each_parser in parser_list:
  42. each_parser.load_data_source("", "")

参考资料

https://realpython.com/python-interface/#using-abstract-method-declaration