def test_string_like_values(self): items = list(WildPath.items(self.agenda)) self.assertTrue( all(len(item[0]) <= 4 for item in items)) # strings are not entered/iterated over characters path = WildPath("meeting") try: path._set_in(agenda, "some other name") # value is not seen as a Sequence except Exception as e: self.fail(e)
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])
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}})
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)
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])
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)