-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptor_pattern.py
More file actions
58 lines (44 loc) · 1.56 KB
/
adaptor_pattern.py
File metadata and controls
58 lines (44 loc) · 1.56 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from abc import *
import base64
class FileFormat(ABC):
@abstractmethod
def read(self): ...
class TxtFormat(FileFormat):
def __init__(self, content):
self.type = 'txt'
self.content = content
def read(self):
print(f"TXT: {self.content}")
class RtfLibrary():
def __init__(self, content):
self.type = 'rtf'
self.encoded_content = base64.b64encode(content.encode('utf-8')).decode('utf-8')
content = self.encoded_content
self.content = content
def get_encoded_data(self):
print(f"RTF: {self.content}")
class RtfAdaptor(RtfLibrary):
def __init__(self, content):
self.decoded = base64.b64decode(content.encode('utf-8')).decode('utf-8')
def get_data(self):
print(f"RTF(decoded): {self.decoded}")
def read_file(self, textfile: FileFormat):
textfile.read()
class FileViewer():
def read_file(self, textfile: FileFormat):
textfile.read()
class FileViewrAdaptor(FileViewer):
def read_file(self, textfile: FileFormat):
if textfile.type == 'rtf':
decoded_content = base64.b64decode(textfile.content.encode('utf-8')).decode('utf-8')
print(f"RTF (decoded): {decoded_content}")
else:
textfile.read()
if __name__ == '__main__':
txt = TxtFormat("hello wordl")
rtf = RtfLibrary("abcdefghijk")
fv = FileViewer()
rta = RtfAdaptor(rtf.content)
fv.read_file(txt)
rtf.get_encoded_data()
rta.get_data()