## 2020年5月29日金曜日

### Python - Object-Oriented Programming - Country, population, area, density, class, methods(__init__, __str__, __repr__)

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 14(Object-Oriented Programming)、Exercise 1の解答を求めてみる。

コード

```#!/usr/bin/env python3
from unittest import TestCase, main

print('1.')

class TestCountry(TestCase):
def setUp(self):
self.usa = Country('United States of America', 313914040, 9826675)

def test_init(self):

def test_is_larger(self):

def test_population_density(self):
3.4535722262227995)

def test_str(self):
self.assertEqual(
str(self.usa),
'United States of America has a population of 313914040 and is '
'9826675 square km.'
)

def test_repr(self):
self.assertEqual(

class Country:
def __init__(self, name: str, population: int, area: int) -> None:
self.name: str = name
self.population: int = population
self.area: int = area

def is_larger(self, other) -> bool:
return isinstance(other, Country) and self.area > other.area

def population_density(self) -> float:
return self.population / self.area

def __str__(self) -> str:
return f'{self.name} has a population of {self.population} and is '\
f'{self.area} square km.'

def __repr__(self) -> str:
return f"Country('{self.name}', {self.population}, {self.area})"

if __name__ == "__main__":
main()
```

```% ./sample1.py
1.
E
======================================================================
ERROR: test_init (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 9, in setUp
NameError: name 'Country' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
% mypy sample1.py
Success: no issues found in 1 source file
% ./sample1.py
1.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
% ./sample1.py
1.
.E
======================================================================
ERROR: test_is_larger (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 18, in test_is_larger
AttributeError: 'Country' object has no attribute 'is_larger'

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (errors=1)
% mypy sample1.py
Success: no issues found in 1 source file
% ./sample1.py
1.
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
% ./sample1.py
1.
..E
======================================================================
ERROR: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
AttributeError: 'Country' object has no attribute 'population_density'

----------------------------------------------------------------------
Ran 3 tests in 0.000s

FAILED (errors=1)
% mypy sample1.py
Success: no issues found in 1 source file
% ./sample1.py
1.
..E
======================================================================
ERROR: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
AttributeError: 'Country' object has no attribute 'population_density'

----------------------------------------------------------------------
Ran 3 tests in 0.000s

FAILED (errors=1)
% ./sample1.py
1.
..F
======================================================================
FAIL: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
AssertionError: 3.453569459125272 != 3.4535722262227995

----------------------------------------------------------------------
Ran 3 tests in 0.000s

FAILED (failures=1)
% ./sample1.py
1.
..F
======================================================================
FAIL: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
AssertionError: 0.2895554908727049 != 3.4535722262227995

----------------------------------------------------------------------
Ran 3 tests in 0.000s

FAILED (failures=1)
% ./sample1.py
1.
..F
======================================================================
FAIL: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
AssertionError: 3.453569459125272 != 3.4535722262227995

----------------------------------------------------------------------
Ran 3 tests in 0.000s

FAILED (failures=1)
% ./sample1.py
1.
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
% ./sample1.py
1.
...F
======================================================================
FAIL: test_str (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 25, in test_str
self.assertEqual(
AssertionError: '<__main__.Country object at 0x10b38a280>' != 'United States of America has a population[35 chars] km.'
- <__main__.Country object at 0x10b38a280>
+ United States of America has a population of 313914040 and is 9826675 square km.

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (failures=1)
% ./sample1.py
File "./sample1.py", line 46
return f'{self.name} has a population of {self.population} and is ' \
{self.area} square km.'
^
SyntaxError: invalid syntax
% ./sample1.py
File "./sample1.py", line 46
return f'{self.name} has a population of {self.population} and is ' +\
{self.area} square km.'
^
SyntaxError: invalid syntax
% ./sample1.py
1.
...F
======================================================================
FAIL: test_str (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 25, in test_str
self.assertEqual(
AssertionError: 'Unit[18 chars]ca has a population of 313914040 and is {self.area} square km.' != 'Unit[18 chars]ca has a population of 313914040 and is 9826675 square km.'
- United States of America has a population of 313914040 and is {self.area} square km.
?                                                               ^^^^^^^^^^^
+ United States of America has a population of 313914040 and is 9826675 square km.
?                                                               ^^^^^^^

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (failures=1)
% ./sample1.py
1.
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
% ./sample1.py
File "./sample1.py", line 33
^
SyntaxError: closing parenthesis ')' does not match opening parenthesis '['
% ./sample1.py
1.
...FF
======================================================================
FAIL: test_repr (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 32, in test_repr
AssertionError: <__main__.Country object at 0x10db61340> != "Country('Canada', 34482779, 9984670)"

======================================================================
FAIL: test_str (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 25, in test_str
self.assertEqual(
AssertionError: 'Unit[14 chars]merica has a population of 313914040 and is ' != 'Unit[14 chars]merica has a population of 313914040 and is 9826675 square km.'
- United States of America has a population of 313914040 and is
+ United States of America has a population of 313914040 and is 9826675 square km.
?                                                               ++++++++++++++++++

----------------------------------------------------------------------
Ran 5 tests in 0.001s

FAILED (failures=2)
% ./sample1.py
1.
...F.
======================================================================
FAIL: test_repr (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 32, in test_repr
AssertionError: <__main__.Country object at 0x106a94340> != "Country('Canada', 34482779, 9984670)"

----------------------------------------------------------------------
Ran 5 tests in 0.001s

FAILED (failures=1)
% ./sample1.py
1.
...F.
======================================================================
FAIL: test_repr (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 32, in test_repr
AssertionError: Country('Canada', 34482779, 9984670) != "Country('Canada', 34482779, 9984670)"

----------------------------------------------------------------------
Ran 5 tests in 0.001s

FAILED (failures=1)
% ./sample1.py
1.
...F.
======================================================================
FAIL: test_repr (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 32, in test_repr
AssertionError: Country('Canada', 34482779, 9984670) != Country('Canada', 34482779, 9984670)

----------------------------------------------------------------------
Ran 5 tests in 0.001s

FAILED (failures=1)
% ./sample1.py
1.
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s

OK
% mypy sample1.py
Success: no issues found in 1 source file
% ./sample1.py -v
1.
test_init (__main__.TestCountry) ... ok
test_is_larger (__main__.TestCountry) ... ok
test_population_density (__main__.TestCountry) ... ok
test_repr (__main__.TestCountry) ... ok
test_str (__main__.TestCountry) ... ok

----------------------------------------------------------------------
Ran 5 tests in 0.001s

OK
%
```