Skip to content

Instantly share code, notes, and snippets.

@GDWR

GDWR/protocol.py Secret

Last active March 3, 2023 09:24
Show Gist options
  • Save GDWR/2dd090fb24c8eb87d696f931e6d76c95 to your computer and use it in GitHub Desktop.
Save GDWR/2dd090fb24c8eb87d696f931e6d76c95 to your computer and use it in GitHub Desktop.
Protocol Typing - Part Three
from typing import Protocol
import requests
class Readable(Protocol):
def read(self) -> str: ...
def write_to_file(reader: Readable, filepath: str) -> None:
with open(filepath, "w") as f:
f.write(reader.read())
with open(__file__, "r") as f:
write_to_file(f, "file.out")
class Website:
def __init__(self, url: str) -> None:
self.url = url
def read(self) -> str:
response = requests.get(self.url)
response.raise_for_status()
return response.text
my_website = Website("https://h.pythondiscord.com")
write_to_file(my_website, "website")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment