-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathmeta_classes.py
More file actions
37 lines (25 loc) · 952 Bytes
/
meta_classes.py
File metadata and controls
37 lines (25 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class ReaderMeta(type):
"""A Reader metaclass used for reader class creation."""
def __instancecheck__(cls, instance):
return cls.__subclasscheck__(type(instance))
def __subclasscheck__(cls, subclass):
return (
hasattr(subclass, "load_file")
and callable(subclass.load_file)
and hasattr(subclass, "extract_text")
and callable(subclass.extract_text)
)
class VirtualFileReader(metaclass=ReaderMeta):
"""Virtual class."""
class PdfReader:
"""Extract text from a PDF."""
def load_file(self, path: str) -> None:
print(f"Loading PDF from {path}")
def extract_text(self) -> str:
return "Extracted PDF text"
class EmailReader:
"""Extract text from an Email."""
def load_file(self, path: str) -> None:
print(f"Loading Email from {path}")
def extract_email_text(self) -> str:
return "Extracted Email text"