Skip to content

Commit 0fbfa35

Browse files
committed
TR updates, first round
1 parent 7f9a3e4 commit 0fbfa35

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

python-protocol/adder_v4.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Protocol, TypeVar
22

3-
T = TypeVar("T", bound=int | float | str)
3+
T = TypeVar("T", bound=int | float)
44

55

66
class Adder(Protocol[T]):
@@ -28,4 +28,4 @@ def add(adder: Adder) -> None:
2828

2929
add(IntAdder())
3030
add(FloatAdder())
31-
add(StrAdder())
31+
# add(StrAdder())

python-protocol/contents.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
from typing import List, Protocol
1+
from typing import Protocol
22

33

44
class ContentCreator(Protocol):
55
def create_content(self) -> str: ...
66

77

88
class Blogger(ContentCreator, Protocol):
9-
posts: List[str]
9+
posts: list[str]
1010

1111
def add_post(self, title: str, content: str) -> None: ...
1212

1313

1414
class Vlogger(ContentCreator, Protocol):
15-
videos: List[str]
15+
videos: list[str]
1616

1717
def add_video(self, title: str, path: str) -> None: ...
1818

python-protocol/linked_list.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33

44
class LinkedListNode(Protocol):
55
value: int
6-
next: Optional["LinkedListNode"]
6+
next_node: Optional["LinkedListNode"]
77

88
def __str__(self) -> str:
9-
return f"{self.value} -> {self.next}"
9+
return f"{self.value} -> {self.next_node}"
1010

1111

1212
class Node:
13-
def __init__(self, value: int, next: Optional["LinkedListNode"] = None):
13+
def __init__(
14+
self,
15+
value: int,
16+
next_node: Optional["LinkedListNode"] = None,
17+
):
1418
self.value = value
15-
self.next = next
19+
self.next_node = next_node
1620

1721
def __str__(self) -> str:
18-
return f"{self.value} -> {self.next}"
22+
return f"{self.value} -> {self.next_node}"
1923

2024

2125
def print_linked_list(start_node: LinkedListNode):

python-protocol/shapes_v3.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from math import pi
2+
from typing import Protocol, runtime_checkable
3+
4+
5+
@runtime_checkable
6+
class Shape(Protocol):
7+
def get_area(self) -> float: ...
8+
9+
def get_perimeter(self) -> float: ...
10+
11+
12+
class Circle:
13+
def __init__(self, radius) -> None:
14+
self.radius = radius
15+
16+
def get_area(self) -> float:
17+
return pi * self.radius**2
18+
19+
def get_perimeter(self) -> float:
20+
return 2 * pi * self.radius
21+
22+
23+
class Square:
24+
def __init__(self, side) -> None:
25+
self.side = side
26+
27+
def get_area(self) -> float:
28+
return self.side**2
29+
30+
def get_perimeter(self) -> float:
31+
return 4 * self.side
32+
33+
34+
def print_shape_info(shape: Shape):
35+
print(f"Area: {shape.get_area()}")
36+
print(f"Perimeter: {shape.get_perimeter()}")
37+
38+
39+
circle = Circle(10)
40+
square = Square(5)
41+
42+
print_shape_info(circle)
43+
print_shape_info(square)

0 commit comments

Comments
 (0)