Skip to content

Commit

Permalink
Fix deprecation warnings, remove unneeded code
Browse files Browse the repository at this point in the history
  • Loading branch information
jr-hartono committed Feb 2, 2024
1 parent 11804d6 commit 0ee4203
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
5 changes: 3 additions & 2 deletions ensysmod/core/file_upload.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from io import BytesIO
from typing import Any
from zipfile import ZipFile

Expand Down Expand Up @@ -260,8 +261,8 @@ def process_excel_file(
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")
with BytesIO(file.file.read()) as content:
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:
Expand Down
3 changes: 2 additions & 1 deletion ensysmod/core/fine_esm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import chdir
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any
Expand Down Expand Up @@ -28,7 +29,7 @@
EnergyStorage,
EnergyTransmission,
)
from ensysmod.utils.utils import chdir, create_temp_file, df_or_s
from ensysmod.utils.utils import create_temp_file, df_or_s


def generate_esm_from_model(db: Session, model: EnergyModel) -> EnergySystemModel:
Expand Down
8 changes: 4 additions & 4 deletions ensysmod/core/security.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta, timezone
from datetime import UTC, datetime, timedelta
from typing import Any

from jose import jwt
Expand All @@ -12,10 +12,10 @@


def create_access_token(subject: str | Any, expires_delta: timedelta | None = None) -> str:
if expires_delta:
expire = datetime.now(tz=timezone.utc) + expires_delta
if expires_delta is not None:
expire = datetime.now(tz=UTC) + expires_delta
else:
expire = datetime.now(tz=timezone.utc) + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
expire = datetime.now(tz=UTC) + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)

Check warning on line 18 in ensysmod/core/security.py

View check run for this annotation

Codecov / codecov/patch

ensysmod/core/security.py#L18

Added line #L18 was not covered by tests
to_encode = {"exp": expire, "sub": str(subject)}
return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)

Expand Down
4 changes: 2 additions & 2 deletions ensysmod/crud/base_depends_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def get_dataframe(self, db: Session, *, component_id: int) -> pd.DataFrame:
dataset_id = crud.energy_component.get(db, id=component_id).ref_dataset
region_names = list(db.execute(select(Region.name).where(Region.ref_dataset == dataset_id)).scalars().all())

dataframe = pd.DataFrame(0, index=region_names, columns=region_names)
dataframe = pd.DataFrame(0, index=region_names, columns=region_names, dtype=float)
for d in data:
dataframe[d.region_to.name][d.region.name] = getattr(d, self.data_column)
dataframe.loc[d.region.name, d.region_to.name] = getattr(d, self.data_column)
return dataframe

# otherwise return dataframe with 1 row and regions as columns
Expand Down
19 changes: 2 additions & 17 deletions ensysmod/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
from collections.abc import Generator
from contextlib import contextmanager
from pathlib import Path
from tempfile import mkstemp

Expand All @@ -21,19 +19,6 @@ def remove_file(file_path: Path) -> None:
file_path.unlink()


@contextmanager
def chdir(path: Path | str) -> Generator[None, None, None]:
# In Python 3.11 use contextlib.chdir().
current_dir = Path.cwd()
try:
os.chdir(path)
yield
finally:
os.chdir(current_dir)


def df_or_s(df: pd.DataFrame) -> pd.DataFrame | pd.Series:
# Convert a dataframe to a series if it has only one row.
if df.shape[0] == 1:
return df.squeeze(axis=0)
return df
"""Convert a dataframe to a series if it has only one row."""
return df.squeeze(axis=0) if df.shape[0] == 1 else df

0 comments on commit 0ee4203

Please sign in to comment.