Example #1
0
 def test_returns_int(self):
     """
     Assert ns_delta returns int if floats used for arguments
     """
     val = ns_delta(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1)
     assert val == int(1 + 1e3 + 1e6 + 1e9 + (1e9 * 60)  + (1e9 * 60 * 60) + \
         (1e9 * 60 * 60 * 24) )
     assert isinstance(val, int)
Example #2
0
class TestPointwidth(object):

    @pytest.mark.parametrize("delta, expected", [
        (timedelta(days=365), 54),
        (timedelta(days=30), 51),
        (timedelta(days=7), 49),
        (timedelta(days=1), 46),
        (timedelta(hours=4), 43),
        (timedelta(minutes=15), 39),
        (timedelta(seconds=30), 34),
    ])

    def test_from_timedelta(self, delta, expected):
        """
        Test getting the closest point width from a timedelta
        """
        assert pointwidth.from_timedelta(delta) == expected

    @pytest.mark.parametrize("nsec, expected", [
        (ns_delta(days=365), 54),
        (ns_delta(days=30), 51),
        (ns_delta(days=7), 49),
        (ns_delta(days=1), 46),
        (ns_delta(hours=12), 45),
        (ns_delta(minutes=30), 40),
        (ns_delta(seconds=1), 29),
    ])

    def test_from_nanoseconds(self, nsec, expected):
        """
        Test getting the closest point width from nanoseconds
        """
        assert pointwidth.from_nanoseconds(nsec) == expected

    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_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_equality(self):
        """
        Test equality comparision of pointwidth
        """
        assert pointwidth(32) == pointwidth(32)
        assert 32 == pointwidth(32)
        assert pointwidth(32) == 32.0000

    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_decr(self):
        """
        Test decrementing a pointwidth
        """
        assert pointwidth(23).decr() == 22

    def test_incr(self):
        """
        Test incrementing a pointwidth
        """
        assert pointwidth(23).incr() == 24
Example #3
0
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
Example #4
0
 def test_ns_delta_precision(self):
     """
     Assert ns_delta deals with real inputs
     """
     val = ns_delta(days=365, minutes=0.5, nanoseconds=1)
     assert val == int(1e9 * 60 * 60 * 24 * 365) + int(1e9 * 30) + 1
Example #5
0
 def test_ns_delta(self):
     """
     Assert ns_delta converts inputs properly
     """
     val = ns_delta(1, 2, 1, 3, 1, 23, 1)
     assert val == 93663001023001