Ejemplo n.º 1
0
 def repr_test(self):
     # if version < 3.6, order is not guaranteed
     assert_in(
         repr(ILookup({'a': 'b', 'c': 'd'})),
         (
             'ILookup<a: b, c: d>',
             'ILookup<c: d, a: b>'
         )
     )
Ejemplo n.º 2
0
 def test_to_lookup2(self):
     natto = {"name": "natto", "department": "Legumeidae"}
     tomato = {"name": "tomato", "department": "Solanaceae"}
     kidney_beans = {"name": "Kidney beans", "department": "Legumeidae"}
     assert_equal(
         Linq([natto, tomato,
               kidney_beans]).to_lookup(key_f=lambda f: f["name"]),
         ILookup({
             "natto": Linq([natto]),
             "tomato": Linq([tomato]),
             "Kidney beans": Linq([kidney_beans])
         }))
Ejemplo n.º 3
0
 def test_to_lookup3(self):
     assert_equal(
         Linq(range(10)).to_lookup(value_f=lambda n: n % 3),
         ILookup({
             0: Linq([0]),
             1: Linq([1]),
             2: Linq([2]),
             3: Linq([0]),
             4: Linq([1]),
             5: Linq([2]),
             6: Linq([0]),
             7: Linq([1]),
             8: Linq([2]),
             9: Linq([0])
         }))
Ejemplo n.º 4
0
 def test_to_lookup(self):
     foods = Linq([{
         "name": "natto",
         "department": "Legumeidae"
     }, {
         "name": "tomato",
         "department": "Solanaceae"
     }, {
         "name": "Kidney beans",
         "department": "Legumeidae"
     }])
     assert_equal(
         foods.to_lookup(key_f=lambda f: f["department"],
                         value_f=lambda f: f["name"]),
         ILookup({
             "Legumeidae": Linq(["natto", "Kidney beans"]),
             "Solanaceae": Linq(["tomato"])
         }))
Ejemplo n.º 5
0
    def to_lookup(self, key_f=None, value_f=None):
        """
        Return the dictionary from ``key_f(item)`` to Linq object of some of
        ``key_f(item)``.

        >>> natto = {"name": "natto", "department": "Legumeidae"}
        >>> tomato = {"name": "tomato", "department": "Solanaceae"}
        >>> kidney_beans = {"name": "Kidney beans", "department": "Legumeidae"}
        >>> Linq([natto, tomato, kidney_beans]).to_lookup(
        ...     key_f=lambda f: f["department"],
        ...     value_f=lambda f: f["name"]
        ... )
        ILookup<
            Legumeidae: Linq<natto, Kidney beans>,
            Solanaceae: Linq<tomato>
        >
        """
        from ilinq.ilookup import ILookup
        d = defaultdict(lambda: Linq([]))
        for item in self:
            key = _act(key_f, item)
            value = _act(value_f, item)
            d[key].append(value)
        return ILookup(d)
Ejemplo n.º 6
0
 def init_test(self):
     ILookup()
Ejemplo n.º 7
0
 def to_linq_test(self):
     assert_equal(
         ILookup({1: 2, 2: 3, 3: 6}).to_linq(),
         Linq([(1, 2), (2, 3), (3, 6)]))
Ejemplo n.º 8
0
 def init_test2(self):
     ILookup([[1, 2], [3, 4]])