Permalink
Browse files

Step 8 - Reducing code duplication

  • Loading branch information...
lgiordani committed Jul 17, 2017
1 parent 17b2413 commit b559a5c91ef58e1e734ac97b676468d09a460a45
Showing with 30 additions and 14 deletions.
  1. +16 −14 datastats/datastats.py
  2. +14 −0 tests/test_datastats.py
View
@@ -4,36 +4,38 @@
class DataStats:
def _salaries(self, data):
return [int(d['salary'][1:]) for d in data]
def _ages(self, data):
return [d['age'] for d in data]
def _avg_salary(self, data):
return math.floor(sum([int(e['salary'][1:]) for e in data])/len(data))
return math.floor(sum(self._salaries(data))/len(data))
def _avg_age(self, data):
return math.floor(sum([e['age'] for e in data])/len(data))
return math.floor(sum(self._ages(data))/len(data))
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.
# Compute average yearly increase
average_age_increase = math.floor(
sum([e['age'] for e in data])/len(data)) - iage
average_salary_increase = math.floor(
sum([int(e['salary'][1:]) for e in data])/len(data)) - isalary
average_age_increase = self._avg_age(data) - iage
average_salary_increase = self._avg_salary(data) - isalary
return math.floor(average_salary_increase/average_age_increase)
def _max_salary(self, data):
# Compute max salary
salaries = [int(e['salary'][1:]) for e in data]
threshold = '£' + str(max(salaries))
def _select_salary(self, data, func):
threshold = '£{}'.format(str(func(self._salaries(data))))
return [e for e in data if e['salary'] == threshold]
def _max_salary(self, data):
return self._select_salary(data, max)
def _min_salary(self, data):
# Compute min salary
salaries = [int(d['salary'][1:]) for d in data]
return [e for e in data if e['salary'] ==
'£{}'.format(str(min(salaries)))]
return self._select_salary(data, min)
def _stats(self, data, iage, isalary):
return {
View
@@ -125,3 +125,17 @@ def test__min_salary():
"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]

0 comments on commit b559a5c

Please sign in to comment.