Skip to content

Commit

Permalink
fix ruff check errors for refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
CunliangGeng committed Jun 11, 2024
1 parent 0be9ee5 commit dc2ff65
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
34 changes: 29 additions & 5 deletions src/nplinker/metabolomics/abc.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
from abc import ABC
from abc import abstractmethod
from collections.abc import Sequence
from typing import TYPE_CHECKING


if TYPE_CHECKING:
from typing import Mapping
from typing import Sequence
from .molecular_family import MolecularFamily
from .spectrum import Spectrum


class SpectrumLoaderBase(ABC):
"""Abstract base class for SpectrumLoader."""

@property
@abstractmethod
def spectra(self) -> Sequence["Spectrum"]: ...
def spectra(self) -> Sequence["Spectrum"]:
"""Get Spectrum objects.
Returns:
A sequence of Spectrum objects.
"""


class MolecularFamilyLoaderBase(ABC):
"""Abstract base class for MolecularFamilyLoader."""

@abstractmethod
def get_mfs(self, keep_singleton: bool) -> Sequence["MolecularFamily"]:
"""Get MolecularFamily objects.
Expand All @@ -26,17 +36,31 @@ def get_mfs(self, keep_singleton: bool) -> Sequence["MolecularFamily"]:
only one spectrum.
Returns:
A list of MolecularFamily objects.
A sequence of MolecularFamily objects.
"""


class FileMappingLoaderBase(ABC):
"""Abstract base class for FileMappingLoader."""

@property
@abstractmethod
def mappings(self) -> dict[str, list[str]]: ...
def mappings(self) -> Mapping[str, Sequence[str]]:
"""Get file mappings.
Returns:
A mapping from spectrum ID to the names of files where the spectrum occurs.
"""


class AnnotationLoaderBase(ABC):
"""Abstract base class for AnnotationLoader."""

@property
@abstractmethod
def annotations(self) -> dict[str, dict]: ...
def annotations(self) -> Mapping[str, Mapping]:
"""Get annotations.
Returns:
A mapping from spectrum ID to its annotations.
"""
2 changes: 1 addition & 1 deletion src/nplinker/schemas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def validate_podp_json(json_data: dict) -> None:
All validation error messages are collected and raised as a single
ValueError.
Parameters:
Args:
json_data: The JSON data to validate.
Raises:
Expand Down
18 changes: 18 additions & 0 deletions src/nplinker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ def is_file_format(file: str | PathLike, format: str = "tsv") -> bool:


def calculate_md5(fpath: str | PathLike, chunk_size: int = 1024 * 1024) -> str:
"""Calculate the MD5 checksum of a file.
Args:
fpath: Path to the file.
chunk_size: Chunk size for reading the file. Defaults to 1024*1024.
Returns:
MD5 checksum of the file.
"""
if sys.version_info >= (3, 9):
md5 = hashlib.md5(usedforsecurity=False)
else:
Expand All @@ -120,6 +129,15 @@ def calculate_md5(fpath: str | PathLike, chunk_size: int = 1024 * 1024) -> str:


def check_md5(fpath: str | PathLike, md5: str) -> bool:
"""Verify the MD5 checksum of a file.
Args:
fpath: Path to the file.
md5: MD5 checksum to verify.
Returns:
True if the MD5 checksum matches, False otherwise.
"""
return md5 == calculate_md5(fpath)


Expand Down

0 comments on commit dc2ff65

Please sign in to comment.