Permalink
Browse files

Step 2 - Getting rid of the JSON format

  • Loading branch information...
lgiordani committed Jul 15, 2017
1 parent 27a1d8c commit 65e2997d71ade752633229186c6669803a46f185
Showing with 56 additions and 3 deletions.
  1. +8 −3 datastats/datastats.py
  2. +48 −0 tests/test_datastats.py
View
@@ -4,7 +4,7 @@
class DataStats:
def stats(self, data, iage, isalary):
def _stats(self, data, iage, isalary):
# iage and isalary are the starting age and salary used to
# compute the average yearly increase of salary.
@@ -28,11 +28,16 @@ def stats(self, data, iage, isalary):
min_salary = [e for e in data if e['salary'] ==
'£{}'.format(str(min(salaries)))]
return json.dumps({
return {
'avg_age': math.floor(sum([e['age'] for e in data])/len(data)),
'avg_salary': math.floor(sum(
[int(e['salary'][1:]) for e in data])/len(data)),
'avg_yearly_increase': yearly_avg_increase,
'max_salary': max_salary,
'min_salary': min_salary
})
}
def stats(self, data, iage, isalary):
return json.dumps(
self._stats(data, iage, isalary)
)
View
@@ -51,3 +51,51 @@ def test_json():
}]
}
)
def test__stats():
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"
}
]
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"
}]
}

0 comments on commit 65e2997

Please sign in to comment.