def test_equality(self): """ Test equality comparision of pointwidth """ assert pointwidth(32) == pointwidth(32) assert 32 == pointwidth(32) assert pointwidth(32) == 32.0000
def test_int_conversion(self): """ Test converting a pointwidth into an int and back """ assert int(pointwidth(42)) == 42 assert isinstance(int(pointwidth(32)), int) assert isinstance(pointwidth(pointwidth(32)), pointwidth)
def test_strings(self): """ Test the string representation of pointwidth """ assert "years" in str(pointwidth(55)) assert "months" in str(pointwidth(52)) assert "weeks" in str(pointwidth(50)) assert "days" in str(pointwidth(47)) assert "hours" in str(pointwidth(42)) assert "minutes" in str(pointwidth(36)) assert "seconds" in str(pointwidth(30)) assert "milliseconds" in str(pointwidth(20)) assert "microseconds" in str(pointwidth(10)) assert "nanoseconds" in str(pointwidth(5))
def test_time_conversions(self): """ Test standard pointwidth time conversions """ assert pointwidth(62).years == pytest.approx(146.2171) assert pointwidth(56).years == pytest.approx(2.2846415) assert pointwidth(54).months == pytest.approx(6.854793) assert pointwidth(50).weeks == pytest.approx(1.861606) assert pointwidth(48).days == pytest.approx(3.2578122) assert pointwidth(44).hours == pytest.approx(4.886718) assert pointwidth(38).minutes == pytest.approx(4.581298) assert pointwidth(32).seconds == pytest.approx(4.294967) assert pointwidth(26).milliseconds == pytest.approx(67.108864) assert pointwidth(14).microseconds == pytest.approx(16.384) assert pointwidth(8).nanoseconds == pytest.approx(256)
def test_incr(self): """ Test incrementing a pointwidth """ assert pointwidth(23).incr() == 24
def test_decr(self): """ Test decrementing a pointwidth """ assert pointwidth(23).decr() == 22
def to_nearest_pointwidth(days=0, hours=0, minutes=0, seconds=0, milliseconds=0, microseconds=0, nanoseconds=0, hertz=0): """ Convert time delta of days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds or hertz to nearest pointwidth. This function will return the nearest pointwidth and the margin of error in nanoseconds. Parameters ---------- days : int or float Number of days. hours : int or float Number of hours. minutes : int or float Number of minutes. seconds : int or float Number of seconds. milliseconds : int or float Number of milliseconds. microseconds : int or float Number of mimicrosecondsnutes. nanoseconds : int or float Number of nanoseconds. hertz : int or float Frequency in hertz. Returns ---------- prevpw : int Closest pointwidth of the input time/frequency. preverr : float Margin of errors to the closest pointwidth of the input time/frequency. nextpw : int Closest pointwidth of the input time/frequency. nexterr : float Margin of errors to the closest pointwidth of the input time/frequency. Examples ---------- >>> pw, err = to_nearest_pointwidth(days=1) """ if hertz != 0: seconds = 1 / hertz dt_ns = ns_delta(days=days, hours=hours, minutes=minutes, seconds=seconds, milliseconds=milliseconds, microseconds=microseconds, nanoseconds=nanoseconds) prevpw = int(np.floor(np.log2(dt_ns))) nextpw = int(np.ceil(np.log2(dt_ns))) preverr = dt_ns - pointwidth(prevpw).nanoseconds nexterr = pointwidth(nextpw).nanoseconds - dt_ns if preverr <= nexterr: return prevpw, preverr else: return nextpw, nexterr