コード例 #1
0
 def test_longer_get(self):
     s = deepcopy(self.simple)
     p = WildPath("b.0")
     self.assertEqual(p.get_in(s), 2)
     p = WildPath("c.d")
     self.assertEqual(p.get_in(s), 4)
     p = WildPath("d.e")
     self.assertEqual(p.get_in(s), 6)
コード例 #2
0
 def test_index_composite(self):
     obj = list(range(8))
     path = WildPath("1:3|5:7")  # starting with b or c
     self.assertEqual(path.get_in(obj), [1, 2, 5, 6])
     path = WildPath("::2&::3")  # starting with b AND ending with c
     self.assertEqual(path.get_in(obj), [0, 6])
     path = WildPath("!(::2|::3)")
     self.assertEqual(path.get_in(obj), [1, 5, 7])
コード例 #3
0
 def test_flat(self):
     obj = deepcopy(self.simple)
     path = WildPath("f.*.*.*.1")
     self.assertEqual(
         set(path.get_in(obj, flat=True)),
         {7, 8, 0, 9, 1, 2, 4, 3})  #order is not preserved for dicts
     path = WildPath("f.*.*.*")
     self.assertTrue(
         all(isinstance(p, list) for p in path.get_in(obj, flat=True)))
コード例 #4
0
 def test_basic_set(self):
     p1 = WildPath("a")
     p2 = WildPath("1")
     d = dict(a=1)
     l = [0, 1]
     o = Object(a=1)
     p1._set_in(d, 0)
     p2._set_in(l, 0)
     p1._set_in(o, 0)
     self.assertEqual(p1.get_in(d), 0)
     self.assertEqual(p2.get_in(l), 0)
     self.assertEqual(p1.get_in(o), 0)
コード例 #5
0
    def test_wildslice_or(self):
        L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        path_1 = WildPath(":2|3:")
        path_2 = WildPath("::2|::3")
        path_3 = WildPath("::-2|::-3")
        path_4 = WildPath(":3|2:")  #all
        path_5 = WildPath("!(:2|3:)")

        #  get_in
        self.assertEqual(path_1.get_in(L),
                         [0, 1, 3, 4, 5, 6, 7, 8, 9])  # not 2
        self.assertEqual(path_2.get_in(L), [0, 2, 4, 6, 8, 3, 9])
        self.assertEqual(path_3.get_in(L), [9, 7, 5, 3, 1, 6, 0])
        self.assertEqual(path_4.get_in(L), L)
        self.assertEqual(path_5.get_in(L), [2])
コード例 #6
0
    def test_constant_set(self):
        s = deepcopy(self.simple)
        p = WildPath("e.*.b")
        p._set_in(s, 13)
        self.assertEqual(p.get_in(s), [13, 13, 13])

        s = deepcopy(self.simple)
        p = WildPath("e.:2.*")
        p._set_in(s, 13)
        self.assertEqual(p.get_in(s), [{'a': 13, 'b': 13}, {'c': 13, 'b': 13}])

        s = deepcopy(self.simple)
        p = WildPath("e.:2")
        p._set_in(s, 13)
        self.assertEqual(p.get_in(s), [13, 13])
コード例 #7
0
 def test_longer_set(self):
     s = deepcopy(self.simple)
     p = WildPath("b.0")
     p._set_in(s, 11)
     self.assertEqual(p.get_in(s), 11)
     p = WildPath("c.d")
     p._set_in(s, 11)
     self.assertEqual(p.get_in(s), 11)
     p = WildPath("d.e")
     p._set_in(s, 11)
     self.assertEqual(p.get_in(s), 11)
     s = deepcopy(self.simple)
     p = WildPath("e.:2.b")
     p._set_in(s, [11, 12])
     self.assertEqual(p.get_in(s), [11, 12])
コード例 #8
0
 def test_wild_set(self):
     p1 = WildPath("bb.*")
     p2 = WildPath("b*.1")
     p3 = WildPath("c*.e")
     p4 = WildPath("c*.e*")
     s = deepcopy(self.complex)
     p1._set_in(s, [14, 15])
     self.assertEqual(p1.get_in(s), [14, 15])
     s = deepcopy(self.complex)
     p2._set_in(s, {'ba': 13, 'bb': 15})
     self.assertEqual(p2.get_in(s), {'ba': 13, 'bb': 15})
     s = deepcopy(self.complex)
     p3._set_in(s, {'ca': 17, 'cb': 18})
     self.assertEqual(p3.get_in(s), {'ca': 17, 'cb': 18})
     s = deepcopy(self.complex)
     p4._set_in(s, {'ca': {'e': 17}, 'cb': {'e': 18}})
     self.assertEqual(p4.get_in(s), {'ca': {'e': 17}, 'cb': {'e': 18}})
コード例 #9
0
 def test_pop(self):
     for path_string in [
             "b*", "ca|cb", "ca.d", "gg.*.b", "ff.*", "ff.:", "*", "ff.::2",
             "ff.-1:0:-2"
     ]:
         obj = deepcopy(self.complex)
         path = WildPath(path_string)
         get = path.get_in(obj)
         self.assertEqual(path.pop_in(obj), get)
