Exemplo n.º 1
0
    def test_iter_getter(self):
        obj = {"a": 1, "b": 2, "c": 3}
        pl = pluckable(obj)

        self.assertEqual(pl["a"].value, 1)
        self.assertEqual(pl["a", "b"].value, [1, 2])
        self.assertEqual(pl[("a", "b")].value, [1, 2])
        self.assertEqual(pl[["a", "b"]].value, [1, 2])

        def gen():
            yield "a"
            yield "b"
        self.assertEqual(pl[gen()].value, [1, 2])
Exemplo n.º 2
0
 def setUp(self):
     self.src = {
         "a": 1,
         "b": range(10),
         "c": {
             "I": {
                 "a": 1,
                 "b": 2
             },
             "II": [{
                 "x": "x1",
                 "y": "y1"
             }, {
                 "x": "x2",
                 "y": "y2",
                 "z": "z2"
             }]
         },
         "users": [{
             "uid": 1,
             "name": {
                 "first": "john",
                 "last": "smith"
             },
         }, {
             "uid": 2,
             "name": {
                 "last": "bonobo"
             }
         }, {
             "uid": 3
         }],
         1: "one",
         2: "two"
     }
     self.obj = pluckable(self.src)
     self.obj2 = pluckable(self.src, skipmissing=False)
Exemplo n.º 3
0
 def test_attrgetter_custom_attr_class_object(self):
     class Obj(object):
         x = 1
     self.assertEqual(pluckable(Obj()).x.value, 1)
Exemplo n.º 4
0
 def test_inplace(self):
     p = pluckable(self.src, inplace=True)
     p2 = p.c.I.b
     self.assertEqual(id(p), id(p2))
     self.assertEqual(p2.obj, 2)
Exemplo n.º 5
0
 def test_str_slice_range(self):
     self.assertEqual(pluckable("str")[0:2].value, ['s', 't'])
Exemplo n.º 6
0
 def test_getitem_str_from_dict_simple(self):
     D = {"x": 1}
     self.assertEqual(pluckable(D)["x"].value, 1)
Exemplo n.º 7
0
 def test_getitem_slice_from_list_simple(self):
     L = list(range(10))
     self.assertEqual(pluckable(L)[2:5].value, L[2:5])
Exemplo n.º 8
0
 def test_iter_noniterable_singular_value(self):
     self.assertEqual(list(iter(pluckable({'x': 1}).x)), [1])
Exemplo n.º 9
0
 def test_dict_numeric_key(self):
     self.assertEqual(pluckable({0: 0})[0].value, 0)
Exemplo n.º 10
0
 def test_skipmissing_dict_slice(self):
     D = {"x": 1}
     self.assertEqual(
         pluckable(D, skipmissing=False)[0:5].invalid.value, [None] * 5)
Exemplo n.º 11
0
 def test_skipmissing_dict_keys2(self):
     D = {"x": 1}
     self.assertEqual(
         pluckable(D, skipmissing=False)["x", "y"].invalid.value,
         [None, None])
Exemplo n.º 12
0
 def test_skipmissing_list_slice(self):
     self.assertEqual(
         pluckable([1, 2], skipmissing=False)[0:4].value,
         [1, 2, None, None])
Exemplo n.º 13
0
 def test_skipmissing_list_indices(self):
     self.assertEqual(
         pluckable([1, 2], skipmissing=False)[0, 2].value, [1, None])
Exemplo n.º 14
0
 def test_getitem_str2_from_dict_simple(self):
     D = {"x": 1, "y": 2, "z": 3}
     self.assertEqual(pluckable(D)["x", "z"].value, [1, 3])
Exemplo n.º 15
0
 def test_attrgetter_custom_attr_shadowed_combination(self):
     class Dct(dict):
         x = 1
         y = 2
     d = Dct(x=2)
     self.assertEqual(pluckable(d)['x', 'y'].value, [2, 2])
Exemplo n.º 16
0
 def test_attrgetter_namedtuple(self):
     Point = namedtuple("Point", "x y z")
     self.assertEqual(pluckable(Point(3, 2, 1)).x.value, 3)
Exemplo n.º 17
0
 def test_dict_slice_singular(self):
     self.assertEqual(pluckable({0: 0, 1: 1, 2: 2})[1:2].value, [1])
Exemplo n.º 18
0
 def test_items_dict(self):
     self.assertEqual(list(pluckable({'x': {'y': 2}}).x.items()), [('y', 2)])
Exemplo n.º 19
0
 def test_dict_slice_range(self):
     self.assertEqual(pluckable({0: 0, 1: 1, 2: 2})[1:3].value, [1, 2])
Exemplo n.º 20
0
 def test_getitem_index_missing_from_list_simple(self):
     L = list(range(10))
     self.assertEqual(pluckable(L)[70].value, None)
Exemplo n.º 21
0
 def test_dict_slice_unbound_bottom(self):
     self.assertEqual(pluckable({0: 0, 1: 1, 2: 2})[:2].value, [0, 1])
