-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__getitem__1.py
More file actions
33 lines (24 loc) · 1.02 KB
/
__getitem__1.py
File metadata and controls
33 lines (24 loc) · 1.02 KB
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
from dataclasses import dataclass
@dataclass(kw_only=True)
class Fruit:
name: str
grams: float
class Basket:
def __init__(self, *, fruits: list[Fruit]) -> None:
self.fruits = fruits
def __getitem__(self, item: str) -> list[Fruit]:
return [fruit for fruit in self.fruits if fruit.name.lower() == item.lower()]
def main() -> None:
fruits: list[Fruit] = [Fruit(name='Apple', grams=2500),
Fruit(name='Apple', grams=50),
Fruit(name='Banana', grams=1000),
Fruit(name='Banana', grams=9000),
Fruit(name='Orange', grams=1500),
Fruit(name='Orange', grams=1000),
]
basket: Basket = Basket(fruits=fruits)
matches: list[Fruit] = basket['orange']
print(f'Matches: {matches}') # Matches: [Fruit(name='Orange', grams=1500), Fruit(name='Orange', grams=1000)]
print(f'Total: {len(matches)}') # Total: 2
if __name__ == '__main__':
main()