how to make a person: tests and solution
how to make a person with strings: tests
The code in
person/tests/test_person.pyfrom how to make a person with strings1def joe(): 2 return 'joe, blow, M, 1996' 3 4 5def jane(): 6 return 'jane, doe, F, 1991' 7 8 9def john(): 10 return 'john, smith, M, 1580' 11 12 13def mary(): 14 return 'mary, public, F, 2000' 15 16 17def test_joe(): 18 assert joe() == 'joe, blow, M, 1996' 19 20 21def test_jane(): 22 assert jane() == 'jane, doe, F, 1991' 23 24 25def test_john(): 26 assert john() == 'john, smith, M, 1580' 27 28 29def test_mary(): 30 assert mary() == 'mary, public, F, 2000' 31 32 33# Exceptions seen 34# AssertionError 35# NameError 36# TypeError
how to make a person with f-strings: tests and solution
how to make a person with f-strings: tests
The code in person/tests/test_person.py from how to make a person with f-strings
1import src.person
2
3
4def assert_say_hello_works(
5 first_name, last_name,
6 year_of_birth,
7 ):
8 reality = src.person.say_hello(
9 first_name=first_name,
10 last_name=last_name,
11 year_of_birth=year_of_birth,
12 )
13 my_expectation = (
14 f'Hello, my name is {first_name}'
15 f' {last_name} and I am'
16 f' {2026-year_of_birth}.'
17 )
18 assert reality == my_expectation
21def assert_person_factory_works(
22 first_name, last_name,
23 sex, year_of_birth
24 ):
25 reality = src.person.person(
26 first_name=first_name,
27 last_name=last_name,
28 sex=sex,
29 year_of_birth=year_of_birth,
30 )
31 my_expectation = (
32 f'{first_name}, {last_name},'
33 f' {sex}, {year_of_birth}'
34 )
35 assert reality == my_expectation
38def test_joe():
39 first_name = 'joe'
40 last_name = 'blow'
41 sex = 'M'
42 year_of_birth = 1996
43
44 assert_person_factory_works(
45 first_name=first_name,
46 last_name=last_name,
47 sex=sex,
48 year_of_birth=year_of_birth,
49 )
50
51 assert_say_hello_works(
52 first_name=first_name,
53 last_name=last_name,
54 year_of_birth=year_of_birth,
55 )
58def test_jane():
59 first_name = 'jane'
60 last_name = 'doe'
61 sex = 'F'
62 year_of_birth = 1991
63
64 assert_person_factory_works(
65 first_name=first_name,
66 last_name=last_name,
67 sex=sex,
68 year_of_birth=year_of_birth,
69 )
70
71 assert_say_hello_works(
72 first_name=first_name,
73 last_name=last_name,
74 year_of_birth=year_of_birth,
75 )
78def test_john():
79 first_name = 'john'
80 last_name = 'smith'
81 sex = 'M'
82 year_of_birth = 1580
83
84 assert_person_factory_works(
85 first_name=first_name,
86 last_name=last_name,
87 sex=sex,
88 year_of_birth=year_of_birth,
89 )
90
91 assert_say_hello_works(
92 first_name=first_name,
93 last_name=last_name,
94 year_of_birth=year_of_birth,
95 )
98def test_mary():
99 first_name = 'mary'
100 last_name = 'public'
101 sex = 'F'
102 year_of_birth = 2000
103
104 assert_person_factory_works(
105 first_name=first_name,
106 last_name=last_name,
107 sex=sex,
108 year_of_birth=year_of_birth,
109 )
110
111 assert_say_hello_works(
112 first_name=first_name,
113 last_name=last_name,
114 year_of_birth=year_of_birth,
115 )
116
117
118# Exceptions seen
119# AssertionError
120# NameError
121# TypeError
122# AttributeError
how to make a person with f-strings: solutions
The code in person/src/person/__init__.py from how to make a person with f-strings
1def say_hello(
2 first_name, last_name, year_of_birth
3 ):
4 return (
5 f'Hello, my name is {first_name}'
6 f' {last_name} and I am'
7 f' {2026-year_of_birth}.'
8 )
how to make a person with an object: tests and solution
how to make a person with an object: tests
The code in person/tests/test_person.py from how to make a person with an object
1import src.person
2
3
4def assert_equal(left, right):
5 assert left == right
8def assert_person_can_say_hello(
9 first_name, last_name,
10 sex, year_of_birth,
11 ):
12 assert_equal(
13 src.person.Person(
14 first_name=first_name,
15 last_name=last_name,
16 sex=sex,
17 year_of_birth=year_of_birth,
18 ).say_hello(),
19 (
20 f'Hello, my name is {first_name}'
21 f' {last_name} and I am'
22 f' {2026-year_of_birth}.'
23 )
24 )
27def assert_say_hello_works(
28 first_name, last_name,
29 year_of_birth,
30 ):
31 assert_equal(
32 src.person.say_hello(
33 first_name=first_name,
34 last_name=last_name,
35 year_of_birth=year_of_birth
36 ),
37 (
38 f'Hello, my name is {first_name}'
39 f' {last_name} and I am'
40 f' {2026-year_of_birth}.'
41 )
42 )
45def assert_person_factory_works(
46 first_name, last_name,
47 sex, year_of_birth
48 ):
49 assert_equal(
50 src.person.person(
51 first_name=first_name,
52 last_name=last_name,
53 sex=sex,
54 year_of_birth=year_of_birth,
55 ),
56 (
57 f'{first_name}, {last_name},'
58 f' {sex}, {year_of_birth}'
59 )
60 )
63def test_joe():
64 first_name = 'joe'
65 last_name = 'blow'
66 sex = 'M'
67 year_of_birth = 1996
68
69 assert_person_factory_works(
70 first_name=first_name,
71 last_name=last_name,
72 sex=sex,
73 year_of_birth=year_of_birth
74 )
75
76 assert_say_hello_works(
77 first_name=first_name,
78 last_name=last_name,
79 year_of_birth=year_of_birth,
80 )
81
82 assert_person_can_say_hello(
83 first_name=first_name,
84 last_name=last_name,
85 sex=sex,
86 year_of_birth=year_of_birth
87 )
90def test_jane():
91 first_name = 'jane'
92 last_name = 'doe'
93 sex = 'F'
94 year_of_birth = 1991
95
96 assert_person_factory_works(
97 first_name=first_name,
98 last_name=last_name,
99 sex=sex,
100 year_of_birth=year_of_birth,
101 )
102
103 assert_say_hello_works(
104 first_name=first_name,
105 last_name=last_name,
106 year_of_birth=year_of_birth,
107 )
108
109 assert_person_can_say_hello(
110 first_name=first_name,
111 last_name=last_name,
112 sex=sex,
113 year_of_birth=year_of_birth,
114 )
117def test_john():
118 first_name = 'john'
119 last_name = 'smith'
120 sex = 'M'
121 year_of_birth = 1580
122
123 assert_person_factory_works(
124 first_name=first_name,
125 last_name=last_name,
126 sex=sex,
127 year_of_birth=year_of_birth,
128 )
129
130 assert_say_hello_works(
131 first_name=first_name,
132 last_name=last_name,
133 year_of_birth=year_of_birth,
134 )
135
136 assert_person_can_say_hello(
137 first_name=first_name,
138 last_name=last_name,
139 sex=sex,
140 year_of_birth=year_of_birth,
141 )
144def test_mary():
145 first_name = 'mary'
146 last_name = 'public'
147 sex = 'F'
148 year_of_birth = 2000
149
150 assert_person_factory_works(
151 first_name=first_name,
152 last_name=last_name,
153 sex=sex,
154 year_of_birth=year_of_birth,
155 )
156
157 assert_say_hello_works(
158 first_name=first_name,
159 last_name=last_name,
160 year_of_birth=year_of_birth,
161 )
162
163 assert_person_can_say_hello(
164 first_name=first_name,
165 last_name=last_name,
166 sex=sex,
167 year_of_birth=year_of_birth,
168 )
171def test_dir_person_class():
172 assert_equal(
173 dir(src.person.Person),
174 [
175 '__class__', '__delattr__', '__dict__',
176 '__dir__', '__doc__', '__eq__',
177 '__firstlineno__', '__format__', '__ge__',
178 '__getattribute__', '__getstate__', '__gt__',
179 '__hash__', '__init__', '__init_subclass__',
180 '__le__', '__lt__', '__module__', '__ne__',
181 '__new__', '__reduce__', '__reduce_ex__',
182 '__repr__', '__setattr__', '__sizeof__',
183 '__static_attributes__', '__str__',
184 '__subclasshook__', '__weakref__', 'say_hello'
185 ]
186 )
188def test_dir_person_instance():
189 assert_equal(
190 dir(
191 src.person.Person(
192 first_name='first_name',
193 last_name='last_name',
194 sex='M',
195 year_of_birth=2026,
196 )
197 ),
198 [
199 '__class__', '__delattr__', '__dict__',
200 '__dir__', '__doc__', '__eq__',
201 '__firstlineno__', '__format__', '__ge__',
202 '__getattribute__', '__getstate__', '__gt__',
203 '__hash__', '__init__', '__init_subclass__',
204 '__le__', '__lt__', '__module__', '__ne__',
205 '__new__', '__reduce__', '__reduce_ex__',
206 '__repr__', '__setattr__', '__sizeof__',
207 '__static_attributes__', '__str__',
208 '__subclasshook__', '__weakref__', 'first_name',
209 'last_name', 'say_hello', 'sex', 'year_of_birth',
210 ]
211 )
212
213
214# Exceptions seen
215# AssertionError
216# NameError
217# TypeError
218# AttributeError
219# SyntaxError
how to make a person with an object: solutions
The code in person/src/person/__init__.py from how to make a person with an object
1class Person:
2
3 def __init__(
4 self, first_name, last_name,
5 sex, year_of_birth,
6 ):
7 self.first_name = first_name
8 self.last_name = last_name
9 self.year_of_birth = year_of_birth
10 self.sex = sex
11
12 def say_hello(self):
13 return (
14 f'Hello, my name is {self.first_name}'
15 f' {self.last_name} and I am'
16 f' {2026-self.year_of_birth}.'
17 )
20def say_hello(
21 first_name, last_name, year_of_birth
22 ):
23 return (
24 f'Hello, my name is {first_name}'
25 f' {last_name} and I am'
26 f' {2026-year_of_birth}.'
27 )
28
29
30def person(
31 first_name, last_name,
32 sex, year_of_birth,
33 ):
34 return (
35 f'{first_name}, {last_name},'
36 f' {sex}, {year_of_birth}'
37 )
test person with unittest: tests
The code in person/tests/test_person.py from test person with unittest
1import src.person
2import unittest
3
4
5class TestPerson(unittest.TestCase):
6
7 def assert_person_factory_works(
8 self, first_name, last_name,
9 sex, year_of_birth
10 ):
11 self.assertEqual(
12 src.person.person(
13 first_name=first_name,
14 last_name=last_name,
15 sex=sex,
16 year_of_birth=year_of_birth,
17 ),
18 (
19 f'{first_name}, {last_name},'
20 f' {sex}, {year_of_birth}'
21 )
22 )
24 def assert_say_hello_works(
25 self, first_name, last_name,
26 year_of_birth,
27 ):
28 self.assertEqual(
29 src.person.say_hello(
30 first_name=first_name,
31 last_name=last_name,
32 year_of_birth=year_of_birth
33 ),
34 (
35 f'Hello, my name is {first_name}'
36 f' {last_name} and I am'
37 f' {2026-year_of_birth}.'
38 )
39 )
41 def assert_person_can_say_hello(
42 self,first_name, last_name,
43 sex, year_of_birth,
44 ):
45 self.assertEqual(
46 src.person.Person(
47 first_name=first_name,
48 last_name=last_name,
49 sex=sex,
50 year_of_birth=year_of_birth,
51 ).say_hello(),
52 (
53 f'Hello, my name is {first_name}'
54 f' {last_name} and I am'
55 f' {2026-year_of_birth}.'
56 )
57 )
59 def test_joe(self):
60 first_name = 'joe'
61 last_name = 'blow'
62 sex = 'M'
63 year_of_birth = 1996
64
65 self.assert_person_factory_works(
66 first_name=first_name,
67 last_name=last_name,
68 sex=sex,
69 year_of_birth=year_of_birth
70 )
71
72 self.assert_say_hello_works(
73 first_name=first_name,
74 last_name=last_name,
75 year_of_birth=year_of_birth,
76 )
77
78 self.assert_person_can_say_hello(
79 first_name=first_name,
80 last_name=last_name,
81 sex=sex,
82 year_of_birth=year_of_birth
83 )
85 def test_jane(self):
86 first_name = 'jane'
87 last_name = 'doe'
88 sex = 'F'
89 year_of_birth = 1991
90
91 self.assert_person_factory_works(
92 first_name=first_name,
93 last_name=last_name,
94 sex=sex,
95 year_of_birth=year_of_birth,
96 )
97
98 self.assert_say_hello_works(
99 first_name=first_name,
100 last_name=last_name,
101 year_of_birth=year_of_birth,
102 )
103
104 self.assert_person_can_say_hello(
105 first_name=first_name,
106 last_name=last_name,
107 sex=sex,
108 year_of_birth=year_of_birth,
109 )
111 def test_john(self):
112 first_name = 'john'
113 last_name = 'smith'
114 sex = 'M'
115 year_of_birth = 1580
116
117 self.assert_person_factory_works(
118 first_name=first_name,
119 last_name=last_name,
120 sex=sex,
121 year_of_birth=year_of_birth,
122 )
123
124 self.assert_say_hello_works(
125 first_name=first_name,
126 last_name=last_name,
127 year_of_birth=year_of_birth,
128 )
129
130 self.assert_person_can_say_hello(
131 first_name=first_name,
132 last_name=last_name,
133 sex=sex,
134 year_of_birth=year_of_birth,
135 )
137 def test_mary(self):
138 first_name = 'mary'
139 last_name = 'public'
140 sex = 'F'
141 year_of_birth = 2000
142
143 self.assert_person_factory_works(
144 first_name=first_name,
145 last_name=last_name,
146 sex=sex,
147 year_of_birth=year_of_birth,
148 )
149
150 self.assert_say_hello_works(
151 first_name=first_name,
152 last_name=last_name,
153 year_of_birth=year_of_birth,
154 )
155
156 self.assert_person_can_say_hello(
157 first_name=first_name,
158 last_name=last_name,
159 sex=sex,
160 year_of_birth=year_of_birth,
161 )
163 def test_dir_person_class(self):
164 self.assertEqual(
165 dir(src.person.Person),
166 [
167 '__class__', '__delattr__', '__dict__',
168 '__dir__', '__doc__', '__eq__',
169 '__firstlineno__', '__format__', '__ge__',
170 '__getattribute__', '__getstate__', '__gt__',
171 '__hash__', '__init__', '__init_subclass__',
172 '__le__', '__lt__', '__module__', '__ne__',
173 '__new__', '__reduce__', '__reduce_ex__',
174 '__repr__', '__setattr__', '__sizeof__',
175 '__static_attributes__', '__str__',
176 '__subclasshook__', '__weakref__', 'say_hello'
177 ]
178 )
180 def test_dir_person_instance(self):
181 self.assertEqual(
182 dir(
183 src.person.Person(
184 first_name='first_name',
185 last_name='last_name',
186 sex='M',
187 year_of_birth=2026,
188 )
189 ),
190 [
191 '__class__', '__delattr__', '__dict__',
192 '__dir__', '__doc__', '__eq__',
193 '__firstlineno__', '__format__', '__ge__',
194 '__getattribute__', '__getstate__', '__gt__',
195 '__hash__', '__init__', '__init_subclass__',
196 '__le__', '__lt__', '__module__', '__ne__',
197 '__new__', '__reduce__', '__reduce_ex__',
198 '__repr__', '__setattr__', '__sizeof__',
199 '__static_attributes__', '__str__',
200 '__subclasshook__', '__weakref__', 'first_name',
201 'last_name', 'say_hello', 'sex', 'year_of_birth',
202 ]
203 )
204
205
206# Exceptions seen
207# AssertionError
208# NameError
209# TypeError
210# AttributeError
211# SyntaxError
test person with datetime: tests and solutions
test person with datetime: tests
The code added to person/tests/test_person.py from test person with datetime
1import datetime
2import src.person
3import unittest
4
5
6class TestPerson(unittest.TestCase):
7
8 this_year = datetime.date.today().year
9
10 def assert_person_factory_works(
11 self, first_name, last_name,
12 sex, year_of_birth
13 ):
27 def calculate_age(self, year_of_birth):
28 return self.this_year - year_of_birth
30 def assert_say_hello_works(
31 self, first_name, last_name,
32 year_of_birth,
33 ):
34 self.assertEqual(
35 src.person.say_hello(
36 first_name=first_name,
37 last_name=last_name,
38 year_of_birth=year_of_birth
39 ),
40 (
41 f'Hello, my name is {first_name}'
42 f' {last_name} and I am'
43 f' {self.calculate_age(year_of_birth)}.'
44 )
45 )
47 def assert_person_can_say_hello(
48 self,first_name, last_name,
49 sex, year_of_birth,
50 ):
51 self.assertEqual(
52 src.person.Person(
53 first_name=first_name,
54 last_name=last_name,
55 sex=sex,
56 year_of_birth=year_of_birth,
57 ).say_hello(),
58 (
59 f'Hello, my name is {first_name}'
60 f' {last_name} and I am'
61 f' {self.calculate_age(year_of_birth)}.'
62 )
63 )
117 def test_john(self):
118 first_name = 'john'
119 last_name = 'smith'
120 sex = 'M'
121 year_of_birth = 1980
122
123 self.assert_person_factory_works(
124 first_name=first_name,
125 last_name=last_name,
126 sex=sex,
127 year_of_birth=year_of_birth,
128 )
129
130 self.assert_say_hello_works(
131 first_name=first_name,
132 last_name=last_name,
133 year_of_birth=year_of_birth,
134 )
135
136 self.assert_person_can_say_hello(
137 first_name=first_name,
138 last_name=last_name,
139 sex=sex,
140 year_of_birth=year_of_birth,
141 )
169 def test_when_person_is_older_than_120(self):
170 src.person.Person(
171 first_name='first_name',
172 last_name='last_name',
173 sex='M',
174 year_of_birth=self.this_year-121
175 )
176 # ).say_hello() fails
177 # because age > 120
179 def test_when_year_of_birth_is_the_future(self):
180 src.person.Person(
181 first_name='first_name',
182 last_name='last_name',
183 sex='F',
184 year_of_birth=self.this_year+1,
185 )
186 # ).say_hello() fails
187 # because age < 0
189 def test_when_year_of_birth_is_not_an_integer(self):
190 src.person.Person(
191 first_name='first_name',
192 last_name='last_name',
193 sex='M',
194 # year_of_birth=None, # fails
195 # year_of_birth=2026.0, # fails
196 # year_of_birth='2026', # fails
197 # year_of_birth=(2026,), # fails
198 )
199 # ).say_hello() fails
200 # because year_of_birth is not an integer
201
202 def test_dir_person_class(self):
test person with datetime: solutions
The code in person/src/person.py from test person with datetime
1import datetime
2
3
4class Person:
5
6 def __init__(
7 self, first_name, last_name,
8 sex, year_of_birth=None,
9 ):
10 self.first_name = first_name
11 self.last_name = last_name
12 self.year_of_birth = year_of_birth
13 self.sex = sex
15 def say_hello(self):
16 return (
17 f'Hello, my name is {self.first_name}'
18 f' {self.last_name} and I am'
19 f' {calculate_age(self.year_of_birth)}.'
20 )
23def calculate_age(year_of_birth):
24 assert isinstance(year_of_birth, int)
25 age = (
26 datetime.date.today().year
27 - year_of_birth
28 )
29 assert age >= 0
30 assert age <= 120
31 return age
34def say_hello(
35 first_name, last_name, year_of_birth
36 ):
37 return (
38 f'Hello, my name is {first_name}'
39 f' {last_name} and I am'
40 f' {calculate_age(year_of_birth)}.'
41 )
how to make a person with conditions: tests and solutions
how to make a person with conditions: tests
The code in person/tests/test_person.py from how to make a person with conditions
65 def test_joe(self):
66 first_name = 'joe'
67 last_name = 'blow'
68 sex = 'M'
69 year_of_birth = 1996
70
71 self.assert_person_factory_works(
72 first_name=first_name,
73 last_name=last_name,
74 sex=sex,
75 year_of_birth=year_of_birth
76 )
77
78 self.assert_say_hello_works(
79 first_name=first_name,
80 last_name=last_name,
81 year_of_birth=year_of_birth,
82 )
83
84 self.assert_person_can_say_hello(
85 first_name=first_name,
86 last_name=last_name,
87 sex=sex,
88 year_of_birth=year_of_birth
89 )
90
91 joe = src.person.Person(
92 first_name=first_name,
93 last_name=last_name,
94 sex=sex,
95 year_of_birth=year_of_birth,
96 )
97 self.assertEqual(joe.can_vote(), True)
98 self.assertEqual(joe.can_get_license(), False)
100 def test_jane(self):
101 first_name = 'jane'
102 last_name = 'doe'
103 sex = 'F'
104 year_of_birth = 1991
105
106 self.assert_person_factory_works(
107 first_name=first_name,
108 last_name=last_name,
109 sex=sex,
110 year_of_birth=year_of_birth,
111 )
112
113 self.assert_say_hello_works(
114 first_name=first_name,
115 last_name=last_name,
116 year_of_birth=year_of_birth,
117 )
118
119 self.assert_person_can_say_hello(
120 first_name=first_name,
121 last_name=last_name,
122 sex=sex,
123 year_of_birth=year_of_birth,
124 )
125
126 jane = src.person.Person(
127 first_name=first_name,
128 last_name=last_name,
129 sex=sex,
130 year_of_birth=year_of_birth,
131 passed_test=True,
132 )
133 self.assertEqual(jane.can_vote(), True)
134 self.assertEqual(jane.can_get_license(), True)
136 def test_john(self):
137 first_name = 'john'
138 last_name = 'smith'
139 sex = 'M'
140 year_of_birth = 1980
141
142 self.assert_person_factory_works(
143 first_name=first_name,
144 last_name=last_name,
145 sex=sex,
146 year_of_birth=year_of_birth,
147 )
148
149 self.assert_say_hello_works(
150 first_name=first_name,
151 last_name=last_name,
152 year_of_birth=year_of_birth,
153 )
154
155 self.assert_person_can_say_hello(
156 first_name=first_name,
157 last_name=last_name,
158 sex=sex,
159 year_of_birth=year_of_birth,
160 )
161
162 john = src.person.Person(
163 first_name=first_name,
164 last_name=last_name,
165 sex=sex,
166 year_of_birth=year_of_birth,
167 is_citizen=False,
168 passed_test=False,
169 )
170 self.assertEqual(john.can_vote(), False)
171 self.assertEqual(john.can_get_license(), False)
173 def test_mary(self):
174 first_name = 'mary'
175 last_name = 'public'
176 sex = 'F'
177 year_of_birth = 2000
178
179 self.assert_person_factory_works(
180 first_name=first_name,
181 last_name=last_name,
182 sex=sex,
183 year_of_birth=year_of_birth,
184 )
185
186 self.assert_say_hello_works(
187 first_name=first_name,
188 last_name=last_name,
189 year_of_birth=year_of_birth,
190 )
191
192 self.assert_person_can_say_hello(
193 first_name=first_name,
194 last_name=last_name,
195 sex=sex,
196 year_of_birth=year_of_birth,
197 )
198
199 mary = src.person.Person(
200 first_name=first_name,
201 last_name=last_name,
202 sex=sex,
203 year_of_birth=year_of_birth,
204 is_citizen=False,
205 passed_test=True,
206 )
207 self.assertEqual(mary.can_vote(), False)
208 self.assertEqual(mary.can_get_license(), True)
210 def test_underage_citizen(self):
211 underage = src.person.Person(
212 first_name='first_name',
213 last_name='last_name',
214 sex='M',
215 year_of_birth=self.this_year-17,
216 is_citizen=True,
217 passed_test=True,
218 )
219 self.assertEqual(underage.can_vote(), False)
220 self.assertEqual(underage.can_get_license(), False)
222 @unittest.skip('fails because age > 120')
223 def test_when_person_is_older_than_120(self):
224 src.person.Person(
225 first_name='first_name',
226 last_name='last_name',
227 sex='M',
228 year_of_birth=self.this_year-121
229 )
231 @unittest.skip('fails because age < 0')
232 def test_when_year_of_birth_is_the_future(self):
233 src.person.Person(
234 first_name='first_name',
235 last_name='last_name',
236 sex='F',
237 year_of_birth=self.this_year+1,
238 )
240 @unittest.skip(
241 'fails because year_of_birth is not an integer'
242 )
243 def test_when_year_of_birth_is_not_an_integer(self):
244 src.person.Person(
245 first_name='first_name',
246 last_name='last_name',
247 sex='M',
248 # year_of_birth=None, # fails
249 # year_of_birth=2026.0, # fails
250 # year_of_birth='2026', # fails
251 # year_of_birth=(2026,), # fails
252 )
254 def test_dir_person_class(self):
255 self.assertEqual(
256 dir(src.person.Person),
257 [
258 '__class__', '__delattr__', '__dict__',
259 '__dir__', '__doc__', '__eq__',
260 '__firstlineno__', '__format__', '__ge__',
261 '__getattribute__', '__getstate__', '__gt__',
262 '__hash__', '__init__', '__init_subclass__',
263 '__le__', '__lt__', '__module__', '__ne__',
264 '__new__', '__reduce__', '__reduce_ex__',
265 '__repr__', '__setattr__', '__sizeof__',
266 '__static_attributes__', '__str__',
267 '__subclasshook__', '__weakref__',
268 'can_get_license', 'can_vote', 'check_age',
269 'say_hello',
270 ]
271 )
273 def test_dir_person_instance(self):
274 self.assertEqual(
275 dir(
276 src.person.Person(
277 first_name='first_name',
278 last_name='last_name',
279 sex='M',
280 year_of_birth=2026,
281 )
282 ),
283 [
284 '__class__', '__delattr__', '__dict__',
285 '__dir__', '__doc__', '__eq__',
286 '__firstlineno__', '__format__', '__ge__',
287 '__getattribute__', '__getstate__', '__gt__',
288 '__hash__', '__init__', '__init_subclass__',
289 '__le__', '__lt__', '__module__', '__ne__',
290 '__new__', '__reduce__', '__reduce_ex__',
291 '__repr__', '__setattr__', '__sizeof__',
292 '__static_attributes__', '__str__',
293 '__subclasshook__', '__weakref__',
294 'age', 'can_get_license', 'can_vote',
295 'check_age', 'first_name', 'is_citizen',
296 'last_name', 'passed_test', 'say_hello',
297 'sex', 'year_of_birth',
298 ]
299 )
300
301
302# Exceptions seen
303# AssertionError
304# NameError
305# TypeError
306# AttributeError
307# SyntaxError
how to make a person with conditions: solutions
The code in person/src/person.py after how to make a person with conditions
1import datetime
2
3
4class Person:
5
6 def __init__(
7 self, first_name, last_name,
8 sex, year_of_birth=None,
9 is_citizen=True, passed_test=False,
10 ):
11 self.first_name = first_name
12 self.last_name = last_name
13 self.year_of_birth = year_of_birth
14 self.sex = sex
15 self.is_citizen = is_citizen
16 self.passed_test = passed_test
17 self.age = calculate_age(year_of_birth)
19 def check_age(self, status):
20 if self.age < 18:
21 return False
22 return status
23
24 def can_get_license(self):
25 return self.check_age(self.passed_test)
27 def can_vote(self):
28 return self.check_age(self.is_citizen)
29
30 def say_hello(self):
31 return (
32 f'Hello, my name is {self.first_name}'
33 f' {self.last_name} and I am {self.age}.'
34 )
37def calculate_age(year_of_birth):
38 assert isinstance(year_of_birth, int)
39 age = (
40 datetime.date.today().year
41 - year_of_birth
42 )
43 assert age >= 0
44 assert age <= 120
45 return age
48def say_hello(
49 first_name, last_name, year_of_birth
50 ):
51 return (
52 f'Hello, my name is {first_name}'
53 f' {last_name} and I am'
54 f' {calculate_age(year_of_birth)}.'
55 )
how to make a person with Exceptions: tests and solutions
how to make a person with Exceptions: tests
The code in person/tests/test_person.py from how to make a person with Exceptions
1import datetime
2import src.person
3import unittest
4
5
6class TestPerson(unittest.TestCase):
7
8 @staticmethod
9 def calculate_age(year_of_birth):
10 return (
11 datetime.date.today().year
12 - year_of_birth
13 )
14
15 def test_joe(self):
16 first_name = 'joe'
17 last_name = 'blow'
18 sex = 'M'
19 year_of_birth = 1996
20
21 reality = src.person.person(
22 first_name=first_name,
23 last_name=last_name,
24 sex=sex,
25 year_of_birth=year_of_birth,
26 )
27 my_expectation = (
28 f'{first_name}, {last_name},'
29 f' {sex}, {year_of_birth}'
30 )
31 assert reality == my_expectation
32 self.assertEqual(reality, my_expectation)
33
34 reality = src.person.say_hello(
35 first_name=first_name,
36 last_name=last_name,
37 year_of_birth=year_of_birth,
38 )
39 my_expectation = (
40 f'Hello, my name is {first_name}'
41 f' {last_name} and I am'
42 f' {self.calculate_age(year_of_birth)}.'
43 )
44 assert reality == my_expectation
45 self.assertEqual(reality, my_expectation)
46
47 joe = src.person.Person(
48 first_name=first_name,
49 last_name=last_name,
50 sex=sex,
51 year_of_birth=year_of_birth,
52 )
53
54 reality = joe.say_hello()
55 assert reality == my_expectation
56 self.assertEqual(reality, my_expectation)
57 self.assertEqual(joe.can_vote(), True)
58 self.assertEqual(joe.can_get_license(), False)
59
60 def test_jane(self):
61 first_name = 'jane'
62 last_name = 'doe'
63 sex = 'F'
64 year_of_birth = 1991
65
66 reality = src.person.person(
67 first_name=first_name,
68 last_name=last_name,
69 sex=sex,
70 year_of_birth=year_of_birth,
71 )
72 my_expectation = (
73 f'{first_name}, {last_name},'
74 f' {sex}, {year_of_birth}'
75 )
76 assert reality == my_expectation
77 self.assertEqual(reality, my_expectation)
78
79 reality = src.person.say_hello(
80 first_name=first_name,
81 last_name=last_name,
82 year_of_birth=year_of_birth,
83 )
84 my_expectation = (
85 f'Hello, my name is {first_name}'
86 f' {last_name} and I am'
87 f' {self.calculate_age(year_of_birth)}.'
88 )
89 assert reality == my_expectation
90 self.assertEqual(reality, my_expectation)
91
92 jane = src.person.Person(
93 first_name=first_name,
94 last_name=last_name,
95 sex=sex,
96 year_of_birth=year_of_birth,
97 passed_test=True,
98 )
99
100 reality = jane.say_hello()
101 assert reality == my_expectation
102 self.assertEqual(reality, my_expectation)
103 self.assertEqual(jane.can_vote(), True)
104 self.assertEqual(jane.can_get_license(), True)
105
106 def test_john(self):
107 first_name = 'john'
108 last_name = 'smith'
109 sex = 'M'
110 year_of_birth = 1980
111 # year_of_birth = 1580
112 # raises AssertionError
113 # because older than 120
114
115 reality = src.person.person(
116 first_name=first_name,
117 last_name=last_name,
118 sex=sex,
119 year_of_birth=year_of_birth,
120 )
121 my_expectation = (
122 f'{first_name}, {last_name},'
123 f' {sex}, {year_of_birth}'
124 )
125 assert reality == my_expectation
126 self.assertEqual(reality, my_expectation)
127
128 reality = src.person.say_hello(
129 first_name=first_name,
130 last_name=last_name,
131 year_of_birth=year_of_birth,
132 )
133 my_expectation = (
134 f'Hello, my name is {first_name}'
135 f' {last_name} and I am'
136 f' {self.calculate_age(year_of_birth)}.'
137 )
138 assert reality == my_expectation
139 self.assertEqual(reality, my_expectation)
140
141 john = src.person.Person(
142 first_name=first_name,
143 last_name=last_name,
144 sex=sex,
145 year_of_birth=year_of_birth,
146 is_citizen=False,
147 )
148
149 reality = john.say_hello()
150 assert reality == my_expectation
151 self.assertEqual(reality, my_expectation)
152 self.assertEqual(john.can_vote(), False)
153 self.assertEqual(john.can_get_license(), False)
154
155 def test_mary(self):
156 first_name = 'mary'
157 last_name = 'public'
158 sex = 'F'
159 year_of_birth = 2000
160
161 reality = src.person.person(
162 first_name=first_name,
163 last_name=last_name,
164 sex=sex,
165 year_of_birth=year_of_birth,
166 )
167 my_expectation = (
168 f'{first_name}, {last_name},'
169 f' {sex}, {year_of_birth}'
170 )
171 assert reality == my_expectation
172 self.assertEqual(reality, my_expectation)
173
174 reality = src.person.say_hello(
175 first_name=first_name,
176 last_name=last_name,
177 year_of_birth=year_of_birth,
178 )
179 my_expectation = (
180 f'Hello, my name is {first_name}'
181 f' {last_name} and I am'
182 f' {self.calculate_age(year_of_birth)}.'
183 )
184 assert reality == my_expectation
185 self.assertEqual(reality, my_expectation)
186
187 mary = src.person.Person(
188 first_name=first_name,
189 last_name=last_name,
190 sex=sex,
191 year_of_birth=year_of_birth,
192 is_citizen=False,
193 passed_test=True,
194 )
195
196 reality = mary.say_hello()
197 assert reality == my_expectation
198 self.assertEqual(reality, my_expectation)
199 self.assertEqual(mary.can_vote(), False)
200 self.assertEqual(mary.can_get_license(), True)
201
202 def test_underage_citizen(self):
203 person = src.person.Person(
204 first_name='first_name',
205 last_name='last_name',
206 sex='M',
207 year_of_birth=datetime.date.today().year-17,
208 is_citizen=True,
209 passed_test=True,
210 )
211 self.assertEqual(person.can_vote(), False)
212 self.assertEqual(
213 person.can_get_license(), False
214 )
215
216 def test_when_person_is_too_old_to_be_alive(self):
217 try:
218 src.person.Person(
219 first_name='first_name',
220 last_name='last_name',
221 sex='F',
222 year_of_birth=datetime.date.today().year-121,
223 )
224 except ValueError:
225 pass
226
227 def test_when_year_of_birth_is_not_an_integer(self):
228 try:
229 src.person.Person(
230 first_name='first_name',
231 last_name='last_name',
232 sex='M',
233 year_of_birth=None,
234 )
235 except TypeError:
236 pass
237
238 try:
239 src.person.Person(
240 first_name='first_name',
241 last_name='last_name',
242 sex='M',
243 year_of_birth=2026.0,
244 )
245 except TypeError:
246 pass
247
248 try:
249 src.person.Person(
250 first_name='first_name',
251 last_name='last_name',
252 sex='M',
253 year_of_birth='2026',
254 )
255 except TypeError:
256 pass
257
258 try:
259 src.person.Person(
260 first_name='first_name',
261 last_name='last_name',
262 sex='M',
263 year_of_birth=(2026,),
264 )
265 except TypeError:
266 pass
267
268 def test_dir_person_class(self):
269 reality = dir(src.person.Person)
270 my_expectation = [
271 '__class__',
272 '__delattr__',
273 '__dict__',
274 '__dir__',
275 '__doc__',
276 '__eq__',
277 '__firstlineno__',
278 '__format__',
279 '__ge__',
280 '__getattribute__',
281 '__getstate__',
282 '__gt__',
283 '__hash__',
284 '__init__',
285 '__init_subclass__',
286 '__le__',
287 '__lt__',
288 '__module__',
289 '__ne__',
290 '__new__',
291 '__reduce__',
292 '__reduce_ex__',
293 '__repr__',
294 '__setattr__',
295 '__sizeof__',
296 '__static_attributes__',
297 '__str__',
298 '__subclasshook__',
299 '__weakref__',
300 'can_get_license',
301 'can_vote',
302 'check_age',
303 'say_hello',
304 ]
305 assert reality == my_expectation
306 self.assertEqual(reality, my_expectation)
307
308 def test_dir_person_instance(self):
309 an_instance_of_person = src.person.Person(
310 first_name='first_name',
311 last_name='last_name',
312 sex='M',
313 year_of_birth=2026,
314 )
315
316 reality = dir(an_instance_of_person)
317 my_expectation = [
318 '__class__',
319 '__delattr__',
320 '__dict__',
321 '__dir__',
322 '__doc__',
323 '__eq__',
324 '__firstlineno__',
325 '__format__',
326 '__ge__',
327 '__getattribute__',
328 '__getstate__',
329 '__gt__',
330 '__hash__',
331 '__init__',
332 '__init_subclass__',
333 '__le__',
334 '__lt__',
335 '__module__',
336 '__ne__',
337 '__new__',
338 '__reduce__',
339 '__reduce_ex__',
340 '__repr__',
341 '__setattr__',
342 '__sizeof__',
343 '__static_attributes__',
344 '__str__',
345 '__subclasshook__',
346 '__weakref__',
347 'age',
348 'can_get_license',
349 'can_vote',
350 'check_age',
351 'first_name',
352 'is_citizen',
353 'last_name',
354 'passed_test',
355 'say_hello',
356 'sex',
357 'year_of_birth',
358 ]
359 assert reality == my_expectation
360 self.assertEqual(reality, my_expectation)
361
362
363# Exceptions seen
364# AssertionError
365# NameError
366# TypeError
367# AttributeError
368# SyntaxError
how to make a person with Exceptions: solutions
The code in person/src/person.py from how to make a person with Exceptions
1import datetime
2
3
4class Person:
5
6 def __init__(
7 self, first_name, last_name,
8 sex, year_of_birth=None,
9 is_citizen=True,
10 passed_test=False,
11 ):
12 self.first_name = first_name
13 self.last_name = last_name
14 self.year_of_birth = year_of_birth
15 self.sex = sex
16 self.is_citizen = is_citizen
17 self.passed_test = passed_test
18 self.age = calculate_age(year_of_birth)
19
20 def can_get_license(self):
21 return self.check_age(
22 self.age, self.passed_test
23 )
24
25 def can_vote(self):
26 return self.check_age(
27 self.age, self.is_citizen
28 )
29
30 @staticmethod
31 def check_age(age, response):
32 if age < 18:
33 return False
34 return response
35
36 def say_hello(self):
37 return (
38 f'Hello, my name is {self.first_name}'
39 f' {self.last_name} and I am'
40 f' {self.age}.'
41 )
42
43
44def calculate_age(year_of_birth):
45 if not isinstance(year_of_birth, int):
46 raise TypeError
47
48 age = (
49 datetime.date.today().year
50 - year_of_birth
51 )
52
53 if age > 120:
54 raise ValueError
55 return age
56
57
58def say_hello(
59 first_name, last_name, year_of_birth,
60):
61 return (
62 f'Hello, my name is {first_name}'
63 f' {last_name} and I am'
64 f' {calculate_age(year_of_birth)}.'
65 )
66
67
68def person(
69 first_name, last_name,
70 sex, year_of_birth,
71 ):
72 return (
73 f'{first_name}, {last_name},'
74 f' {sex}, {year_of_birth}'
75 )