Skip to content

Commit 259c684

Browse files
author
l
committed
generic tasks
1 parent 4c4928b commit 259c684

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

‎sprint-5/10-predict.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from dataclasses import dataclass
2+
3+
@dataclass(frozen=True)
4+
class Animal:
5+
name: str
6+
species: str
7+
8+
@dataclass(frozen=True)
9+
class Person:
10+
name: str
11+
age: int
12+
13+
@dataclass(frozen=True)
14+
class FamilyTree:
15+
parent: Person
16+
members: list
17+
18+
pet = Animal(name="Gromit", species="Dog")
19+
fatma = Person(name="Fatma", age=4)
20+
aisha = Person(name="Aisha", age=6)
21+
imran = Person(name="Imran", age=30)
22+
23+
family = FamilyTree(parent=imran, members=[fatma, aisha, pet])
24+
25+
def print_family_tree(family: FamilyTree):
26+
print(family.parent.name)
27+
for child in family.members:
28+
print(f"{child.name} ({child.age} years old)")
29+
30+
print_family_tree(family)

‎sprint-5/11-fix.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from dataclasses import dataclass
2+
from typing import List
3+
4+
@dataclass(frozen=True)
5+
class Animal:
6+
name: str
7+
size: str
8+
9+
@dataclass(frozen=True)
10+
class Person:
11+
name: str
12+
age: int
13+
14+
@dataclass(frozen=True)
15+
class Tree[T]:
16+
parent: T
17+
children: List[T]
18+
19+
def print_tree(self):
20+
print(self.parent)
21+
for child in self.children:
22+
print(child)
23+
24+
fatma = Person(name="Fatma", age=4)
25+
aisha = Person(name="Aisha", age=6)
26+
imran = Person(name="Imran", age=30)
27+
family_tree = Tree[Person](parent=imran, children=[fatma, aisha])
28+
29+
cats = Animal(name="Cat", size="Small")
30+
dogs = Animal(name="Dog", size="Medium")
31+
mammals = Animal(name="Mammals", size="Variable")
32+
species_tree = Tree[Animal](parent=mammals, children=[cats, dogs])
33+
34+
family_tree.print_tree()
35+
species_tree.print_tree()
36+
37+
# Task 11:
38+
# Experiment with mypy and make sure that the family tree only takes `Person` types and the species tree only takes `Animal` types.
39+
#
40+
# We are going to add printing to the above code.
41+
#
42+
# Unlike our earlier example, we want to avoid having to create two
43+
# separate looping methods to print out an entire tree for each datatype.
44+
# So we have created a single looping function within Tree that will work for any datatype.
45+
#
46+
# Currently the `Tree.print_tree()` function doesn't do anything.
47+
#
48+
# Add some appropriate methods to each of the Animal and Person classes to allow it to work.
49+
#
50+
# **Stretch task**
51+
#
52+
# Think of another type of data that can be organised into a tree.
53+
#
54+
# Add a new class for this, instantiate some variables, and have the existing `Tree` class print it out.
55+
# Here is an example of what should be printed out
56+
'''
57+
Imran (30 years old)
58+
- Fatma (4 years old)
59+
- Aisha (6 years old))
60+
Mammals (Variable size)
61+
- Cat (Small size)
62+
- Dog (Medium size)
63+
'''

0 commit comments

Comments
 (0)