Skip to content

Commit 84c6d0a

Browse files
committed
Changes to age_bands and dateValidator
1 parent 02f6cbc commit 84c6d0a

4 files changed

Lines changed: 64 additions & 31 deletions

File tree

codonPython/age_bands.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import math
22

3-
43
def age_band_5_years(age: int)->str:
54
"""
65
Place age into appropriate 5 year band
@@ -22,24 +21,26 @@ def age_band_5_years(age: int)->str:
2221
--------
2322
>>> age_band_5_years(3)
2423
'0-4'
25-
>>> age_band_5_years(-1)
24+
>>> age_band_5_years(None)
2625
'Age not known'
2726
>>> age_band_5_years(95)
2827
'90 and over'
2928
"""
3029

31-
if age is None or age < 0:
30+
if age is None:
3231
return 'Age not known'
3332

34-
if age > 89:
35-
if age < 150:
36-
return '90 and over'
33+
if age >= 90:
34+
if age >= 150:
35+
raise ValueError("The age input: {} is too large.".format(age))
3736
else:
38-
raise ValueError
39-
40-
lowerbound = 5 * int(math.floor(age / 5))
41-
upperbound = lowerbound + 4
42-
return '{}-{}'.format(lowerbound, upperbound)
37+
return '90 and over'
38+
elif age < 0:
39+
raise ValueError("The age input: {} is too low.".format(age))
40+
else:
41+
lowerbound = 5 * int(math.floor(age / 5))
42+
upperbound = lowerbound + 4
43+
return '{}-{}'.format(lowerbound, upperbound)
4344

4445

4546
def age_band_10_years(age: int)->str:
@@ -63,21 +64,23 @@ def age_band_10_years(age: int)->str:
6364
--------
6465
>>> age_band_10_years(3)
6566
'0-9'
66-
>>> age_band_10_years(-1)
67+
>>> age_band_10_years(None)
6768
'Age not known'
6869
>>> age_band_10_years(95)
6970
'90 and over'
7071
"""
7172

72-
if age is None or age < 0:
73+
if age is None:
7374
return 'Age not known'
7475

75-
if age > 89:
76-
if age < 150:
77-
return '90 and over'
76+
if age >= 90:
77+
if age >= 150:
78+
raise ValueError("The age input: {} is too large.".format(age))
7879
else:
79-
raise ValueError
80-
81-
lowerbound = 10 * int(math.floor(age / 10))
82-
upperbound = lowerbound + 9
83-
return '{}-{}'.format(lowerbound, upperbound)
80+
return '90 and over'
81+
elif age < 0:
82+
raise ValueError("The age input: {} is too low.".format(age))
83+
else:
84+
lowerbound = 10 * int(math.floor(age / 10))
85+
upperbound = lowerbound + 9
86+
return '{}-{}'.format(lowerbound, upperbound)

codonPython/dateValidator.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import re
22

3-
43
def validDate(date_string: str)->bool:
54
"""
65
Validates stringtype dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy` from
76
years 1900-9999. Leap year support included.
87
98
Parameters
109
----------
11-
date_string : str
12-
Date to be validated
10+
date_string : str
11+
Date to be validated
1312
1413
Returns
1514
----------
16-
boolean
17-
Whether the date is valid or not
15+
boolean
16+
Whether the date is valid or not
1817
1918
Examples
2019
---------
@@ -25,6 +24,8 @@ def validDate(date_string: str)->bool:
2524
>>> validDate("43/01/1996")
2625
False
2726
"""
27+
28+
# Let TypeError be.
2829

2930
# This regex string will validate dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy`
3031
# from years 1900 - 2049. Leap year support included. Original Regex string based on
@@ -40,4 +41,4 @@ def validDate(date_string: str)->bool:
4041
date_string, flags=0):
4142
return True
4243
else:
43-
return False
44+
return False

codonPython/tests/age_bands_test.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ def test_age_band_5_years_typeErrors():
2525

2626
@pytest.mark.parametrize("age", [
2727
np.nan,
28-
math.inf
28+
math.inf,
29+
-3,
30+
343,
31+
-0.1
2932
])
3033
def test_age_band_5_years_valueErrors(age):
3134
with pytest.raises(ValueError):
3235
age_bands.age_band_5_years(age)
3336

3437
@pytest.mark.parametrize("age, expected", [
35-
(-1, 'Age not known'),
3638
(None, 'Age not known'),
3739
])
3840
def test_age_band_5_years_edgeCases(age, expected):
@@ -76,14 +78,16 @@ def test_age_band_10_years_typeErrors():
7678

7779
@pytest.mark.parametrize("age", [
7880
np.nan,
79-
math.inf
81+
math.inf,
82+
-3,
83+
343,
84+
-0.1
8085
])
8186
def test_age_band_10_years_valueErrors(age):
8287
with pytest.raises(ValueError):
8388
age_bands.age_band_10_years(age)
8489

8590
@pytest.mark.parametrize("age, expected", [
86-
(-1, 'Age not known'),
8791
(None, 'Age not known'),
8892
])
8993
def test_age_band_10_years_edgeCases(age, expected):
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from codonPython import dateValidator
2+
import numpy as np
3+
import math
4+
import pytest
5+
6+
@pytest.mark.parametrize("date_string, expected", [
7+
('01/01/1900', True), #Edge date
8+
('29/02/1992', True), #Leap Year
9+
('31/05/2020', True), #31-day month
10+
('29/02/2040', True), #Leap Year
11+
('31/12/2049', True), #Edge date
12+
])
13+
def test_validDate_positives(date_string, expected):
14+
assert expected == dateValidator.validDate(date_string)
15+
16+
@pytest.mark.parametrize("date_string, expected", [
17+
('31/12/1899', False), #Edge date
18+
('29/02/1990', False), #Leap Year
19+
('31/04/2020', False), #31-day month
20+
('29/02/2041', False), #Leap Year
21+
('01/01/2050', False), #Edge date
22+
])
23+
def test_validDate_negatives(date_string, expected):
24+
assert expected == dateValidator.validDate(date_string)
25+

0 commit comments

Comments
 (0)