Exemple #1
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__, {})
Exemple #2
0
 def test_longer_del(self):
     s = deepcopy(self.simple)
     p = WildPath("b.0")
     p._del_in(s)
     self.assertEqual(s.b, [3])
     p = WildPath("c.d")
     p._del_in(s)
     self.assertEqual(s.c, {"e": 5})
     p = WildPath("d.e")
     p._del_in(s)
     self.assertEqual(len(s.d.__dict__), 0)
Exemple #3
0
 def test_basic_del(self):
     p1 = WildPath("a")
     p2 = WildPath("1")
     d = dict(a=1)
     l = [0, 1]
     o = Object(a=1)
     p1._del_in(d)
     p2._del_in(l)
     p1._del_in(o)
     self.assertEqual(d, {})
     self.assertEqual(l, [0])
     self.assertEqual(len(o.__dict__), 0)
Exemple #4
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)