コード例 #10
0
    def test_wild_not_slice(self):
        L = [0, 1, 2, 3, 4, 5]
        path_1 = WildPath("!:")
        path_2 = WildPath("!::2")
        path_3 = WildPath("!1:3")
        path_4 = WildPath("!-1:0:-2")
        #  get_in
        self.assertEqual(path_1.get_in(L), [])
        self.assertEqual(path_2.get_in(L), [1, 3, 5])
        self.assertEqual(path_3.get_in(L), [0, 3, 4, 5])
        self.assertEqual(path_4.get_in(L), [0, 2, 4])
        #  set_in
        L = [0, 1, 2, 3, 4, 5]
        path_1.set_in(L, [1, 2, 3])
        self.assertEqual(L, [0, 1, 2, 3, 4, 5])

        L = [0, 1, 2, 3, 4, 5]
        path_2.set_in(L, [1, 2, 3])
        self.assertEqual(L, [0, 1, 2, 2, 4, 3])

        L = [0, 1, 2, 3, 4, 5]
        path_3.set_in(L, [1, 2, 3, 4])
        self.assertEqual(L, [1, 1, 2, 2, 3, 4])

        L = [0, 1, 2, 3, 4, 5]
        path_4.set_in(L, [1, 2, 3])
        self.assertEqual(L, [1, 1, 2, 3, 3, 5])
        #  del_in
        L = [0, 1, 2, 3, 4, 5]
        path_1.del_in(L)
        self.assertEqual(L, [0, 1, 2, 3, 4, 5])

        L = [0, 1, 2, 3, 4, 5]
        path_2.del_in(L)
        self.assertEqual(L, [0, 2, 4])

        L = [0, 1, 2, 3, 4, 5]
        path_3.del_in(L)
        self.assertEqual(L, [1, 2])

        L = [0, 1, 2, 3, 4, 5]
        path_4.del_in(L)
        self.assertEqual(L, [1, 3, 5])
コード例 #11
0
    def test_google_example(self):
        def get_geo_locations(json_route):
            geo_locs = []
            for json_step in json_route["routes"][0]["legs"][0][
                    "steps"]:  # there is only 1 route and 1 leg in the response
                geo_locs.append({
                    "start_location": json_step["start_location"],
                    "end_location": json_step["end_location"]
                })
            return geo_locs

        geo_locations_1 = get_geo_locations(self.google_route)

        location_path = WildPath("routes.0.legs.0.steps.*.*_location")
        geo_locations_2 = location_path.get_in(self.google_route)
        self.assertEqual(geo_locations_1, geo_locations_2)
コード例 #12
0
 def test_simple_keys(self):
     obj = {"a": 1, "b": 2, "c": 3}
     wildkey_expected = {
         "*": {"a", "b", "c"},
         "(*)": {"a", "b", "c"},
         "!(*)": set(),
         "(!*)": set(),
         "!a": {"b", "c"},
         "a*": {"a"},
         "!a*": {"b", "c"},
         "!(a*)": {"b", "c"},
         "a|b": {"a", "b"},
         "a&b": set(),
         "a|!b": {"a", "c"},
         "a&!b": {"a"},
         "!(a|b)": {"c"},
         "!(a&b)": {"a", "b", "c"}
     }
     for wildkey, expected in wildkey_expected.items():
         path = WildPath(wildkey)
         self.assertEqual(set(path.get_in(obj).keys()), expected)
コード例 #13
0
 def test_simple_indices(self):
     obj = tuple(range(3))
     wildkey_expected = {
         "*": {0, 1, 2},
         ":": {0, 1, 2},
         "(:)": {0, 1, 2},
         "!(:)": set(),
         "(!:)": set(),
         "!1": {0, 2},
         "1:": {1, 2},
         "!1:": {0},
         "!(1:)": {0},
         "1|2": {1, 2},
         "1&2": set(),
         "1|!2": {0, 1},
         "1&!2": {1},
         "!(1|2)": {0},
         "!(1&2)": {0, 1, 2}
     }
     for wildkey, expected in wildkey_expected.items():
         path = WildPath(wildkey)
         self.assertEqual(set(path.get_in(obj)), expected)
コード例 #14
0
 def test_wild_del(self):
     p1 = WildPath("bb.*")
     p2 = WildPath("b*.1")
     p3 = WildPath("c*.e")
     p4 = WildPath("c*.e*")
     p5 = WildPath("*")
     s = deepcopy(self.complex)
     p1._del_in(s)
     self.assertEqual(p1.get_in(s), [])
     s = deepcopy(self.complex)
     p2._del_in(s)
     self.assertEqual(s.ba, [2])
     self.assertEqual(s.bb, [4])
     s = deepcopy(self.complex)
     p3._del_in(s)
     self.assertEqual(s.ca, {"d": 6, "f": 8})
     self.assertEqual(s.cb.__dict__, {})
     s = deepcopy(self.complex)
     p4._del_in(s)
     self.assertEqual(s.ca, {"d": 6, "f": 8})
     self.assertEqual(s.cb.__dict__, {})
     s = deepcopy(self.complex)
     p5._del_in(s)
     self.assertEqual(s.__dict__, {})
