Esempio n. 1
0
    def test_select_function3(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        le = [{"nom": "j", "age": 10, "gender": "M"},
              {"nom": "jean", "age": 40, "gender": "M"},
              {"nom": "jeanne", "age": 2, "gender": "F"}]
        tbl = IterRow(None, le)

        iter = tbl.groupby(tbl.gender, tbl.nom, nbs=tbl.age.len())
        res = list(iter)

        exp = [{'nbs': 1, 'gender': 'F', 'nom': 'jeanne'},
               {'nbs': 1, 'gender': 'M', 'nom': 'j'},
               {'nbs': 1, 'gender': 'M', 'nom': 'jean'}]

        if res != exp:
            raise ValueError(str(res))

        try:
            tbl.groupby(
                tbl.gender,
                len_nom=tbl.nom.len() * 2,
                avg_age=tbl.age.avg())
            raise Exception("unexpected, it should raise an exception")
        except NotAllowedOperation:
            pass
Esempio n. 2
0
    def test_select_operators(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [("nom", 10), ("jean", 40), ("jeanne", 2)]
        schema = [("nom", str), ("age", int)]
        tbl = IterRow(schema, lr)

        iter = tbl.select(tbl.nom, age0=tbl.age,
                          agem=tbl.age * 0.5,
                          agea=tbl.age + 0.5,
                          ages=tbl.age - 0.5,
                          agep=tbl.age ** 0.5, agedd=tbl.age // 3, agemod=tbl.age % 3
                          )

        res = list(iter)

        exp = [{'age0': 10, 'ages': 9.5, 'agemod': 1, 'agem': 5.0, 'agep': 3.1622776601683795, 'agea': 10.5, 'agedd': 3, 'nom': 'nom'},
               {'age0': 40,
                'ages': 39.5,
                'agemod': 1,
                'agem': 20.0,
                'agep': 6.324555320336759,
                'agea': 40.5,
                'agedd': 13,
                'nom': 'jean'},
               {'age0': 2, 'ages': 1.5, 'agemod': 2, 'agem': 1.0, 'agep': 1.4142135623730951, 'agea': 2.5, 'agedd': 0, 'nom': 'jeanne'}]

        if res != exp:
            raise ValueError(str(res))
Esempio n. 3
0
 def test_select_simple(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     lr = [("jake", 10), ("jean", 40)]
     schema = [("nom", str), ("age", int)]
     tbl = IterRow(schema, lr)
     lr = list(tbl.select(tbl.nom, tbl.age))
     for _ in lr:
         fLOG("+", _)
     if lr != [{'age': 10, 'nom': 'jake'}, {'age': 40, 'nom': 'jean'}]:
         raise Exception(str(lr))
Esempio n. 4
0
 def test_iter_simple_dict2(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     l0 = [{"nom": "jean", "age": 10},
           {"nom": "j", "age": 20}]
     tbl = IterRow(None, l0)
     tbl2 = tbl.select(tbl.nom)
     lr = list(tbl2)
     assert len(lr) == 2
     if lr != [{"nom": "jean"}, {"nom": "j"}]:
         raise ValueError(str(lr))
Esempio n. 5
0
    def test_where(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [("nom", 10), ("jean", 40), ("jeanne", 2)]
        schema = [("nom", str), ("age", int)]
        tbl = IterRow(schema, lr)

        iter = tbl.where(tbl.age == 40)
        res = list(iter)

        exp = [{'nom': "jean", 'age': 40}]

        if res != exp:
            raise ValueError(str(res))
Esempio n. 6
0
    def test_select_mismatch(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [("nom", 10), ("jean", 40), ("jeanne", 2)]
        schema = [("nom", str), ("age", int)]
        tbl = IterRow(schema, lr)

        iter = tbl.select(tbl.nom, age2=tbl.age, age3=tbl.age * 0.5)
        try:
            iter.select(iter.nom, tbl.age)
            raise TypeError(
                "we should not be able to reach this code due to confusion between iter and tbl")
        except IterException as e:
            fLOG(e)
            assert "mismatch" in str(e)
Esempio n. 7
0
    def test_select_orderby(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        le = [{"nom": "j", "age": 10, "gender": "M"},
              {"nom": "jean", "age": 40, "gender": "M"},
              {"nom": "jeanne", "age": 2, "gender": "F"}]
        tbl = IterRow(None, le)

        iter = tbl.orderby(tbl.nom, tbl.age, ascending=False)
        res = list(iter)

        exp = [{'gender': 'F', 'nom': 'jeanne', 'age': 2},
               {'gender': 'M', 'nom': 'jean', 'age': 40},
               {'gender': 'M', 'nom': 'j', 'age': 10}]

        if res != exp:
            raise ValueError(str(res))
Esempio n. 8
0
    def test_select_bracket(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [("nom", 10), ("jean", 40), ("jeanne", 2)]
        schema = [("nom", str), ("age", int)]
        tbl = IterRow(schema, lr)

        iter = tbl.select(tbl.nom, formula=tbl.age + (tbl.age + 2) / 3)

        res = list(iter)

        exp = [{'formula': 14.0, 'nom': 'nom'},
               {'formula': 54.0, 'nom': 'jean'},
               {'formula': 3.333333333333333, 'nom': 'jeanne'}]

        if res != exp:
            raise ValueError(str(res))
Esempio n. 9
0
    def test_select_function(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [("nom", 10), ("jean", 40), ("jeanne", 2)]
        schema = [("nom", str), ("age", int)]
        tbl = IterRow(schema, lr)

        def myf(x, y):
            return x * 2.5 + y
        iter = tbl.select(tbl.nom, age0=CFT(myf, tbl.age, tbl.age))
        res = list(iter)

        exp = [{'nom': 'nom', 'age0': 35.0},
               {'nom': 'jean', 'age0': 140.0},
               {'nom': 'jeanne', 'age0': 7.0}]

        if res != exp:
            raise ValueError(str(res))
Esempio n. 10
0
    def test_select_function2(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [{"nom": "j", "age": 10},
              {"nom": "jean", "age": 40},
              {"nom": "jeanne", "age": 2}]
        tbl = IterRow(None, lr)

        def myf(x, y):
            return x * 2.5 + y
        iter = tbl.select(tbl.nom, age0=CFT(myf, tbl.age, tbl.age))
        res = list(iter)

        exp = [{'nom': 'j', 'age0': 35.0},
               {'nom': 'jean', 'age0': 140.0},
               {'nom': 'jeanne', 'age0': 7.0}]

        if res != exp:
            raise ValueError(str(res))
Esempio n. 11
0
    def test_where_or(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [("nom", 10), ("jean", 40), ("jeanne", 2)]
        schema = [("nom", str), ("age", int)]
        tbl = IterRow(schema, lr)
        iter = tbl.where(
            (tbl.age == 2).Or(
                tbl.age == 40),
            append_condition=True)
        res = list(iter)
        exp = [{'nom': 'jean', 'age': 40, '__unk__': True},
               {'nom': "jeanne", 'age': 2, '__unk__': True}, ]
        if res != exp:
            raise ValueError(str(res) + "\n\n" + iter.print_schema())

        iter = tbl.where((tbl.age == 10).Not())
        res = list(iter)
        assert len(res) == 2
Esempio n. 12
0
    def test_select_simple2(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [("nom", 10), ("jean", 40)]
        schema = [("nom", str), ("age", int)]
        tbl = IterRow(schema, lr)

        iter = tbl.select(tbl.nom, age2=tbl.age * 2, age3=tbl.age * 3)

        lr = list(iter)
        assert len(lr) == 2
        if lr != [{'nom': 'nom', 'age2': 20, 'age3': 30},
                  {'nom': 'jean', 'age2': 80, 'age3': 120}]:
            raise Exception(str(lr))

        iter = tbl.select(tbl.nom, age2=tbl.age * 2)
        sch = iter.Schema
        assert sch[0].Name == "nom"
        assert sch[1].Name == "age2"
Esempio n. 13
0
    def test_select_union_notin(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        le = [{"nom": "j", "age": 10, "gender": "M"},
              {"nom": "jean", "age": 40, "gender": "M"},
              {"nom": "jeanne", "age": 2, "gender": "F"}]
        tbl = IterRow(None, le)

        le = [{"nom": "j", "newage": 10, "gender": "M"},
              {"nom": "jean", "newage": 40, "gender": "M"},
              {"nom": "jeanne", "newage": 2, "gender": "F"}]
        tbl2 = IterRow(None, le)

        iter = tbl.unionall(tbl2, merge_schema=True)
        res = list(iter)

        exp = [{'gender': 'M', 'nom': 'j', 'newage': 'NA()', 'age': 10},
               {'gender': 'M', 'nom': 'jean', 'newage': 'NA()', 'age': 40},
               {'gender': 'F', 'nom': 'jeanne', 'newage': 'NA()', 'age': 2},
               {'gender': 'F', 'nom': 'jeanne', 'newage': 10, 'age': 'NA()'},
               {'gender': 'F', 'nom': 'jeanne', 'newage': 40, 'age': 'NA()'},
               {'gender': 'F', 'nom': 'jeanne', 'newage': 2, 'age': 'NA()'}]

        def repl(d):
            return {k: repla(v) for k, v in d.items()}

        def repla(v):
            if isinstance(v, NA):
                return 'NA()'
            else:
                return v
        res = [repl(r) for r in res]

        if res != exp:
            raise ValueError(str(res))
Esempio n. 14
0
    def test_select_union(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [{"nom": "j", "age": 10, "gender": "M"},
              {"nom": "jean", "age": 40, "gender": "M"},
              {"nom": "jeanne", "age": 2, "gender": "F"}]
        tbl = IterRow(None, lr)

        iter = tbl.unionall(tbl)
        res = list(iter)

        exp = [{"nom": "j", "age": 10, "gender": "M"},
               {"nom": "jean", "age": 40, "gender": "M"},
               {"nom": "jeanne", "age": 2, "gender": "F"},
               {"nom": "j", "age": 10, "gender": "M"},
               {"nom": "jean", "age": 40, "gender": "M"},
               {"nom": "jeanne", "age": 2, "gender": "F"},
               ]

        if res != exp:
            raise ValueError(str(res))
Esempio n. 15
0
    def test_select_simple_square(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        lr = [("nom", 10), ("jean", 40), ("jeanne", 2)]
        schema = [("nom", str), ("age", int)]
        tbl = IterRow(schema, lr)

        iter = tbl.select(tbl.nom, age2=tbl.age, age3=tbl.age * 0.5)
        iter2 = iter.select(iter.nom, age4=iter.age2 * iter.age3)

        lr = list(iter2)
        assert len(lr) == 3
        fLOG(";".join([str(_) for _ in iter2.Schema]))
        fLOG(lr)
        if lr != [{'age4': 50.0, 'nom': 'nom'}, {
                'age4': 800.0, 'nom': 'jean'}, {'age4': 2.0, 'nom': 'jeanne'}]:
            raise Exception(str(lr))

        sch = iter2.Schema
        assert sch[0].Name == "nom"
        assert sch[1].Name == "age4"