Skip to content

Commit

Permalink
refactor: Make PrimerPair.amplicon a cached property (#93)
Browse files Browse the repository at this point in the history
See this discussion for context:
#89 (comment)

This PR also updates the mypy config to exclude the `mkdocs`
directories.
  • Loading branch information
msto authored Nov 20, 2024
1 parent ebc72f7 commit 385e1b1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
19 changes: 4 additions & 15 deletions prymer/api/primer_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class methods to represent a primer pair. The primer pair is comprised of a lef
""" # noqa: E501

from dataclasses import dataclass
from dataclasses import field
from dataclasses import replace
from functools import cached_property
from typing import Iterator
from typing import Optional

Expand All @@ -55,7 +55,7 @@ class methods to represent a primer pair. The primer pair is comprised of a lef
from prymer.api.span import Span


@dataclass(frozen=True, init=True, kw_only=True, slots=True)
@dataclass(frozen=True, init=True, kw_only=True)
class PrimerPair(OligoLike):
"""
Represents a pair of primers that work together to amplify an amplicon. The
Expand All @@ -79,22 +79,11 @@ class PrimerPair(OligoLike):
amplicon_tm: float
penalty: float
amplicon_sequence: Optional[str] = None
_amplicon: Span = field(init=False)

def __post_init__(self) -> None:
# Derive the amplicon from the left and right primers. This must be done before
# calling super() as `PrimerLike.id` depends on the amplicon being set
object.__setattr__(
self,
"_amplicon",
PrimerPair.calculate_amplicon_span(self.left_primer, self.right_primer),
)
super(PrimerPair, self).__post_init__()

@property
@cached_property
def amplicon(self) -> Span:
"""Returns the mapping for the amplicon"""
return self._amplicon
return self.calculate_amplicon_span(self.left_primer, self.right_primer)

@property
def span(self) -> Span:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true
exclude = ["site/", "docs/"]

[[tool.mypy.overrides]]
module = "defopt"
Expand Down

0 comments on commit 385e1b1

Please sign in to comment.