Skip to content

Commit f927761

Browse files
murd0georgeRobertson
authored andcommitted
Con 140 flake8 (#110)
* Add linting to testing pipeline. * black all .py files and ensuring they pass flake8 * Add some newlines, try to trigger build in travis. * Try to rollback the change that broke the test * Change is to == for a few lines * Remove tests for deprecated error handling * Remove == True
1 parent 9d7395e commit f927761

25 files changed

Lines changed: 1152 additions & 1043 deletions

.flake8

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[flake8]
2+
# Rule definitions: http://flake8.pycqa.org/en/latest/user/error-codes.html
3+
# D203: 1 blank line required before class docstring
4+
# W503: line break before binary operator
5+
# W504: line break after binary operator
6+
# F401: file imported but not used
7+
# F841: local variable is assigned to but never used
8+
exclude = __pycache__,node_modules,.git,.pytest_cache,docs
9+
ignore = D203,W503,W504,F401
10+
max-complexity = 24
11+
max-line-length = 120
12+
per-file-ignores =
13+
codonPython/tests/file_utils_test.py:F841

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ install:
1010

1111
script:
1212
- pytest --cov=./
13+
- flake8 codonPython
1314

1415
after_success:
1516
- codecov
@@ -20,4 +21,4 @@ deploy:
2021
skip_cleanup: true
2122
github_token: $githubtoken
2223
local_dir: docs/build/html
23-
keep_history: true
24+
keep_history: true

codonPython/age_bands.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def age_band_5_years(age: int) -> str:
55
"""
66
Place age into appropriate 5 year band
77
8-
This function takes the age supplied as an argument and returns a string
8+
This function takes the age supplied as an argument and returns a string
99
representing the relevant 5 year banding.
1010
1111
Parameters
@@ -29,26 +29,26 @@ def age_band_5_years(age: int) -> str:
2929
"""
3030

3131
if age is None:
32-
return 'Age not known'
32+
return "Age not known"
3333

3434
if age >= 90:
3535
if age >= 150:
3636
raise ValueError("The age input: {} is too large.".format(age))
3737
else:
38-
return '90 and over'
38+
return "90 and over"
3939
elif age < 0:
4040
raise ValueError("The age input: {} is too low.".format(age))
4141
else:
4242
lowerbound = 5 * int(math.floor(age / 5))
4343
upperbound = lowerbound + 4
44-
return '{}-{}'.format(lowerbound, upperbound)
44+
return "{}-{}".format(lowerbound, upperbound)
4545

4646

4747
def age_band_10_years(age: int) -> str:
4848
"""
4949
Place age into appropriate 10 year band
5050
51-
This function takes the age supplied as an argument and returns a string
51+
This function takes the age supplied as an argument and returns a string
5252
representing the relevant 10 year banding.
5353
5454
Parameters
@@ -72,16 +72,16 @@ def age_band_10_years(age: int) -> str:
7272
"""
7373

7474
if age is None:
75-
return 'Age not known'
75+
return "Age not known"
7676

7777
if age >= 90:
7878
if age >= 150:
7979
raise ValueError("The age input: {} is too large.".format(age))
8080
else:
81-
return '90 and over'
81+
return "90 and over"
8282
elif age < 0:
8383
raise ValueError("The age input: {} is too low.".format(age))
8484
else:
8585
lowerbound = 10 * int(math.floor(age / 10))
8686
upperbound = lowerbound + 9
87-
return '{}-{}'.format(lowerbound, upperbound)
87+
return "{}-{}".format(lowerbound, upperbound)

codonPython/check_consistent_measures.py

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

44

5-
def check_consistent_measures(data, geography_col: str = "Org_Level", measure_col: str = "Measure", measures_set: set = set()) -> bool:
5+
def check_consistent_measures(
6+
data,
7+
geography_col: str = "Org_Level",
8+
measure_col: str = "Measure",
9+
measures_set: set = set(),
10+
) -> bool:
611
"""
712
Check every measure is in every geography level.
813
@@ -48,7 +53,7 @@ def check_consistent_measures(data, geography_col: str = "Org_Level", measure_co
4853

4954
if data.isna().any(axis=None):
5055
raise ValueError(
51-
f"Missing values at locations {list(map(tuple, np.argwhere(data.isna().values)))}"
56+
f"Missing values at locations {list(map(tuple, np.argwhere(data.isna().values)))}"
5257
)
5358
if not isinstance(geography_col, str) or not isinstance(measure_col, str):
5459
raise ValueError("Please input strings for column indexes.")
@@ -59,8 +64,7 @@ def check_consistent_measures(data, geography_col: str = "Org_Level", measure_co
5964

6065
# Every geography level should have the same set of measures as the global set.
6166
global_set = measures_set if measures_set else set(data[measure_col].unique())
62-
subsets = data.groupby(geography_col) \
63-
.agg({measure_col: "unique"})
67+
subsets = data.groupby(geography_col).agg({measure_col: "unique"})
6468
subset_agreement = all(set(x) == global_set for x in subsets[measure_col])
6569

6670
return subset_agreement

codonPython/check_consistent_submissions.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import pandas as pd
22

33

4-
def check_consistent_submissions(data, national_geog_level: str = "National", geography_col: str = "Org_Level", submissions_col: str = "Value_Unsuppressed", measure_col: str = "Measure", ) -> bool:
4+
def check_consistent_submissions(
5+
data,
6+
national_geog_level: str = "National",
7+
geography_col: str = "Org_Level",
8+
submissions_col: str = "Value_Unsuppressed",
9+
measure_col: str = "Measure",
10+
) -> bool:
511
"""
612
Check total submissions for each measure are the same across all geography levels
713
except national.
@@ -49,24 +55,28 @@ def check_consistent_submissions(data, national_geog_level: str = "National", ge
4955
"""
5056

5157
if (
52-
not isinstance(submissions_col, str) or
53-
not isinstance(measure_col, str) or
54-
not isinstance(geography_col, str) or
55-
not isinstance(national_geog_level, str)
58+
not isinstance(submissions_col, str)
59+
or not isinstance(measure_col, str)
60+
or not isinstance(geography_col, str)
61+
or not isinstance(national_geog_level, str)
5662
):
57-
raise ValueError("Please input strings for column names and national geography level.")
63+
raise ValueError(
64+
"Please input strings for column names and national geography level."
65+
)
5866
if (
59-
submissions_col not in data.columns or
60-
measure_col not in data.columns or
61-
geography_col not in data.columns
67+
submissions_col not in data.columns
68+
or measure_col not in data.columns
69+
or geography_col not in data.columns
6270
):
6371
raise KeyError("Check column names correspond to the DataFrame.")
6472

6573
# All non-national measures should have only one unique submission number for each
6674
# geography level.
67-
submissions_by_measure = data[data[geography_col] != national_geog_level] \
68-
.groupby(measure_col) \
69-
.agg({submissions_col: "nunique"})
75+
submissions_by_measure = (
76+
data[data[geography_col] != national_geog_level]
77+
.groupby(measure_col)
78+
.agg({submissions_col: "nunique"})
79+
)
7080
result = (submissions_by_measure[submissions_col] == 1).all()
7181

7282
return result

codonPython/check_nat_val.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import pandas as pd
22

33

4-
def check_nat_val(df: pd.DataFrame, breakdown_col: str = "Breakdown",
5-
measure_col: str = "Measure", value_col: str =
6-
"Value_Unsuppressed", nat_val: str = "National") -> bool:
4+
def check_nat_val(
5+
df: pd.DataFrame,
6+
breakdown_col: str = "Breakdown",
7+
measure_col: str = "Measure",
8+
value_col: str = "Value_Unsuppressed",
9+
nat_val: str = "National",
10+
) -> bool:
711
"""
812
Check national value less than or equal to sum of breakdowns.
913
@@ -66,24 +70,32 @@ def check_nat_val(df: pd.DataFrame, breakdown_col: str = "Breakdown",
6670
False
6771
"""
6872

69-
if not isinstance(breakdown_col, str) or not isinstance(measure_col, str)\
70-
or not isinstance(value_col, str):
73+
if (
74+
not isinstance(breakdown_col, str)
75+
or not isinstance(measure_col, str)
76+
or not isinstance(value_col, str)
77+
):
7178
raise ValueError("Please input strings for column indexes.")
7279
if not isinstance(nat_val, str):
7380
raise ValueError("Please input strings for value indexes.")
74-
if breakdown_col not in df.columns or measure_col not in df.columns or\
75-
value_col not in df.columns:
81+
if (
82+
breakdown_col not in df.columns
83+
or measure_col not in df.columns
84+
or value_col not in df.columns
85+
):
7686
raise KeyError("Check column names correspond to the DataFrame.")
77-
# aggregate values by measure and breakdown
78-
grouped = df.groupby([measure_col, breakdown_col]).agg({value_col: sum})\
79-
.reset_index()
87+
# aggregate values by measure and breakdown
88+
grouped = (
89+
df.groupby([measure_col, breakdown_col]).agg({value_col: sum}).reset_index()
90+
)
8091
national = grouped.loc[grouped[breakdown_col] == nat_val].reset_index()
8192
non_national = grouped.loc[grouped[breakdown_col] != nat_val].reset_index()
82-
# check values are less than or equal to national value for each measure
83-
join = pd.merge(non_national, national, left_on=measure_col,
84-
right_on=measure_col, how='left')
85-
left = value_col + '_x'
86-
right = value_col + '_y'
87-
join['Check'] = join[right] <= join[left]
88-
result = all(join['Check'])
93+
# check values are less than or equal to national value for each measure
94+
join = pd.merge(
95+
non_national, national, left_on=measure_col, right_on=measure_col, how="left"
96+
)
97+
left = value_col + "_x"
98+
right = value_col + "_y"
99+
join["Check"] = join[right] <= join[left]
100+
result = all(join["Check"])
89101
return result

codonPython/check_null.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import numpy
22
import pandas as pd
33

4+
45
def check_null(dataframe: pd.DataFrame, columns_to_be_checked: list) -> int:
56
"""
67
Checks a pandas dataframe for null values
78
8-
This function takes a pandas dataframe supplied as an argument and returns a integer value representing any null values found within the columns to check
9+
This function takes a pandas dataframe supplied as an argument and returns a integer value
10+
representing any null values found within the columns to check.
911
1012
Parameters
1113
----------
@@ -29,18 +31,16 @@ def check_null(dataframe: pd.DataFrame, columns_to_be_checked: list) -> int:
2931

3032
if not isinstance(columns_to_be_checked, list):
3133
raise ValueError("Please make sure that all your columns passed are strings")
32-
else:
33-
pass
3434

3535
for eachCol in columns_to_be_checked:
3636
if eachCol not in dataframe.columns:
37-
raise KeyError("Please check the column names correspond to values in the DataFrame.")
38-
else:
39-
pass
37+
raise KeyError(
38+
"Please check the column names correspond to values in the DataFrame."
39+
)
4040

4141
null_count = 0
4242
for eachColumn in columns_to_be_checked:
4343
prev_null_count = null_count
4444
null_count = prev_null_count + (len(dataframe) - dataframe[eachColumn].count())
4545

46-
return null_count
46+
return null_count

codonPython/dateValidator.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
def validDate(date_string: str) -> bool:
55
"""
6-
Validates stringtype dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy` from
6+
Validates stringtype dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy` from
77
years 1900-9999. Leap year support included.
88
99
Parameters
@@ -33,13 +33,15 @@ def validDate(date_string: str) -> bool:
3333
# https://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy
3434
# modified to confine the year dates.
3535
if re.match(
36-
r"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1" +
37-
r"|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2" +
38-
r"))(?:(?:1[9]..|2[0][0-4].))$|^(?:29(\/|-|\.)0?2\3" +
39-
r"(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]" +
40-
r"|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4" +
41-
r"(?:(?:1[9]..|2[0][0-4].))$",
42-
date_string, flags=0):
36+
r"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1"
37+
+ r"|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2"
38+
+ r"))(?:(?:1[9]..|2[0][0-4].))$|^(?:29(\/|-|\.)0?2\3"
39+
+ r"(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]"
40+
+ r"|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4"
41+
+ r"(?:(?:1[9]..|2[0][0-4].))$",
42+
date_string,
43+
flags=0,
44+
):
4345
return True
4446
else:
4547
return False

0 commit comments

Comments
 (0)