Skip to content

Commit

Permalink
Add __bool__ method to TimeStamp and TimeSpan classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Dec 21, 2023
1 parent 17f6041 commit d196556
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 8 additions & 4 deletions fastkml/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def __init__(
super().__init__(ns=ns, name_spaces=name_spaces, id=id, target_id=target_id)
self.timestamp = timestamp

def __bool__(self) -> bool:
"""Return True if the timestamp is valid."""
return bool(self.timestamp)

def etree_element(
self,
precision: Optional[int] = None,
Expand Down Expand Up @@ -214,6 +218,10 @@ def __init__(
self.begin = begin
self.end = end

def __bool__(self) -> bool:
"""Return True if the begin or end date is valid."""
return bool(self.begin) or bool(self.end)

def etree_element(
self,
precision: Optional[int] = None,
Expand All @@ -234,10 +242,6 @@ def etree_element(
f"{self.ns}end",
)
end.text = text
if self.begin == self.end is None:
msg = "Either begin, end or both must be set"
raise ValueError(msg)
# TODO test if end > begin
return element

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion tests/times_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ def test_timespan(self) -> None:
ts.end = None
assert now.dt.isoformat() not in str(ts.to_string())
assert y2k.dt.isoformat() in str(ts.to_string())
assert ts
ts.begin = None
pytest.raises(ValueError, ts.to_string)
assert not ts

def test_feature_timestamp(self) -> None:
now = datetime.datetime.now()
Expand Down

0 comments on commit d196556

Please sign in to comment.