Skip to content

Commit

Permalink
Code formatted with Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
ABDreos authored and github-actions[bot] committed Apr 11, 2024
1 parent a0851b6 commit 59c9da3
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 55 deletions.
47 changes: 26 additions & 21 deletions 2024/protocol/abc_concrete_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,57 @@
from pickle import dumps as pickle_dumps, loads as pickle_loads
from json import dumps as json_dumps, loads as json_loads


class SerializedFileHandler(ABC):
def __init__(self, filename):
self.filename = filename

@abstractmethod
def serialize(self, data):
pass

@abstractmethod
def deserialize(self, data):
pass

def write(self, data):
with open(self.filename, 'wb') as file:
with open(self.filename, "wb") as file:
file.write(self.serialize(data))

def read(self):
with open(self.filename, 'rb') as file:
with open(self.filename, "rb") as file:
return self.deserialize(file.read())



class PickleHandler(SerializedFileHandler):
def serialize(self, data):
return pickle_dumps(data)

def deserialize(self, data):
return pickle_loads(data)



class JSONHandler(SerializedFileHandler):
def serialize(self, data):
return json_dumps(data).encode('utf-8')
return json_dumps(data).encode("utf-8")

def deserialize(self, data):
return json_loads(data.decode('utf-8'))

return json_loads(data.decode("utf-8"))


def main():
data = {'name': 'John Doe', 'age': 30}
pickle_writer = PickleHandler('data.pkl')
data = {"name": "John Doe", "age": 30}
pickle_writer = PickleHandler("data.pkl")
pickle_writer.write(data)
print(pickle_writer.read())
json_writer = JSONHandler('data.json')
json_writer.write( data)

json_writer = JSONHandler("data.json")
json_writer.write(data)
print(json_writer.read())

assert isinstance(pickle_writer, SerializedFileHandler)
assert isinstance(json_writer, SerializedFileHandler)
if __name__ == '__main__':


if __name__ == "__main__":
main()
28 changes: 15 additions & 13 deletions 2024/protocol/abc_example.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
from abc import ABC, abstractmethod


class SerializedFileHandler(ABC):

def __init__(self, filename):
self.filename = filename

@abstractmethod
def serialize(self, data: dict) -> bytes:
pass

@abstractmethod
def deserialize(self, data: bytes) -> dict:
pass

def write(self, data: dict):
with open(self.filename, 'wb') as file:
with open(self.filename, "wb") as file:
file.write(self.serialize(data))

def read(self) -> dict:
with open(self.filename, 'rb') as file:
with open(self.filename, "rb") as file:
return self.deserialize(file.read())



def main():
data = {'name': 'John Doe', 'age': 30}
file_handler = SerializedFileHandler('data.dat')
data = {"name": "John Doe", "age": 30}
file_handler = SerializedFileHandler("data.dat")
file_handler.write(data)
print(file_handler.read())

if __name__ == '__main__':


if __name__ == "__main__":
main()

6 changes: 3 additions & 3 deletions 2024/protocol/protocol_as_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ def read(self) -> dict:

class ReadWritable(Readable, Writable):
def __str__(self):
return f'{self.__class__.__name__}()'
return f"{self.__class__.__name__}()"

def write(self, data: dict) -> None:
"""Write some data."""


def main():
data = {'name': 'John Doe', 'age': 30}
data = {"name": "John Doe", "age": 30}
handlers = ReadWritable()
handlers.write(data)
print(handlers.read())


if __name__ == '__main__':
if __name__ == "__main__":
main()
12 changes: 6 additions & 6 deletions 2024/protocol/protocol_as_abc_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def deserialize(self, data):
pass

def write(self, data):
with open(self.filename, 'wb') as file:
with open(self.filename, "wb") as file:
file.write(self.serialize(data))

def read(self):
with open(self.filename, 'rb') as file:
with open(self.filename, "rb") as file:
return self.deserialize(file.read())


Expand Down Expand Up @@ -60,18 +60,18 @@ def read(handler: Readable) -> dict:


def main():
data = {'name': 'John Doe', 'age': 30}
pickle_writer = PickleHandler('../data.pkl')
data = {"name": "John Doe", "age": 30}
pickle_writer = PickleHandler("../data.pkl")
write(pickle_writer, data)
print(read(pickle_writer))

json_writer = JSONHandler('../data.json')
json_writer = JSONHandler("../data.json")
write(json_writer, data)
print(read(json_writer))

assert isinstance(pickle_writer, Writable)
assert isinstance(json_writer, Readable)


if __name__ == '__main__':
if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion 2024/protocol/protocol_as_abc_mistyping.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ def main():
# io_writer.write({'name': 'John Doe', 'age': 30})


if __name__ == '__main__':
if __name__ == "__main__":
main()
16 changes: 10 additions & 6 deletions 2024/protocol/protocol_example.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
from typing import Protocol


class Writable(Protocol):
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."""



class ReadWritable(Readable, Writable):
def __str__(self):
return f'{self.__class__.__name__}()'
return f"{self.__class__.__name__}()"


def main():
data = {'name': 'John Doe', 'age': 30}
data = {"name": "John Doe", "age": 30}
handlers = ReadWritable()
handlers.write(data)
print(handlers.read())
if __name__ == '__main__':


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""Initial tables
Revision ID: 57065631a799
Revises:
Revises:
Create Date: 2024-03-06 13:29:02.127468
"""

from typing import Sequence, Union


# revision identifiers, used by Alembic.
revision: str = '57065631a799'
revision: str = "57065631a799"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

base_router = APIRouter()

base_router.include_router(cities.router, tags=["cities"], prefix="/v1") # Define the missing variable "cities"
base_router.include_router(
cities.router, tags=["cities"], prefix="/v1"
) # Define the missing variable "cities"
base_router.include_router(weathers.router, tags=["weathers"], prefix="/v1")
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from app.models import Base
from app.database.engine import engine


# Create tables
async def create_tables():
async with engine.begin() as conn:
Expand Down
4 changes: 2 additions & 2 deletions 2024/tuesday_tips/generics/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def push(self, item: T) -> None:

def pop(self) -> T:
return self._container.pop()

def peek(self) -> Optional[T]:
if self.is_empty():
return None
return self._container[-1]

def is_empty(self) -> bool:
return self._container == []

def size(self) -> int:
return len(self._container)

0 comments on commit 59c9da3

Please sign in to comment.