Ejemplo n.º 1
0
    def test_from_sec_nsec(self):
        """This tests that time offsets can be created from second:nanosecond pairs."""
        tests_ts = [
            (("1:1", ), TimeOffset(1, 1)),
            (("-1:1", ), TimeOffset(1, 1, sign=-1)),
            (("1", ), TimeOffset(1, 0)),
        ]

        for t in tests_ts:
            with self.subTest(t=t):
                r = TimeOffset.from_sec_nsec(*t[0])
                self.assertEqual(
                    r,
                    t[1],
                    msg="TimeOffset.from_sec_nsec{!r} == {!r}, expected {!r}".
                    format(t[0], r, t[1]))

        bad_params = [
            ("0:0:1", ),
        ]

        for params in bad_params:
            with self.assertRaises(TsValueError):
                TimeOffset.from_sec_nsec(*params)
    def test_convert_sec_nsec(self):
        """This tests that the conversion to and from TAI second:nanosecond pairs works as expected."""

        tests_ts = [("0:0", TimeOffset(0, 0), "0:0"),
                    ("0:1", TimeOffset(0, 1), "0:1"),
                    ("-0:1", TimeOffset(0, 1, -1), "-0:1"),
                    ("5", TimeOffset(5, 0), "5:0"),
                    ("5:1", TimeOffset(5, 1), "5:1"),
                    ("-5:1", TimeOffset(5, 1, -1), "-5:1"),
                    ("5:999999999", TimeOffset(5, 999999999), "5:999999999")]

        for t in tests_ts:
            ts = TimeOffset.from_sec_nsec(t[0])
            self.assertEqual(ts,
                             t[1],
                             msg="Called with {} {} {}".format(
                                 t[0], t[1], t[2]))
            ts_str = ts.to_sec_nsec()
            self.assertEqual(ts_str,
                             t[2],
                             msg="Called with {} {} {}".format(
                                 t[0], t[1], t[2]))
            self.assertEqual(ts_str, str(ts))
Ejemplo n.º 3
0
def decode_value(o: JSONSerialisable) -> MediaJSONSerialisable:
    if isinstance(o, dict):
        if len(o.keys()) == 2 and "numerator" in o and "denominator" in o:
            return Fraction(o['numerator'], o['denominator'])
        else:
            res = {}
            for key in o:
                res[key] = decode_value(o[key])
            return res
    elif isinstance(o, list):
        return [decode_value(v) for v in o]
    elif isinstance(o, str):
        if re.match(UUID_REGEX, o):
            return uuid.UUID(o)
        elif re.match(r'^\d+:\d+$', o):
            return Timestamp.from_tai_sec_nsec(o)
        elif re.match(r'^(\+|-)\d+:\d+$', o):
            return TimeOffset.from_sec_nsec(o)
        elif re.match(r'^(\(|\[)?(\d+:\d+)?_(\d+:\d+)?(\)|\])?$', o):
            return TimeRange.from_str(o)
        elif o == "()":
            return TimeRange.never()
    return o