Exemplo n.º 22
0
 def test_getitem_slice_rot_list_simple(self):
     L = list(range(10))
     self.assertEqual(pluckable(L)[:5, 5:].value, L[:5] + L[5:])
Exemplo n.º 23
0
 def test_dict_slice_unbound(self):
     self.assertEqual(pluckable({0: 0, 1: 1, 2: 2})[:].value, [0, 1, 2])
Exemplo n.º 24
0
 def test_str_slice_singular(self):
     self.assertEqual(pluckable("str")[0:1].value, ['s'])
Exemplo n.º 25
0
 def test_dict_slice_reduced_to_single_result(self):
     self.assertEqual(pluckable({0: 0, 1: 1, 4: 4})[1::2].value, [1])
Exemplo n.º 26
0
 def test_str_slice_unbound(self):
     self.assertEqual(pluckable("str")[:].value, ['s', 't', 'r'])
Exemplo n.º 27
0
 def test_str_numeric_key(self):
     self.assertEqual(pluckable("str")[0].value, 's')
Exemplo n.º 28
0
 def test_attrgetter_custom_attr_old_style_class(self):
     class Obj:
         x = 1
     self.assertEqual(pluckable(Obj).x.value, 1)
Exemplo n.º 29
0
 def test_str_numeric_negative_key(self):
     self.assertEqual(pluckable("str")[-1].value, 'r')
Exemplo n.º 30
0
 def test_attrgetter_custom_attr_shadowed(self):
     class Dct(dict):
         x = 1
     d = Dct(x=2)
     self.assertEqual(pluckable(d).x.value, 1)
     self.assertEqual(pluckable(d)['x'].value, 2)
Exemplo n.º 31
0
 def test_str_slice_singular(self):
     self.assertEqual(pluckable("str")[0:1].value, ['s'])
Exemplo n.º 32
0
 def test_attrgetter_custom_attr_shadowed_combination_inv_deep(self):
     class Dct(dict):
         x = dict(y=1)
     d = Dct()
     self.assertEqual(pluckable(d)['x'].y.value, 1)
Exemplo n.º 33
0
 def test_str_slice_range(self):
     self.assertEqual(pluckable("str")[0:2].value, ['s', 't'])
Exemplo n.º 34
0
 def test_iter_empty(self):
     self.assertEqual(list(iter(pluckable('x').y)), [])
Exemplo n.º 35
0
 def test_dict_slice_step_from_zero(self):
     self.assertEqual(pluckable({0: 0, 1: 1, 3: 3, 4: 4})[::2].value, [0, 4])
Exemplo n.º 36
0
 def test_iter_dictkeys(self):
     self.assertEqual(list(iter(pluckable({'x': {'y': 2}}).x)), ['y'])
Exemplo n.º 37
0
 def test_dict_slice_step_from_one(self):
     self.assertEqual(pluckable({0: 0, 1: 1, 3: 3, 4: 4})[1::2].value, [1, 3])
Exemplo n.º 38
0
 def test_items_list(self):
     self.assertEqual(list(pluckable(range(10))[0:3].items()), [0, 1, 2])
Exemplo n.º 39
0
 def test_dict_slice_reduced_to_single_result(self):
     self.assertEqual(pluckable({0: 0, 1: 1, 4: 4})[1::2].value, [1])
Exemplo n.º 40
0
 def test_getattr_on_pluckable_list(self):
     users_list = self.obj.value['users']
     self.assertEqual(pluckable(users_list).name.last.value, ["smith", "bonobo"])
Exemplo n.º 41
0
 def test_dict_extract_only_numerical_keys(self):
     self.assertEqual(sorted(pluckable({0: 0, 'a': 'a', 1: 1, 'b': 'b', 3: 3, 4: 4})[:].value), [0, 1, 3, 4])
Exemplo n.º 42
0
 def test_getitem_index2_from_list_simple(self):
     L = list(range(10))
     self.assertEqual(pluckable(L)[5, 7].value, [L[5], L[7]])
Exemplo n.º 43
0
 def test_str_numeric_key(self):
     self.assertEqual(pluckable("str")[0].value, 's')
Exemplo n.º 44
0
 def test_getitem_slice_and_index_from_list_simple(self):
     L = list(range(10))
     self.assertEqual(pluckable(L)[2:5, 8].value, L[2:5] + [L[8]])
Exemplo n.º 45
0
 def test_str_numeric_negative_key(self):
     self.assertEqual(pluckable("str")[-1].value, 'r')
Exemplo n.º 46
0
 def test_getitem_slice_reverse_dup_list_simple(self):
     L = list(range(10))
     self.assertEqual(pluckable(L)[:, ::-1].value, L + list(reversed(L)))
Exemplo n.º 47
0
 def test_getitem_slice_reverse_dup_list_simple(self):
     L = list(range(10))
     self.assertEqual(pluckable(L)[:, ::-1].value, L + list(reversed(L)))