Closed magmax closed 4 months ago
Fixes #1037, where comparison between Timestamp were wrong.
Timestamp
I've chosen this solution after testing different ones because it is the faster one. Files and speed tests here.
Different implementations:
from collections import namedtuple class TimestampNamedtuple(namedtuple("Timestamp", ["sec", "nsec"])): """A nanosecond-resolution timestamp.""" def __new__(cls, sec: float, nsec: float) -> None: if nsec < 0 or nsec >= 1e9: raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}") if sec < 0: nsec = -nsec return super().__new__(cls, int(sec), int(nsec)) def __str__(self) -> str: return f"{self.sec}.{self.nsec:09d}" def __repr__(self) -> str: return f"Timestamp({self.sec}, {self.nsec})" def __float__(self) -> float: return float(self.sec) + float(self.nsec) / 1e9
Here there is no need to implement comparison operators because they are already implemented in the parent class.
This was the worse implementation, around 50% slower than the chosen one.
def __gt__(self, other: "Timestamp") -> bool: return (self.sec, self.nsec) > (other.sec, other.nsec)
def __gt__(self, other: "Timestamp") -> bool: return self.sec > other.sec or (self.sec == other.sec and self.nsec > other.nsec)
Fixes #1037, where comparison between
Timestamp
were wrong.I've chosen this solution after testing different ones because it is the faster one. Files and speed tests here.
Different implementations:
Changing the full class by a namedtuple
Here there is no need to implement comparison operators because they are already implemented in the parent class.
This was the worse implementation, around 50% slower than the chosen one.
tuples
logical operators