Skip to content
Snippets Groups Projects

Feature/comp 114 python code quality

Merged Remco Tukker requested to merge feature/COMP-114_python_code_quality into develop
23 files
+ 219
338
Compare changes
  • Side-by-side
  • Inline
Files
23
 
import logging
import openpyxl
import openpyxl
import os
import os
import logging
from compendium_v2.db.model import FeeType
from compendium_v2.db.model import FeeType
from compendium_v2.environment import setup_logging
from compendium_v2.environment import setup_logging
@@ -9,9 +9,7 @@ setup_logging()
@@ -9,9 +9,7 @@ setup_logging()
logger = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
EXCEL_FILE = os.path.join(
EXCEL_FILE = os.path.join(os.path.dirname(__file__), "xlsx", "2021_Organisation_DataSeries.xlsx")
os.path.dirname(__file__), "xlsx",
"2021_Organisation_DataSeries.xlsx")
def fetch_budget_excel_data():
def fetch_budget_excel_data():
@@ -32,12 +30,7 @@ def fetch_budget_excel_data():
@@ -32,12 +30,7 @@ def fetch_budget_excel_data():
if budget is not None:
if budget is not None:
budget = round(budget / 1000000, 2)
budget = round(budget / 1000000, 2)
if budget > 200:
if budget > 200:
logger.info(
logger.info(f'{nren} has budget set to >200M EUR for {year}. ({budget})')
f'{nren} has budget set to '
f'>200M EUR for {year}. ({budget})')
# process the data (e.g. save to database)
# print(f"NREN: {nren}, Budget: {budget}, Year: {year}")
yield nren.upper(), budget, year
yield nren.upper(), budget, year
@@ -52,17 +45,12 @@ def fetch_funding_excel_data():
@@ -52,17 +45,12 @@ def fetch_funding_excel_data():
def hard_number_convert(s, source_name, nren, year):
def hard_number_convert(s, source_name, nren, year):
if s is None:
if s is None:
logger.info(
logger.info(f'Invalid Value :{nren} has empty value for {source_name} for year ({year})')
f'Invalid Value :{nren} has empty value for {source_name}.'
+ f'for year ({year})')
return float(0)
return float(0)
""" Returns True if string is a number. """
try:
try:
return float(s)
return float(s)
except ValueError:
except ValueError:
logger.info(
logger.info(f'Invalid Value :{nren} has empty value for {source_name} for year ({year}) with value ({s})')
f'Invalid Value :{nren} has empty value for {source_name}.'
+ f'for year ({year}) with value ({s})')
return float(0)
return float(0)
# iterate over the rows in the worksheet
# iterate over the rows in the worksheet
@@ -76,7 +64,6 @@ def fetch_funding_excel_data():
@@ -76,7 +64,6 @@ def fetch_funding_excel_data():
gov_public_bodies = ws.cell(row=row, column=col_start + 3).value
gov_public_bodies = ws.cell(row=row, column=col_start + 3).value
other_european_funding = ws.cell(row=row, column=col_start + 4).value
other_european_funding = ws.cell(row=row, column=col_start + 4).value
other = ws.cell(row=row, column=col_start + 5).value
other = ws.cell(row=row, column=col_start + 5).value
print(nren, client_institution, commercial, geant_subsidy, gov_public_bodies, other_european_funding, other)
client_institution = hard_number_convert(client_institution, "client institution", nren, year)
client_institution = hard_number_convert(client_institution, "client institution", nren, year)
commercial = hard_number_convert(commercial, "commercial", nren, year)
commercial = hard_number_convert(commercial, "commercial", nren, year)
@@ -89,10 +76,7 @@ def fetch_funding_excel_data():
@@ -89,10 +76,7 @@ def fetch_funding_excel_data():
# process the data (e.g. save to database)
# process the data (e.g. save to database)
if nren is not None:
if nren is not None:
yield (nren.upper(), year, client_institution,
yield (nren.upper(), year, client_institution, european_funding, gov_public_bodies, commercial, other)
european_funding,
gov_public_bodies,
commercial, other)
def create_points_for_year_from_2018(start_row, end_row, year, col_start):
def create_points_for_year_from_2018(start_row, end_row, year, col_start):
for row in range(start_row, end_row):
for row in range(start_row, end_row):
@@ -112,10 +96,7 @@ def fetch_funding_excel_data():
@@ -112,10 +96,7 @@ def fetch_funding_excel_data():
# process the data (e.g. save to database)
# process the data (e.g. save to database)
if nren is not None:
if nren is not None:
yield (nren.upper(), year, client_institution,
yield (nren.upper(), year, client_institution, european_funding, gov_public_bodies, commercial, other)
european_funding,
gov_public_bodies,
commercial, other)
# For 2016
# For 2016
yield from create_points_for_year_until_2017(8, 50, 2016, 43, 45)
yield from create_points_for_year_until_2017(8, 50, 2016, 43, 45)
@@ -147,26 +128,22 @@ def fetch_charging_structure_excel_data():
@@ -147,26 +128,22 @@ def fetch_charging_structure_excel_data():
# extract the data from the row
# extract the data from the row
nren = ws.cell(row=row, column=col_start).value
nren = ws.cell(row=row, column=col_start).value
charging_structure = ws.cell(row=row, column=col_start + 1).value
charging_structure = ws.cell(row=row, column=col_start + 1).value
logger.info(
logger.info(f'NREN: {nren}, Charging Structure: {charging_structure}, Year: {year}')
f'NREN: {nren}, Charging Structure: {charging_structure},'
f' Year: {year}')
if charging_structure is not None:
if charging_structure is not None:
if "do not charge" in charging_structure:
if "do not charge" in charging_structure:
charging_structure = FeeType.no_charge.value
charging_structure = FeeType.no_charge
elif "combination" in charging_structure:
elif "combination" in charging_structure:
charging_structure = FeeType.combination.value
charging_structure = FeeType.combination
elif "flat" in charging_structure:
elif "flat" in charging_structure:
charging_structure = FeeType.flat_fee.value
charging_structure = FeeType.flat_fee
elif "usage-based" in charging_structure:
elif "usage-based" in charging_structure:
charging_structure = FeeType.usage_based_fee.value
charging_structure = FeeType.usage_based_fee
elif "Other" in charging_structure:
elif "Other" in charging_structure:
charging_structure = FeeType.other.value
charging_structure = FeeType.other
else:
else:
charging_structure = None
charging_structure = None
logger.info(
logger.info(f'NREN: {nren}, Charging Structure: {charging_structure}, Year: {year}')
f'NREN: {nren}, Charging Structure: {charging_structure},'
f' Year: {year}')
yield nren.upper(), year, charging_structure
yield nren.upper(), year, charging_structure
Loading