Skip to content

Commit 99a38c1

Browse files
Merge pull request #16 from codonlibrary/format-pep8
Run files through auto-pep8.
2 parents 8dea66a + 87d29bd commit 99a38c1

10 files changed

Lines changed: 121 additions & 99 deletions

File tree

CONTRIBUTING.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ First off, thank you for taking the time to contribute!
66

77
Source + Doc + Test = :heart_eyes:
88

9-
Please send a [GitHub Pull Request to codonPython](https://github.com/codonlibrary/codonPython/pull/new/master) with a
10-
clear description of what you have done.
11-
We suggest you follow our coding conventions (below) and make sure all of your commits are atomic (one feature per commit).
9+
Please send a [GitHub Pull Request to codonPython](https://github.com/codonlibrary/codonPython/pull/new/master) with a
10+
clear description of what you have done.
11+
We suggest you follow our coding conventions (below) and make sure all of your commits are atomic (one feature per commit).
1212
Please do not send us undocumented code as we might not accept it. Including tests to your pull request brings tears of joy to our eyes.
13-
14-
The easier it is for us to review a pull request, the faster we will merge it.
13+
14+
The easier it is for us to review a pull request, the faster we will merge it.
1515

1616
## Coding conventions
1717

1818
Start reading our code and you will get an idea of it:
1919

20-
* We use [PEP8](https://www.python.org/dev/peps/pep-0008/).
21-
* We use docstrings and we try to (loosely) follow [`numpy`'s docstring standards](https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard).
22-
* This is open source software. Consider the people who will read your code, and make it look nice for them.
20+
* We use [PEP8](https://www.python.org/dev/peps/pep-0008/).
21+
* We use docstrings and we try to (loosely) follow [`numpy`'s docstring standards](https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard).
22+
* This is open source software. Consider the people who will read your code, and make it look nice for them.
2323

2424
Thank you,
2525
codonPeople

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
[![Build Status](https://travis-ci.com/codonlibrary/codonPython.svg?branch=master)](https://travis-ci.com/codonlibrary/codonPython)
44
[![codecov](https://codecov.io/gh/codonlibrary/codonPython/branch/master/graph/badge.svg)](https://codecov.io/gh/codonlibrary/codonPython)
55

6-
76
This is a simple example of a future package pulled from the DPS Core code base.
87
The package can be directly installed by typing in your terminal: `python -m pip install --user git+https://github.com/codonlibrary/codonPython.git`

codonPython/age_bands.py

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

3-
def age_band_5_years(age: int)->str:
3+
4+
def age_band_5_years(age: int) -> str:
45
"""
56
Place age into appropriate 5 year band
67
@@ -31,7 +32,7 @@ def age_band_5_years(age: int)->str:
3132
return 'Age not known'
3233

3334
if age >= 90:
34-
if age >= 150:
35+
if age >= 150:
3536
raise ValueError("The age input: {} is too large.".format(age))
3637
else:
3738
return '90 and over'
@@ -43,7 +44,7 @@ def age_band_5_years(age: int)->str:
4344
return '{}-{}'.format(lowerbound, upperbound)
4445

4546

46-
def age_band_10_years(age: int)->str:
47+
def age_band_10_years(age: int) -> str:
4748
"""
4849
Place age into appropriate 10 year band
4950
@@ -74,7 +75,7 @@ def age_band_10_years(age: int)->str:
7475
return 'Age not known'
7576

7677
if age >= 90:
77-
if age >= 150:
78+
if age >= 150:
7879
raise ValueError("The age input: {} is too large.".format(age))
7980
else:
8081
return '90 and over'

codonPython/dateValidator.py

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

3-
def validDate(date_string: str)->bool:
3+
4+
def validDate(date_string: str) -> bool:
45
"""
56
Validates stringtype dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy` from
67
years 1900-9999. Leap year support included.
7-
8+
89
Parameters
910
----------
1011
date_string : str
@@ -24,11 +25,11 @@ def validDate(date_string: str)->bool:
2425
>>> validDate("43/01/1996")
2526
False
2627
"""
27-
28+
2829
# Let TypeError be.
2930

30-
# This regex string will validate dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy`
31-
# from years 1900 - 2049. Leap year support included. Original Regex string based on
31+
# This regex string will validate dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy`
32+
# from years 1900 - 2049. Leap year support included. Original Regex string based on
3233
# https://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy
3334
# modified to confine the year dates.
3435
if re.match(
@@ -38,7 +39,7 @@ def validDate(date_string: str)->bool:
3839
r"(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]" +
3940
r"|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4" +
4041
r"(?:(?:1[9]..|2[0][0-4].))$",
41-
date_string, flags=0):
42+
date_string, flags=0):
4243
return True
4344
else:
44-
return False
45+
return False

codonPython/nhsNumber.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33

44

5-
def nhsNumberValidator(number: int)->bool:
5+
def nhsNumberValidator(number: int) -> bool:
66
"""
77
Validate NHS Number according to modulus 11 checks as recorded in the data dictionary.
88
https://www.datadictionary.nhs.uk/data_dictionary/attributes/n/nhs/nhs_number_de.asp?shownav=1
@@ -26,26 +26,30 @@ def nhsNumberValidator(number: int)->bool:
2626
"""
2727

2828
if not isinstance(number, int):
29-
raise ValueError("Please input a positive 10 digit integer to validate.")
29+
raise ValueError(
30+
"Please input a positive 10 digit integer to validate.")
3031
if number < 0:
31-
raise ValueError("Please input a postitive 10 digit integer to validate.")
32+
raise ValueError(
33+
"Please input a postitive 10 digit integer to validate.")
3234
digits = [int(digit) for digit in str(number)]
3335
# NHS Numbers are 10 digits long.
3436
if not len(digits) == 10:
35-
raise ValueError("Please input a postitive 10 digit integer to validate.")
37+
raise ValueError(
38+
"Please input a postitive 10 digit integer to validate.")
3639
# Apply weighting to first 9 digits
37-
weighted_digits = np.dot(np.array(digits[:9]), np.arange(10,1,-1))
38-
# Validity is based on the check digit, which has to be equal to `remainder`
40+
weighted_digits = np.dot(np.array(digits[:9]), np.arange(10, 1, -1))
41+
# Validity is based on the check digit, which has to be equal to `remainder`
3942
remainder = weighted_digits % 11
4043
check_digit = 11 - remainder
4144
if check_digit == 11:
4245
check_digit = 0
4346
if check_digit == digits[-1]:
4447
return True
45-
else:
48+
else:
4649
return False
4750

48-
def nhsNumberGenerator(to_generate: int, random_state: int = None)->list:
51+
52+
def nhsNumberGenerator(to_generate: int, random_state: int = None) -> list:
4953
"""
5054
Generates up to 1M random NHS number(s) compliant with modulus 11 checks recorded
5155
in the data dictonary.
@@ -72,11 +76,13 @@ def nhsNumberGenerator(to_generate: int, random_state: int = None)->list:
7276
if random_state:
7377
random.seed(random_state)
7478
if not isinstance(to_generate, int):
75-
raise ValueError("Please input a positive integer to generate numbers.")
79+
raise ValueError(
80+
"Please input a positive integer to generate numbers.")
7681
if to_generate > 1000000:
7782
raise ValueError("More than one million values requested")
7883
if to_generate < 0:
79-
raise ValueError("Please input a postitive integer to generate numbers.")
84+
raise ValueError(
85+
"Please input a postitive integer to generate numbers.")
8086

8187
generated = []
8288
while len(generated) < to_generate:

codonPython/tableFromSql.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def tableFromSql(server: str, database: str, table_name: str, user: str = "", password: str = "", schema: str = None, index_col: str = None, coerce_float: bool = True, parse_dates: list = None, columns: list = None, chunksize: int = None):
77
'''
88
Returns a SQL table in a DataFrame.
9-
9+
1010
Convert a table stored in SQL Server 2016 into a pandas dataframe.
1111
Uses sqlalchemy and pandas.
1212
@@ -43,7 +43,7 @@ def tableFromSql(server: str, database: str, table_name: str, user: str = "", pa
4343
chunksize : int, default : None
4444
If specified, returns an iterator where chunksize is the number of rows to include
4545
in each chunk.
46-
46+
4747
Returns
4848
----------
4949
pd.DataFrame
@@ -58,9 +58,9 @@ def tableFromSql(server: str, database: str, table_name: str, user: str = "", pa
5858
'''
5959

6060
try:
61-
uri = "mssql+pyodbc://{}:{}@{}/{}?driver=SQL Server Native Client 11.0".format(user, password, server, database)
61+
uri = "mssql+pyodbc://{}:{}@{}/{}?driver=SQL Server Native Client 11.0".format(
62+
user, password, server, database)
6263
engine = create_engine(uri)
6364
return pd.read_sql_table(table_name, engine, schema=schema, index_col=index_col, coerce_float=coerce_float, parse_dates=parse_dates, columns=columns, chunksize=chunksize)
6465
except Exception as error:
6566
raise error
66-
Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from codonPython import age_bands
22
import numpy as np
33
import math
4-
import pytest
5-
4+
import pytest
5+
6+
67
@pytest.mark.parametrize("age, expected", [
78
(0, '0-4'),
89
(1, '0-4'),
@@ -17,29 +18,33 @@
1718
(90, '90 and over'),
1819
])
1920
def test_age_band_5_years_BAU(age, expected):
20-
assert expected == age_bands.age_band_5_years(age)
21+
assert expected == age_bands.age_band_5_years(age)
22+
2123

2224
def test_age_band_5_years_typeErrors():
23-
with pytest.raises(TypeError):
24-
age_bands.age_band_5_years("age")
25+
with pytest.raises(TypeError):
26+
age_bands.age_band_5_years("age")
27+
2528

2629
@pytest.mark.parametrize("age", [
27-
np.nan,
28-
math.inf,
29-
-3,
30-
343,
31-
-0.1
30+
np.nan,
31+
math.inf,
32+
-3,
33+
343,
34+
-0.1
3235
])
3336
def test_age_band_5_years_valueErrors(age):
34-
with pytest.raises(ValueError):
35-
age_bands.age_band_5_years(age)
37+
with pytest.raises(ValueError):
38+
age_bands.age_band_5_years(age)
39+
3640

3741
@pytest.mark.parametrize("age, expected", [
38-
(None, 'Age not known'),
42+
(None, 'Age not known'),
3943
])
4044
def test_age_band_5_years_edgeCases(age, expected):
41-
assert expected == age_bands.age_band_5_years(age)
42-
45+
assert expected == age_bands.age_band_5_years(age)
46+
47+
4348
@pytest.mark.parametrize("age, expected", [
4449
(0.1, '0-4'),
4550
(1.2, '0-4'),
@@ -54,7 +59,8 @@ def test_age_band_5_years_edgeCases(age, expected):
5459
(90.1, '90 and over'),
5560
])
5661
def test_age_band_5_years_BAU_floats(age, expected):
57-
assert expected == age_bands.age_band_5_years(age)
62+
assert expected == age_bands.age_band_5_years(age)
63+
5864

5965
@pytest.mark.parametrize("age, expected", [
6066
(0, '0-9'),
@@ -70,29 +76,33 @@ def test_age_band_5_years_BAU_floats(age, expected):
7076
(90, '90 and over'),
7177
])
7278
def test_age_band_10_years_BAU(age, expected):
73-
assert expected == age_bands.age_band_10_years(age)
79+
assert expected == age_bands.age_band_10_years(age)
80+
7481

7582
def test_age_band_10_years_typeErrors():
76-
with pytest.raises(TypeError):
77-
age_bands.age_band_10_years("age")
83+
with pytest.raises(TypeError):
84+
age_bands.age_band_10_years("age")
85+
7886

7987
@pytest.mark.parametrize("age", [
80-
np.nan,
81-
math.inf,
82-
-3,
83-
343,
84-
-0.1
88+
np.nan,
89+
math.inf,
90+
-3,
91+
343,
92+
-0.1
8593
])
8694
def test_age_band_10_years_valueErrors(age):
87-
with pytest.raises(ValueError):
88-
age_bands.age_band_10_years(age)
95+
with pytest.raises(ValueError):
96+
age_bands.age_band_10_years(age)
97+
8998

9099
@pytest.mark.parametrize("age, expected", [
91-
(None, 'Age not known'),
100+
(None, 'Age not known'),
92101
])
93102
def test_age_band_10_years_edgeCases(age, expected):
94-
assert expected == age_bands.age_band_10_years(age)
95-
103+
assert expected == age_bands.age_band_10_years(age)
104+
105+
96106
@pytest.mark.parametrize("age, expected", [
97107
(0.1, '0-9'),
98108
(1.2, '0-9'),
@@ -107,4 +117,4 @@ def test_age_band_10_years_edgeCases(age, expected):
107117
(90.1, '90 and over'),
108118
])
109119
def test_age_band_10_years_BAU_floats(age, expected):
110-
assert expected == age_bands.age_band_10_years(age)
120+
assert expected == age_bands.age_band_10_years(age)
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
from codonPython import dateValidator
22
import numpy as np
33
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
4+
import pytest
5+
6+
7+
@pytest.mark.parametrize("date_string, expected", [
8+
('01/01/1900', True), # Edge date
9+
('29/02/1992', True), # Leap Year
10+
('31/05/2020', True), # 31-day month
11+
('29/02/2040', True), # Leap Year
12+
('31/12/2049', True), # Edge date
1213
])
1314
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
15+
assert expected == dateValidator.validDate(date_string)
16+
17+
18+
@pytest.mark.parametrize("date_string, expected", [
19+
('31/12/1899', False), # Edge date
20+
('29/02/1990', False), # Leap Year
21+
('31/04/2020', False), # 31-day month
22+
('29/02/2041', False), # Leap Year
23+
('01/01/2050', False), # Edge date
2224
])
2325
def test_validDate_negatives(date_string, expected):
24-
assert expected == dateValidator.validDate(date_string)
25-
26+
assert expected == dateValidator.validDate(date_string)

0 commit comments

Comments
 (0)