-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathadapter.py
62 lines (42 loc) · 1.6 KB
/
adapter.py
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
59
60
61
62
"""
An adapter design pattern is a structural design pattern that allows different
and incompatible objects to work together
A good example of an adapter design pattern is using different parsers with
completely incompatible methods and calls.
The example below uses JsonAdapter and XmlAdapter to adapt the `parse` method.
"""
from typing import Any
from xml.etree import ElementTree
import json
class JsonParser:
def load_json(self, data: str):
return json.loads(data)
class XmlParser:
def parse_xml(self, data: str):
root = ElementTree.fromstring(data)
# root = tree.getroot()
return {child.tag: child.text for child in root}
# Adapter Interface
class ParserAdapter:
parser: Any
def parse(self, data: str):
raise NotImplementedError
# Adapters for each parsers
class JSONAdapter(ParserAdapter):
def __init__(self, json_parser):
self.parser = json_parser
def parse(self, data):
return self.parser.load_json(data)
class XMLAdapter(ParserAdapter):
def __init__(self, xml_parser):
self.parser = xml_parser
def parse(self, data):
return self.parser.parse_xml(data)
if __name__ == "__main__":
# defining an adapter with different parser
json_parser = JSONAdapter(JsonParser())
xml_parser = XMLAdapter(XmlParser())
json_parsed_dict = json_parser.parse('{"name": "John Doe", "age":20}')
print(json_parsed_dict) # {'name': 'John Doe', 'age': 20}
xml_parsed_dict = xml_parser.parse("<xml><name>John Doe</name><age>20</age></xml>")
print(xml_parsed_dict) # {'name': 'John Doe', 'age': '20'}