Permalink
Browse files

Step 9 - Advanced refactoring

  • Loading branch information...
lgiordani committed Jul 20, 2017
1 parent b559a5c commit cc0b0a105ebc882cb73831b177e881bb65f4b491
Showing with 191 additions and 118 deletions.
  1. +43 −32 datastats/datastats.py
  2. +0 −86 tests/test_datastats.py
  3. +148 −0 tests/test_newdatastats.py
View
@@ -2,52 +2,63 @@
import json
class DataStats:
class NewDataStats:
def _salaries(self, data):
return [int(d['salary'][1:]) for d in data]
def __init__(self, data):
self.data = data
def _ages(self, data):
return [d['age'] for d in data]
@property
def _salaries(self):
return [int(d['salary'][1:]) for d in self.data]
def _avg_salary(self, data):
return math.floor(sum(self._salaries(data))/len(data))
@property
def _ages(self):
return [d['age'] for d in self.data]
def _avg_age(self, data):
return math.floor(sum(self._ages(data))/len(data))
def _select_salary(self, func):
threshold = '£{}'.format(str(func(self._salaries)))
def _avg_yearly_increase(self, data, iage, isalary):
# iage and isalary are the starting age and salary used to
# compute the average yearly increase of salary.
return [e for e in self.data if e['salary'] == threshold]
# Compute average yearly increase
average_age_increase = self._avg_age(data) - iage
average_salary_increase = self._avg_salary(data) - isalary
def _min_salary(self):
return self._select_salary(min)
return math.floor(average_salary_increase/average_age_increase)
def _max_salary(self):
return self._select_salary(max)
def _avg_salary(self):
return math.floor(sum(self._salaries)/len(self.data))
def _select_salary(self, data, func):
threshold = '£{}'.format(str(func(self._salaries(data))))
def _avg_age(self):
return math.floor(sum(self._ages)/len(self.data))
return [e for e in data if e['salary'] == threshold]
def _avg_yearly_increase(self, iage, isalary):
# iage and isalary are the starting age and salary used to
# compute the average yearly increase of salary.
def _max_salary(self, data):
return self._select_salary(data, max)
# Compute average yearly increase
average_age_increase = self._avg_age() - iage
average_salary_increase = self._avg_salary() - isalary
def _min_salary(self, data):
return self._select_salary(data, min)
return math.floor(average_salary_increase/average_age_increase)
def _stats(self, data, iage, isalary):
def _stats(self, iage, isalary):
return {
'avg_age': self._avg_age(data),
'avg_salary': self._avg_salary(data),
'avg_yearly_increase': self._avg_yearly_increase(
data, iage, isalary),
'max_salary': self._max_salary(data),
'min_salary': self._min_salary(data)
'avg_age': self._avg_age(),
'avg_salary': self._avg_salary(),
'avg_yearly_increase': self._avg_yearly_increase(iage, isalary),
'max_salary': self._max_salary(),
'min_salary': self._min_salary()
}
def stats(self, data, iage, isalary):
def stats(self, iage, isalary):
return json.dumps(
self._stats(data, iage, isalary)
self._stats(iage, isalary)
)
class DataStats:
def stats(self, data, iage, isalary):
nds = NewDataStats(data)
return nds.stats(iage, isalary)
View
@@ -53,89 +53,3 @@ def test_json():
}]
}
)
def test__stats():
ds = DataStats()
assert ds._stats(test_data, 20, 20000) == {
'avg_age': 62,
'avg_salary': 55165,
'avg_yearly_increase': 837,
'max_salary': [{
"id": 3,
"name": "Garth",
"surname": "Fields",
"age": 70,
"salary": "£70472"
}],
'min_salary': [{
"id": 1,
"name": "Laith",
"surname": "Simmons",
"age": 68,
"salary": "£27888"
}]
}
def test__avg_age():
ds = DataStats()
assert ds._avg_age(test_data) == 62
def test__avg_salary():
ds = DataStats()
assert ds._avg_salary(test_data) == 55165
def test__avg_yearly_increase():
ds = DataStats()
assert ds._avg_yearly_increase(test_data, 20, 20000) == 837
def test__max_salary():
ds = DataStats()
assert ds._max_salary(test_data) == [{
"id": 3,
"name": "Garth",
"surname": "Fields",
"age": 70,
"salary": "£70472"
}]
def test__min_salary():
ds = DataStats()
assert ds._min_salary(test_data) == [{
"id": 1,
"name": "Laith",
"surname": "Simmons",
"age": 68,
"salary": "£27888"
}]
def test_salaries():
ds = DataStats()
assert ds._salaries(test_data) == [27888, 67137, 70472]
def test_ages():
ds = DataStats()
assert ds._ages(test_data) == [68, 49, 70]
View
@@ -0,0 +1,148 @@
import json
from datastats.datastats import NewDataStats
test_data = [
{
"id": 1,
"name": "Laith",
"surname": "Simmons",
"age": 68,
"salary": "£27888"
},
{
"id": 2,
"name": "Mikayla",
"surname": "Henry",
"age": 49,
"salary": "£67137"
},
{
"id": 3,
"name": "Garth",
"surname": "Fields",
"age": 70,
"salary": "£70472"
}
]
def test_init():
ds = NewDataStats(test_data)
assert ds.data == test_data
def test_ages():
ds = NewDataStats(test_data)
assert ds._ages == [68, 49, 70]
def test_salaries():
ds = NewDataStats(test_data)
assert ds._salaries == [27888, 67137, 70472]
def test__min_salary():
ds = NewDataStats(test_data)
assert ds._min_salary() == [{
"id": 1,
"name": "Laith",
"surname": "Simmons",
"age": 68,
"salary": "£27888"
}]
def test__max_salary():
ds = NewDataStats(test_data)
assert ds._max_salary() == [{
"id": 3,
"name": "Garth",
"surname": "Fields",
"age": 70,
"salary": "£70472"
}]
def test__avg_age():
ds = NewDataStats(test_data)
assert ds._avg_age() == 62
def test__avg_salary():
ds = NewDataStats(test_data)
assert ds._avg_salary() == 55165
def test__avg_yearly_increase():
ds = NewDataStats(test_data)
assert ds._avg_yearly_increase(20, 20000) == 837
def test__stats():
ds = NewDataStats(test_data)
assert ds._stats(20, 20000) == {
'avg_age': 62,
'avg_salary': 55165,
'avg_yearly_increase': 837,
'max_salary': [{
"id": 3,
"name": "Garth",
"surname": "Fields",
"age": 70,
"salary": "£70472"
}],
'min_salary': [{
"id": 1,
"name": "Laith",
"surname": "Simmons",
"age": 68,
"salary": "£27888"
}]
}
def test_json():
ds = NewDataStats(test_data)
assert ds.stats(20, 20000) == json.dumps(
{
'avg_age': 62,
'avg_salary': 55165,
'avg_yearly_increase': 837,
'max_salary': [{
"id": 3,
"name": "Garth",
"surname": "Fields",
"age": 70,
"salary": "£70472"
}],
'min_salary': [{
"id": 1,
"name": "Laith",
"surname": "Simmons",
"age": 68,
"salary": "£27888"
}]
}
)

0 comments on commit cc0b0a1

Please sign in to comment.