From 96c9126448bef0199131e92205d41e3fe6051f8e Mon Sep 17 00:00:00 2001 From: jr-hartono <130978844+jr-hartono@users.noreply.github.com> Date: Fri, 22 Dec 2023 18:09:23 +0100 Subject: [PATCH] Fix linting errors --- ensysmod/core/file_upload.py | 25 +++++++++++-------- ensysmod/schemas/transmission_distance.py | 2 +- .../schemas/yearly_full_load_hours_min.py | 2 -- tests/utils/data_generator/datasets.py | 1 + 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/ensysmod/core/file_upload.py b/ensysmod/core/file_upload.py index 65d7d7f..3517774 100644 --- a/ensysmod/core/file_upload.py +++ b/ensysmod/core/file_upload.py @@ -215,16 +215,7 @@ def process_excel_file( If as_matrix is true, the file is read as a matrix, otherwise it is read per column. """ try: - if isinstance(file, UploadFile): - file_path = file.filename if file.filename is not None else "" - content = file.file.read() - df: pd.DataFrame = pd.read_excel(content, engine="openpyxl") - elif isinstance(file, tuple): - zip_archive, file_path= file - with zip_archive.open(file_path) as content: - df: pd.DataFrame = pd.read_excel(content, engine="openpyxl") - else: - raise TypeError("Unknown file type.") + file_path, df = read_excel_file(file) if as_matrix: # determine the labels (region and region_to) of rows and columns @@ -265,3 +256,17 @@ def process_excel_file( except Exception as e: return FileUploadResult(status=FileStatus.ERROR, file=file_path, message=str(e)) + + +def read_excel_file(file: UploadFile | tuple[ZipFile, str]) -> tuple[str, pd.DataFrame]: + if isinstance(file, UploadFile): + file_path = file.filename if file.filename is not None else "" + content = file.file.read() + df: pd.DataFrame = pd.read_excel(content, engine="openpyxl") + elif isinstance(file, tuple): + zip_archive, file_path = file + with zip_archive.open(file_path) as content: + df: pd.DataFrame = pd.read_excel(content, engine="openpyxl") + else: + raise TypeError(f"Unknown file type: {type(file)}") + return file_path, df diff --git a/ensysmod/schemas/transmission_distance.py b/ensysmod/schemas/transmission_distance.py index 9845bde..26196fd 100644 --- a/ensysmod/schemas/transmission_distance.py +++ b/ensysmod/schemas/transmission_distance.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel, Field, NonNegativeFloat, validator +from pydantic import BaseModel, Field, validator from ensysmod.schemas.base_ref_component_region import RefCRBase, RefCRBaseBase, RefCRBaseCreate, RefCRBaseUpdate from ensysmod.schemas.region import Region diff --git a/ensysmod/schemas/yearly_full_load_hours_min.py b/ensysmod/schemas/yearly_full_load_hours_min.py index d9fa4ad..18a35db 100644 --- a/ensysmod/schemas/yearly_full_load_hours_min.py +++ b/ensysmod/schemas/yearly_full_load_hours_min.py @@ -26,14 +26,12 @@ class YearlyFullLoadHoursMinCreate(YearlyFullLoadHoursMinBase, RefCRBaseCreate): """ - class YearlyFullLoadHoursMinUpdate(YearlyFullLoadHoursMinBase, RefCRBaseUpdate): """ Attributes to receive via API on update of a YearlyFullLoadHoursMin entry. """ - class YearlyFullLoadHoursMin(YearlyFullLoadHoursMinBase, RefCRBase): """ Attributes to return via API for a YearlyFullLoadHoursMin entry. diff --git a/tests/utils/data_generator/datasets.py b/tests/utils/data_generator/datasets.py index adb5c3b..a1c0344 100644 --- a/tests/utils/data_generator/datasets.py +++ b/tests/utils/data_generator/datasets.py @@ -55,6 +55,7 @@ def dataset_create( ) return crud.dataset.create(db=db, obj_in=create_request) + @contextmanager def get_dataset_zip(folder_name: str) -> Generator[Path, None, None]: """