コード例 #15
0
 def test_key_and(self):
     obj = dict(a=1, b=2, c=3, aa=4)
     path = WildPath("a&b&c")  # no key is a and b and c
     self.assertEqual(path.get_in(obj), dict())
コード例 #16
0
    def test_wild_path_example(self):
        agenda = deepcopy(self.agenda)
        try:
            from wildpath.paths import WildPath

            wildpath = WildPath("items.*.duration")

            durations = wildpath.get_in(
                agenda
            )  # retrieves all the durations of the items on the agenda
            assert durations == ["5 minutes", "25 minutes", "5 minutes"]

            wildpath._set_in(agenda,
                             ["10 minutes", "50 minutes", "10 minutes"
                              ])  # setting all the values,
            assert wildpath.get_in(agenda) == [
                "10 minutes", "50 minutes", "10 minutes"
            ]

            wildpath._set_in(
                agenda, "30 minutes")  # or replacing all with a single value,
            assert wildpath.get_in(agenda) == [
                "30 minutes", "30 minutes", "30 minutes"
            ]

            wildpath._del_in(
                agenda)  # delete all the items at wildpath from the structure
            assert wildpath.has_in(
                agenda
            ) == False  # `has_in` checks if all the items at wildpath are there

            # To get the start and end time of the meeting:

            wildpath = WildPath("*_time")
            assert wildpath.get_in(agenda) == {
                "start_time": "10:00",
                "end_time": "11:00"
            }

            #  WildPath supports a number of wildcard(-like) constructs

            # '|' lets you select multiple keys

            wildpath = WildPath("start_time|end_time")
            assert wildpath.get_in(agenda) == {
                "start_time": "10:00",
                "end_time": "11:00"
            }

            # '?' stands for a single character
            assert WildPath("item?").get_in({
                "item1": "chair",
                "item2": "table",
                "count": 2
            }) == {
                "item1": "chair",
                "item2": "table"
            }

            # '!' at the start of a key definition in a wildpath:
            assert WildPath("!item?").get_in({
                "item1": "chair",
                "item2": "table",
                "count": 2
            }) == {
                "count": 2
            }

            wildpath = WildPath("start_time|end_time")
            assert wildpath.get_in(agenda) == {
                "start_time": "10:00",
                "end_time": "11:00"
            }

            # similarly it supports slices as wildcard like path=elements

            wildpath = WildPath(
                "items.:2.name")  #  takes the names of the first 2 items
            assert wildpath.get_in(agenda) == ["opening", "progress"]

            wildpath = WildPath("items.-1::-1.name")
            assert wildpath.get_in(agenda) == [
                "closing", "progress", "opening"
            ]
        except Exception as e:
            self.fail(e)
コード例 #17
0
 def test_orring_negative_indices(self):
     obj = dict(key=[[0, 1], [2, 3], [4, 5], [6, 7]])
     path = WildPath("key.0|-1.0")
     self.assertEqual(path.get_in(obj), [0, 6])
コード例 #18
0
 def test_index_and(self):
     obj = list(range(8))
     path = WildPath("1&2&3")
     self.assertEqual(path.get_in(obj), [])
コード例 #19
0
 def test_index_not(self):
     obj = list(range(8))
     path = WildPath("!1")
     self.assertEqual(path.get_in(obj), [0, 2, 3, 4, 5, 6, 7])
コード例 #20
0
 def test_index_or(self):
     obj = list(range(8))
     path = WildPath("1|2|3")
     self.assertEqual(path.get_in(obj), [1, 2, 3])
コード例 #21
0
 def test_key_composite(self):
     obj = dict(a=1, b=2, c=3, aa=4, ab=5, ac=6, bb=7, bc=8, cc=9)
     path = WildPath("b*|c*")  # starting with b or c
     self.assertEqual(path.get_in(obj), dict(b=2, c=3, bb=7, bc=8, cc=9))
     path = WildPath("b*&*c")  # starting with b AND ending with c
     self.assertEqual(path.get_in(obj), dict(bc=8))
コード例 #22
0
 def test_key_not(self):
     obj = dict(a=1, b=2, c=3)
     path = WildPath("!a")
     self.assertEqual(path.get_in(obj), dict(b=2, c=3))
コード例 #23
0
 def test_basic_get(self):
     p1 = WildPath("a")
     p2 = WildPath("1")
     self.assertEqual(p1.get_in(dict(a=1)), 1)
     self.assertEqual(p2.get_in([0, 1]), 1)
     self.assertEqual(p1.get_in(Object(a=1)), 1)
コード例 #24
0
 def test_key_or(self):
     obj = dict(a=1, b=2, c=3, aa=4)
     path = WildPath("a|b|c")
     self.assertEqual(path.get_in(obj), dict(a=1, b=2, c=3))