Skip to content

Commit

Permalink
refactor __lt__ function
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-krechan committed Oct 7, 2024
1 parent 875c5ae commit 083e1d1
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/kohlrahbi/docxfilefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,31 @@ def from_path(cls, path: Path) -> "EdiEnergyDocument":
valid_until_date=int(valid_until_date),
)

def __lt__(self, other: "EdiEnergyDocuement") -> bool:
"""
Compare two EdiEnergyDocuement instances based on their document_version and valid_until_date.
def __lt__(self, other: "EdiEnergyDocument") -> bool:
"""
Compare two EdiEnergyDocument instances based on their document_version, valid_until_date, and valid_from_date.
I did not know how the tuple comparison works in Python, so I looked it up:
Python compares tuples lexicographically, meaning it compares the elements one by one from left to right.
The comparison starts with the first elements of both tuples:
If self.document_version is less than other.document_version, the entire expression evaluates to True.
If self.document_version is greater than other.document_version, the entire expression evaluates to False.
If self.document_version is equal to other.document_version, Python moves to the next elements in the tuples.
This process continues with self.valid_until_date vs. other.valid_until_date and then self.valid_from_date vs.
other.valid_from_date.
if self.document_version == other.document_version:
if self.valid_until_date == other.valid_until_date:
return self.valid_from_date < other.valid_from_date
return self.valid_until_date < other.valid_until_date
Args:
other (EdiEnergyDocument): The other document to compare against.
return self.document_version < other.document_version
Returns:
bool: True if this document is considered less than the other document, False otherwise.
"""
return (self.document_version, self.valid_until_date, self.valid_from_date) < (
other.document_version,
other.valid_until_date,
other.valid_from_date,
)


def extract_document_version_and_valid_dates(filename: str) -> tuple[str, str, str]:
Expand Down

0 comments on commit 083e1d1

Please sign in to comment.