-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotocols.py
More file actions
39 lines (24 loc) · 806 Bytes
/
protocols.py
File metadata and controls
39 lines (24 loc) · 806 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
38
39
from abc import abstractmethod
from typing import Protocol
class Writable(Protocol):
@abstractmethod
def write(self, data: dict) -> None:
"""This method should write dictionary data."""
class Readable(Protocol):
def read(self) -> dict:
"""This method should return a dictionary"""
def do_write(writer: Writable, data: dict) -> None:
return writer.write(data)
def do_read(reader: Readable) -> dict:
return reader.read()
class Author(Writable):
def __init__(self, name: str) -> None:
self.name = name
def writable(self, data: dict) -> None:
print(f"{self.name} is writing {data}")
def main():
data = {"name": "John Doe", "age": 30}
author = Author("John doe")
do_write(author, data)
if __name__ == "__main__":
main()