Exemplo n.º 1
0
    def test_save_stock(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        cache = os.path.abspath(os.path.split(__file__)[0])
        cache = os.path.join(cache, "temp_cache_file")
        name = os.path.join(cache, "BNP.PA.2000-01-03.2014-01-15.txt")
        if os.path.exists(name):
            os.remove(name)

        stock = StockPrices(
            "BNP.PA",
            folder=cache,
            end=datetime.datetime(
                2014,
                1,
                15))

        file = os.path.join(cache, "save.txt")
        if os.path.exists(file):
            os.remove(file)
        stock.to_csv(file)
        assert os.path.exists(file)

        stock2 = StockPrices(file, sep="\t")
        assert stock.dataframe.shape == stock2.dataframe.shape
        df = stock2.dataframe
        file = os.path.join(cache, "out_excel.xlsx")
        if os.path.exists(file):
            os.remove(file)
        df.to_excel(file)
        assert os.path.exists(file)
    def test_excel(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        df1 = pandas.DataFrame([{
            "name": "xavier",
            "school": "ENSAE"
        }, {
            "name": "antoine",
            "school": "ENSAE"
        }])

        df2 = pandas.DataFrame([{
            "name": "xavier",
            "company": "Microsoft"
        }, {
            "name": "antoine",
            "company": "Alephd"
        }])

        ef = os.path.abspath(
            os.path.join(os.path.dirname(__file__), "out_example.xlsx"))
        if os.path.exists(ef):
            os.remove(ef)
        dfs2excel({"ecole": df1, "boite": df2}, ef)
        assert os.path.exists(ef)
def nb_open(filename, profile="default", fLOG=fLOG):
    """
    open a notebook with an existing server,
    if no server can be found, it starts a new one
    (and the function runs until the server is closed)

    @param      filename        notebook
    @param      profile         profile to use
    @param      fLOG            logging function
    @return                     a running server or None if not found
    """
    filename = os.path.abspath(filename)
    server_inf = find_best_server(filename, profile)
    if server_inf is not None:
        fLOG("Using existing server at", server_inf["notebook_dir"])
        path = os.path.relpath(filename, start=server_inf["notebook_dir"])
        url = url_path_join(server_inf["url"], "notebooks", path)
        webbrowser.open(url, new=2)
        return server_inf
    else:
        fLOG("Starting new server")
        home_dir = os.path.dirname(filename)
        server = notebookapp.launch_new_instance(
            file_to_run=os.path.abspath(filename),
            notebook_dir=home_dir,
            open_browser=True,
            # Avoid it seeing our own argv
            argv=[],
        )
        return server
Exemplo n.º 4
0
    def test_notebook_runner(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        temp = get_temp_folder(__file__, "temp_notebook1")
        nbfile = os.path.join(
            temp,
            "..",
            "..",
            "..",
            "_doc",
            "notebooks",
            "td2a",
            "td2a_correction_session_1.ipynb")
        nbfile = os.path.normpath(nbfile)
        assert os.path.exists(nbfile)
        addpath = [
            os.path.dirname(
                pyquickhelper.__file__), os.path.dirname(
                pyensae.__file__)]
        addpath = [os.path.normpath(os.path.join(_, "..")) for _ in addpath]

        outfile = os.path.join(temp, "out_" + os.path.split(nbfile)[-1])
        assert not os.path.exists(outfile)
        stat, out = run_notebook(nbfile, working_dir=temp, outfilename=outfile,
                                 additional_path=addpath,
                                 valid=lambda code: '%system' not in code)
        fLOG(out)
        assert os.path.exists(outfile)
Exemplo n.º 5
0
    def test_covariance(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        cache = os.path.abspath(os.path.split(__file__)[0])
        cache = os.path.join(cache, "temp_cache_cov")
        stocks = [StockPrices("BNP.PA", folder=cache),
                  StockPrices("CA.PA", folder=cache),
                  StockPrices("SAF.PA", folder=cache),
                  ]

        dates = StockPrices.available_dates(stocks)
        ok = dates[dates["missing"] == 0]
        stocks = [v.keep_dates(ok) for v in stocks]

        cov = StockPrices.covariance(stocks)
        assert len(cov) == 3

        cor = StockPrices.covariance(stocks, cov=False)
        assert len(cor) == 3
        assert cor.ix["BNP.PA", "BNP.PA"] == 1
        assert cor.ix[2, 2] == 1

        ret, mat = StockPrices.covariance(stocks, cov=False, ret=True)
        assert len(ret) == 3
Exemplo n.º 6
0
    def test_graph3(self):
        """
        This test is failing with Python 3.4 if many pictures are drawn.
        """
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        fix_tkinter_issues_virtualenv()

        from matplotlib import pyplot as plt

        cache = os.path.abspath(os.path.split(__file__)[0])
        cache = os.path.join(cache, "temp_cache3")
        stock = StockPrices("BNP.PA", folder=cache)
        stock2 = StockPrices("CA.PA", folder=cache)

        fig, ax = plt.subplots(figsize=(16, 8))
        ax = stock.plot(ax=ax)
        ax = stock2.plot(ax=ax, axis=2)
        img = os.path.abspath(
            os.path.join(
                os.path.split(__file__)[0],
                "temp_image3.png"))
        if os.path.exists(img):
            os.remove(img)
        fig.savefig(img)
        plt.close('all')
        assert os.path.exists(img)
Exemplo n.º 7
0
    def test_import_sql(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        file = os.path.join(
            os.path.abspath(
                os.path.split(__file__)[0]),
            "data",
            "ACA.PA.txt")
        dbf = os.path.join(
            os.path.abspath(
                os.path.split(__file__)[0]),
            "temp_database_inti.db3")
        if os.path.exists(dbf):
            os.remove(dbf)
        assert not os.path.exists(dbf)

        face = InterfaceSQL.create(dbf)
        face.connect()

        face.import_flat_file(file, "ACAPA2")
        assert face.CC.ACAPA2._ == "ACAPA2"

        face.close()
    def test_jenkins_local(self):
        fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")

        st = rst_table_modules()
        fLOG("\n" + st)
        assert st is not None
        assert len(st) > 0
    def test_notebook_runner_2a_long(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip travis and R
            warnings.warn(
                "travis, unable to test TestNotebookRunner2a_long.test_notebook_runner_2a_long")
            return

        if "R_HOME" not in os.environ or not os.path.exists(os.environ["R_HOME"]):
            paths = [r"C:\Program Files\R\R-3.2.2",
                     r"C:\Program Files\R\R-3.1.2"]
            for path in paths:
                if os.path.exists(path):
                    os.environ["R_HOME"] = path
                    break

        temp = get_temp_folder(__file__, "temp_notebook2a_long_")
        keepnote = ls_notebooks("2a")
        assert len(keepnote) > 0
        res = execute_notebooks(temp, keepnote,
                                lambda i, n: "python_r" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 10
0
    def test_graph(self) :
        """
        This test is failing with Python 3.4 if many pictures are drawn.
        """
        fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")

        cache = os.path.abspath(os.path.split(__file__)[0])
        cache = os.path.join(cache, "temp_cache2")
        stocks = [ StockPrices ("BNP.PA", folder = cache),
                    StockPrices ("CA.PA", folder = cache),
                    StockPrices ("SAF.PA", folder = cache),
                    ]
                    
        if True:
            fig, ax, plt = StockPrices.draw(stocks, figsize=(16,8), field = ["Adj Close", "Close"])
            img = os.path.abspath(os.path.join(os.path.split(__file__)[0],"temp_image.png"))
            if os.path.exists(img): os.remove(img)
            fig.savefig(img)
            assert os.path.exists(img)

        if True and sys.version_info < (3,4) :
            fig, ax, plt = StockPrices.draw(stocks, begin="2010-01-01")
            img = os.path.abspath(os.path.join(os.path.split(__file__)[0],"temp_image2.png"))
            if os.path.exists(img): os .remove(img)
            fig.savefig(img)
            assert os.path.exists(img)
            
        if True and sys.version_info < (3,4):
            fig, ax, plt = StockPrices.draw(stocks[:1], begin="2010-01-01")
            img = os.path.abspath(os.path.join(os.path.split(__file__)[0],"temp_image3.png"))
            if os.path.exists(img): os .remove(img)
            fig.savefig(img)
            assert os.path.exists(img)     
Exemplo n.º 11
0
    def test_charp_graph_networkx(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        code = """
        namespace hello
        {
            public static class world
            {
                public static double function(double x, doubly y)
                {
                    return x+y ;
                }
            }
        }
        """

        clparser, cllexer = get_parser_lexer("C#")
        parser = parse_code(code, clparser, cllexer)
        tree = parser.parse()
        st = get_tree_graph(tree, parser)

        if "travis" in sys.executable:
            # no networkx
            return

        st.draw()

        import matplotlib.pyplot as plt
        if __name__ == "__main__":
            plt.show()

        plt.close('all')
Exemplo n.º 12
0
    def test_notebook_runner_flat2db3(self):
        fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")
        notebook = os.path.split(__file__)[-1].replace(".ipynb", "").replace(".py", "")[5:]
        temp = get_temp_folder(__file__, "temp_" + notebook)
        nbfile = os.path.join(temp, "..", "..", "..", "_doc", "notebooks", "%s.ipynb" % notebook)
        if not os.path.exists(nbfile):
            raise FileNotFoundError(nbfile)
        addpath = [
            os.path.normpath(os.path.join(temp, "..", "..", "..", "src")),
            os.path.normpath(os.path.join(temp, "..", "..", "..", "..", "pyquickhelper", "src")),
            os.path.normpath(os.path.join(temp, "..", "..", "..", "..", "pymyinstall", "src")),
        ]
        outfile = os.path.join(temp, "out_notebook.ipynb")
        assert not os.path.exists(outfile)
        valid = lambda code: 'run_cmd("SQLiteSpy.exe velib_vanves.db3")' not in code

        if "travis" in sys.executable:
            return

        kernel_name = None if "travis" in sys.executable else install_python_kernel_for_unittest("pyensae")

        out = run_notebook(
            nbfile,
            working_dir=temp,
            outfilename=outfile,
            additional_path=addpath,
            valid=valid,
            fLOG=fLOG,
            kernel_name=kernel_name,
        )
        fLOG(out)
        assert os.path.exists(outfile)
    def test_notebook_runner_correction_1_7(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        temp = get_temp_folder(__file__, "temp_notebook1a_correction_1_7")
        keepnote = ls_notebooks("td1a")
        assert len(keepnote) > 0

        cp = os.path.join(temp, "..", "data", "seance4_excel.txt")
        shutil.copy(cp, temp)
        cp = os.path.join(temp, "..", "data", "seance4_excel.xlsx")
        shutil.copy(cp, temp)

        res = execute_notebooks(temp, keepnote,
                                lambda i, n: "_12" not in n
                                and "session6." not in n
                                and "session8." not in n
                                and "session9." not in n
                                and "session_10." not in n
                                and "session_11." not in n
                                and "correction" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 14
0
 def test_voice(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     if sys.platform.startswith("win"):
         from src.ensae_teaching_cs.pythonnet import vocal_synthesis
         vocal_synthesis("test unitaire")
    def test_fonction(self):
        fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")

        assert recherche([2, 3, 45], 3) == 1
        assert recherche([2, 3, 45], 4) == -1
        assert minindex([2, 3, 45, -1, 5]) == (-1, 3)
        li = range(0, 100, 2)
        assert recherche_dichotomique(li, 48) == 24
        assert recherche_dichotomique(li, 49) == -1
        s = "case11;case12;case13|case21;case22;case23"
        mat = text2mat(s, "|", ";")
        t = mat2text(mat, "|", ";")
        assert t == s
        tab = ["zero", "un", "deux"]
        r = triindex(tab)
        assert r == [("deux", 2), ("un", 1), ("zero", 0)]
        li = ["un", "deux", "un", "trois"]
        r = compte(li)
        assert r == {"trois": 1, "deux": 1, "un": 2}
        mat = [[0, 1, 2], [3, 4, 5]]
        r = mat2vect(mat)
        assert r == [0, 1, 2, 3, 4, 5]
        m = vect2mat(r, 3)
        assert m == mat
        x2 = integrale(lambda x: x, 0, 2, 1000)
        assert x2 == 2
Exemplo n.º 16
0
def main():
    try:
        import pyquickhelper
        import pyensae
    except ImportError:
        sys.path.append(
            os.path.normpath(
                os.path.abspath(
                    os.path.join(
                        os.path.split(__file__)[0],
                        "..",
                        "..",
                        "pyquickhelper",
                        "src"))))
        import pyquickhelper
        sys.path.append(
            os.path.normpath(
                os.path.abspath(
                    os.path.join(
                        os.path.split(__file__)[0],
                        "..",
                        "..",
                        "pyensae",
                        "src"))))
        import pyquickhelper

    from pyquickhelper import fLOG, run_cmd, main_wrapper_tests
    fLOG(OutputPrint=True)
    main_wrapper_tests(__file__)
Exemplo n.º 17
0
def nb_open(filename, profile='default', fLOG=fLOG):
    """
    open a notebook with an existing server,
    if no server can be found, it starts a new one
    (and the function runs until the server is closed)

    @param      filename        notebook
    @param      profile         profile to use
    @param      fLOG            logging function
    @return                     a running server or None if not found
    """
    filename = os.path.abspath(filename)
    server_inf = find_best_server(filename, profile)
    if server_inf is not None:
        fLOG("Using existing server at", server_inf['notebook_dir'])
        path = os.path.relpath(filename, start=server_inf['notebook_dir'])
        url = url_path_join(server_inf['url'], 'notebooks', path)
        webbrowser.open(url, new=2)
        return server_inf
    else:
        fLOG("Starting new server")
        home_dir = os.path.dirname(filename)
        server = notebookapp.launch_new_instance(
            file_to_run=os.path.abspath(filename),
            notebook_dir=home_dir,
            open_browser=True,
            # Avoid it seeing our own argv
            argv=[],
        )
        return server
Exemplo n.º 18
0
    def test_head2(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        fp = os.path.join(
            os.path.dirname(
                os.path.abspath(__file__)),
            "data",
            "Exportutf8.txt")
        mg = MagicFile()
        fLOG("--", fp)
        res = mg.head("{0} -n 3".format(fp))
        fLOG("*****", res)
        res = mg.head("{0} -n=3".format(fp))
        fLOG("*****", res)
        res = mg.head("{0}".format(fp))
        fLOG("*****", res)
        assert "9.0" in res.data
        res = mg.head("{0} --n 3 -e utf8".format(fp))

        try:
            res = mg.head("Exportutf8.txt")
        except FileNotFoundError:
            pass
Exemplo n.º 19
0
    def test_htmlhelp(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        mg = MagicFile()
        mg.add_context(
            {"file_tail": file_tail, "Database": Database, "text": 3})
        cmd = "-np -f rawhtml file_tail"
        res = mg.hhelp(cmd)
        assert "<p>extracts the first nbline of a file " in res
        res = mg.hhelp("-np -f rst file_tail")
        assert ":param      threshold:" in res
        res = mg.hhelp("-np -f rawhtml Database")
        assert "SQL file which can be empty or not," in res
        doc = docstring2html(Database.__init__, format="rawhtml")
        assert "it can also contain several files separated by" in doc
        fLOG("----------")
        res = mg.hhelp("-np -f rst Database.__init__")
        assert "it can also contain several files separated by" in res
        res = mg.hhelp("Database.__init__")
        assert res is not None
        res = mg.hhelp("-np -f text Database.__init__")
        assert "it can also contain several files separated by" in res
        assert "@param" in res
Exemplo n.º 20
0
    def test_import_flatflitand_copy(self) :
        fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
        
        file = os.path.join(os.path.abspath(os.path.split(__file__)[0]), "data", "ACA.PA.txt")
        dbf  = os.path.join(os.path.abspath(os.path.split(__file__)[0]), "temp_database_copy.db3")
        if os.path.exists(dbf) : os.remove(dbf)

        dbf2  = os.path.join(os.path.abspath(os.path.split(__file__)[0]), "out_copy.db3")
        if os.path.exists(dbf2) : os.remove(dbf2)
        
        import_flatfile_into_database(dbf, file, fLOG = fLOG)
        assert os.path.exists(dbf)
        
        db = Database(dbf, LOG = fLOG)
        dbm = Database(dbf2, LOG = fLOG)
        db.copy_to(dbm)

        db.connect()
        dbm.connect()
        tbls = dbm.get_table_list()
        if len(tbls)!=1 : raise Exception("expects one table not %d" % len(tbls))
        view = db.execute_view("SELECT * FROM ACAPA")
        viewm = dbm.execute_view("SELECT * FROM ACAPA")
        db.close()
        dbm.close()
        assert len(view) == len(viewm)
        
        dbm2 = Database(":memory:", LOG = fLOG)
        db.copy_to(dbm2)
        dbm2.connect()
        tbls = dbm2.get_table_list()
        if len(tbls)!=1 : raise Exception("expects one table not %d" % len(tbls))
        viewm2 = dbm2.execute_view("SELECT * FROM ACAPA")
        dbm2.close()
        assert len(view) == len(viewm2)
Exemplo n.º 21
0
 def test_fonction1(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     a = racine_carree(2)
     assert a == 2 ** 0.5
Exemplo n.º 22
0
    def test_notebook_runner_2a_long(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip travis and R
            warnings.warn(
                "travis, unable to test TestNotebookRunner2a_long.test_notebook_runner_2a_long"
            )
            return

        if "R_HOME" not in os.environ or not os.path.exists(
                os.environ["R_HOME"]):
            paths = [
                r"C:\Program Files\R\R-3.2.2", r"C:\Program Files\R\R-3.1.2"
            ]
            for path in paths:
                if os.path.exists(path):
                    os.environ["R_HOME"] = path
                    break

        temp = get_temp_folder(__file__, "temp_notebook2a_long_")
        keepnote = ls_notebooks("2a")
        assert len(keepnote) > 0
        res = execute_notebooks(temp,
                                keepnote,
                                lambda i, n: "python_r" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 23
0
    def test_notebook_runner(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        # this notebook describes how to distribute the work with multiple processors
        # it requires to start multiple clusters first (a command line)
        # and to stop them afterwards
        # it still needs to be implemented
        # we skip !
        warnings.warn(
            "TODO: implement a unit test testing the distribution on multiple processors"
        )
        return

        temp = get_temp_folder(__file__, "temp_notebook2a_2_enonce_2D")
        keepnote = ls_notebooks("td2a")
        assert len(keepnote) > 0
        res = execute_notebooks(
            temp,
            keepnote,
            lambda i, n: "_2" in n and "enonce" in n and "_2D" in n,
            fLOG=fLOG,
            clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 24
0
    def test_flake8(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        thi = os.path.abspath(os.path.dirname(__file__))
        src = os.path.normpath(os.path.join(thi, "..", "..", "src"))
        exe = os.path.dirname(sys.executable)
        scr = os.path.join(exe, "Scripts")
        fla = os.path.join(scr, "flake8")
        cmd = fla + " " + src
        out, err = run_cmd(cmd, fLOG=fLOG, wait=True)

        lines = out.split("\n")
        lines = [_ for _ in lines if "E501" not in _ and "__init__.py" not in _ and "E265" not in _
                 and "W291" not in _ and "W293" not in _ and "pandas_helper" not in _]
        lines = [_ for _ in lines if len(_) > 1]
        if __name__ == "__main__":
            for l in lines:
                spl = l.split(":")
                if len(spl[0]) == 1:
                    spl[1] = ":".join(spl[0:2])
                    del spl[0]
                    print(
                        '  File "{0}", line {1}, {2}'.format(spl[0], spl[1], spl[-1]))
        if len(lines) > 1:
            raise Exception(
                "{0} lines\n{1}".format(len(lines), "\n".join(lines)))
Exemplo n.º 25
0
    def test_groupby_sort_head(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        l = [dict(k1="a", k2="b", v=4, i=1),
             dict(k1="a", k2="b", v=5, i=1),
             dict(k1="a", k2="b", v=4, i=2),
             dict(k1="b", k2="b", v=1, i=2),
             dict(k1="b", k2="b", v=1, i=3),
             ]

        exp = [dict(k1="a", k2="b", v=4, i=1),
               dict(k1="b", k2="b", v=1, i=2),
               ]

        df = pandas.DataFrame(l)
        exp = pandas.DataFrame(exp)

        res = groupby_topn(df, by_keys=["k1", "k2"],
                           sort_keys=["v", "i"], as_index=False)
        b = df_equal(exp, res)
        if not b:
            raise Exception(
                "dataframe not equal\nRES:\n{0}\nEXP\n{1}".format(str(res), str(exp)))
Exemplo n.º 26
0
    def test_graph_graphviz(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        code = """
        namespace hello
        {
            public static class world
            {
                public static double function(double x, doubly y)
                {
                    return x+y ;
                }
            }
        }
        """

        clparser, cllexer = get_parser_lexer("C#")
        parser = parse_code(code, clparser, cllexer)
        tree = parser.parse()
        st = get_tree_graph(tree, parser)
        dot = st.to_dot()
        # fLOG(dot)
        assert len(dot) > 0

        if "travis" not in sys.executable:
            temp = get_temp_folder(__file__, "temp_dot_grammar")
            name = os.path.join(temp, "graph.dot")
            with open(name, "w") as f:
                f.write(dot)
            img = os.path.join(temp, "graph.png")
            run_dot(name, img)
            assert os.path.exists(img)
    def test_data_velib_simulation(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__",
            LogFile="temp_hal_log2.txt")
        fold = os.path.abspath(os.path.split(__file__)[0])
        data = os.path.join(fold, "data")

        for speed in (10, 15):
            for bike in (1, 2, 3, 5, 10):
                df = DataVelibCollect.to_df(data)
                dfp, dfs = DataVelibCollect.simulate(
                    df, bike, speed, fLOG=fLOG)

                dfp.to_csv(
                    "out_simul_bike_nb{0}_sp{1}_path.txt".format(
                        bike,
                        speed),
                    sep="\t",
                    index=False)
                dfs.to_csv(
                    "out_simul_bike_nb{0}_sp{1}_data.txt".format(
                        bike,
                        speed),
                    sep="\t",
                    index=False)
                if __name__ != "__main__":
                    return
 def test_notebook_runner(self):
     fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook2a_1")
     keepnote = ls_notebooks("td2a")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote, lambda i, n: "_1" in n, fLOG=fLOG)
     unittest_raise_exception_notebook(res, fLOG)
    def test_fonction(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        assert recherche([2, 3, 45], 3) == 1
        assert recherche([2, 3, 45], 4) == -1
        assert minindex([2, 3, 45, -1, 5]) == (-1, 3)
        li = range(0, 100, 2)
        assert recherche_dichotomique(li, 48) == 24
        assert recherche_dichotomique(li, 49) == -1
        s = "case11;case12;case13|case21;case22;case23"
        mat = text2mat(s, "|", ";")
        t = mat2text(mat, "|", ";")
        assert t == s
        tab = ["zero", "un", "deux"]
        r = triindex(tab)
        assert r == [('deux', 2), ('un', 1), ('zero', 0)]
        li = ["un", "deux", "un", "trois"]
        r = compte(li)
        assert r == {'trois': 1, 'deux': 1, 'un': 2}
        mat = [[0, 1, 2], [3, 4, 5]]
        r = mat2vect(mat)
        assert r == [0, 1, 2, 3, 4, 5]
        m = vect2mat(r, 3)
        assert m == mat
        x2 = integrale(lambda x: x, 0, 2, 1000)
        assert x2 == 2
    def test_data_velib_animation(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__",
            LogFile="temp_hal_log2.txt")
        fold = os.path.abspath(os.path.split(__file__)[0])
        data = os.path.join(fold, "data")

        if "travis" in sys.executable:
            return

        try:
            from JSAnimation import IPython_display
        except ImportError:
            import pymyinstall
            pymyinstall.ModuleInstall(
                "JSAnimation",
                "github",
                "jakevdp").install(
                temp_folder="c:\\temp")

        df = DataVelibCollect.to_df(data)
        anime = DataVelibCollect.js_animation(df)

        from JSAnimation import HTMLWriter
        wr = HTMLWriter(embed_frames=False)
        anime.save(os.path.join(fold, "out_animation.html"), writer=wr)
 def test_matrice_carre(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     mat = construit_matrice_carree(10)
     assert len(mat) == 10
     assert len(mat[0]) == 10
Exemplo n.º 32
0
    def test_csharp(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        code = """
        namespace hello
        {
            public static class world
            {
                public static double function(double x, doubly y)
                {
                    return x+y ;
                }
            }
        }
        """

        clparser, cllexer = get_parser_lexer("C#")
        parser = parse_code(code, clparser, cllexer)
        tree = parser.parse()
        st = get_tree_string(tree, parser)
        fLOG(st.replace("\\n", "\n"))
        assert len(st) > 0
    def test_flake8(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        thi = os.path.abspath(os.path.dirname(__file__))
        src = os.path.normpath(os.path.join(thi, "..", "..", "src"))
        exe = os.path.dirname(sys.executable)
        scr = os.path.join(exe, "Scripts")
        fla = os.path.join(scr, "flake8")
        cmd = fla + " " + src
        out, err = run_cmd(cmd, fLOG=fLOG, wait=True)

        lines = out.split("\n")
        lines = [_ for _ in lines if "E501" not in _ and "__init__.py" not in _ and "E265" not in _
                 and "W291" not in _]
        lines = [_ for _ in lines if len(_) > 1]
        if __name__ == "__main__":
            for l in lines:
                spl = l.split(":")
                if len(spl[0]) == 1:
                    spl[1] = ":".join(spl[0:2])
                    del spl[0]
                    print(
                        '  File "{0}", line {1}, {2}'.format(spl[0], spl[1], spl[-1]))
        if len(lines) > 0:
            raise Exception(
                "{0} lines\n{1}".format(len(lines), "\n".join(lines)))
Exemplo n.º 34
0
 def test_ls(self):
     fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")
     if self.client is None:
         return
     df = self.client.ls(self.blob_serv, None)
     fLOG(df)
     assert isinstance(df, pandas.DataFrame)
    def test_notebook_runner(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        jupyter_cython_extension()
Exemplo n.º 36
0
 def test_einstein_prolog(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     en = Enigma()
     en.solve(logf=fLOG)
     sol = str(en)
     assert "jaune     , norvegien , eau       , Dunhill   , chats" in sol
 def test_voice(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     return
     if sys.platform.startswith("win"):
         cm = CustomMagics()
         cm.SPEAK("fr-FR", "hello")
Exemplo n.º 38
0
 def test_tail_utf8(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     fp = os.path.join(os.path.dirname(__file__), "data", "lines_utf8.txt")
     lines = file_tail(fp, nbline=3, threshold=20)
     fLOG(lines)
 def test_magic_cs(self):
     fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
     if sys.platform.startswith("win"):
         code = "public static double SquareX(double x) {return x*x ; }"
         f = create_cs_function("SquareX", code)       
         r = f ( 2.0 )
         fLOG(r,type(r))
         assert r == 4
Exemplo n.º 40
0
 def test_import_all(self):
     fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")
     all = [" "]
     if __name__ == "__main__":
         # we only test all the resources if this file is the main file
         # otherwise it takes too much time
         for a in all:
             pass
    def test_jenkins_local(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        st = rst_table_modules()
        fLOG("\n" + st)
        assert st is not None
        assert len(st) > 0
Exemplo n.º 42
0
 def test_magic_cs(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     if sys.platform.startswith("win"):
         code = "public static double SquareX(double x) {return x*x ; }"
         f = create_cs_function("SquareX", code)
         r = f(2.0)
         fLOG(r, type(r))
         assert r == 4
 def test_cs(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     if sys.platform.startswith("win"):
         cm = CustomMagics()
         code = "public static double SquareX(double x) {return x*x ; }"
         f = cm.CS("SquareX", code)
         x = f(2.0)
         assert x == 4
Exemplo n.º 44
0
 def test_notebook_runner(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook2a_2_corection_2B")
     keepnote = ls_notebooks("td2a")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote, lambda i, n: "_2" in n
                             and "correction" in n and "_2B" in n, fLOG=fLOG)
     unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 45
0
    def test_r(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip travis and R
            warnings.warn("travis, unable to test TestR.test_r")
            return

        assert r_and_notebook()
Exemplo n.º 46
0
    def test_emails(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        data = os.path.abspath(os.path.dirname(__file__))
        data = os.path.join(data, "data")
        emails = get_emails(path=data)
        assert emails == [
            '*****@*****.**', ' name.lastname@something'
        ]
    def test_folium(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        import folium
        temp = get_temp_folder(__file__, "temp_folium")
        outfile = os.path.join(temp, "osm.map")

        map_osm = folium.Map(location=[48.85, 2.34])
        map_osm.create_map(path=outfile)
        assert os.path.exists(outfile)
    def test_server_start_run(self):
        """
        if this test fails, the unit test is stuck, you need to stop the program yourself
        """
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip travis and Flask
            warnings.warn(
                "travis, unable to test TestSimpleServerRSSTeaching.test_server_start_run")
            return

        temp = get_temp_folder(__file__, "temp_rss_starter")
        dirn = os.path.abspath(os.path.dirname(location))
        xmlb = None

        data = os.path.join(temp, "..", "data", "ensae_teaching_cs_blogs.db3")
        shutil.copy(data, temp)
        dbfile = os.path.join(temp, "ensae_teaching_cs_blogs.db3")

        server = RSSServer(('localhost', 8093), dbfile)

        thread = rss_teachings_update_run_server(dbfile=dbfile, xml_blogs=xmlb,
                                                 browser="none",
                                                 server=server, thread=True)

        fLOG(thread)
        fLOG("fetching first url")
        url = "http://localhost:8093/"
        cont = get_url_content_timeout(url)
        if "Traceback" in cont and not "l''exception ::      Traceback" in cont:
            raise Exception(cont)
        assert len(cont) > 0
        assert "RSS" in cont
        assert "XD blog" in cont

        fLOG("fetching first url")
        url = "http://localhost:8093/rss_search.html?searchterm=command"
        cont = get_url_content_timeout(url)
        if "Traceback" in cont:
            fLOG(cont)
        if "Traceback" in cont and not "l''exception ::      Traceback" in cont:
            raise Exception(cont)
        assert len(cont) > 0
        assert "RSS" in cont
        assert "interesting" not in cont
        assert "command" in cont

        thread.shutdown()
        assert not thread.is_alive()
Exemplo n.º 49
0
 def test_algo2(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     folder = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                           "temp_rues2")
     if not os.path.exists(folder):
         os.mkdir(folder)
     edges = get_data(whereTo=folder, fLOG=fLOG)
     edges = edges[:1000]
     added = eulerien_extension(edges, fLOG=fLOG, alpha=1 / 8)
     assert len(added) > 0
     fLOG("nb added", len(added))
Exemplo n.º 50
0
    def test_faq_ipython(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        nbfile = os.path.abspath(os.path.dirname(__file__))
        nbfile = os.path.join(nbfile, "..", "..", "_doc", "notebooks", "1a",
                              "code_liste_tuple.ipynb")
        assert os.path.exists(nbfile)

        if __name__ == "__main__" and "travis" not in sys.executable.lower():
            s = jupyter_open_notebook(nbfile, fLOG=fLOG)
            fLOG(s)
Exemplo n.º 51
0
 def test_notebook_runner_1a(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook1a_")
     keepnote = ls_notebooks("1a")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote,
                             lambda i, n: "deviner" not in n,
                             fLOG=fLOG,
                             clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 52
0
 def test_notebook_runner_enonce_12(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook1a_correction_12")
     keepnote = ls_notebooks("td1a")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote,
                             lambda i, n: "correction_session_12" in n,
                             fLOG=fLOG,
                             deepfLOG=fLOG if __name__ == "__main__" else noLOG,
                             clean_function=TestNotebookRunner1a_correction_12.clean_function)
     unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 53
0
 def test_notebook_runner_exams(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebookexams_")
     keepnote = ls_notebooks("exams")
     assert len(keepnote) > 0
     res = execute_notebooks(temp,
                             keepnote,
                             lambda i, n: "git" not in n and
                             "interro_rapide_20_minutes_2014_11" not in n,
                             fLOG=fLOG,
                             clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 54
0
    def test_notebook_runner_2a(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip C# on linux
            warnings.warn(
                "travis, unable to test TestNotebookRunner2a_csharp.test_notebook_runner_2a"
            )
            return

        if not sys.platform.startswith("win"):
            return

        temp = get_temp_folder(__file__, "temp_notebook2a_sharp")
        keepnote = ls_notebooks("2a")
        assert len(keepnote) > 0
        res = execute_notebooks(temp,
                                keepnote,
                                lambda i, n: "csharp" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)

        assert len(res) > 0
        fails = [(os.path.split(k)[-1], v) for k, v in sorted(res.items())
                 if not v[0]]
        for f in fails:
            fLOG(f)
        if len(fails) > 0:
            e = str(fails[0][1][-1])
            if "Audio device error encountered" in str(e):
                # maybe the script is running on a virtual machine (no Audia
                # device)
                if os.environ["USERNAME"] == "ensaestudent" or \
                   os.environ["USERNAME"] == "vsxavierdupre" or \
                   "paris" in os.environ["COMPUTERNAME"].lower() or \
                   os.environ["USERNAME"].endswith("$"):  # anonymous Jenkins configuration
                    # I would prefer to catch a proper exception
                    # it just exclude one user only used on remotre machines
                    fLOG("no audio")
                    return
            elif "<class 'int'>-" in str(e):
                # issue with conversion from 3 to double
                return

            fLOG(str(e).replace("\n", " EOL "))
            raise fails[0][1][-1]
        else:
            fLOG("success")
Exemplo n.º 55
0
    def test_pythonnet(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")
        if sys.platform.startswith("win"):
            from src.ensae_teaching_cs.pythonnet import clr
            from System import String
            s = String("example")
            x = s.Replace("e", "j")
            assert "jxamplj" == x

            from System.Collections.Generic import Dictionary
            d = Dictionary[String, String]()
            d["un"] = "1"
            assert d.Count == 1
 def test_notebook_runner_exposelong1(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebookexposelong1_")
     keepnote = ls_notebooks("expose")
     assert len(keepnote) > 0
     res = execute_notebooks(
         temp,
         keepnote,
         lambda i, n: "velib" in n,
         fLOG=fLOG,
         deepfLOG=fLOG if __name__ == "__main__" else noLOG,
         clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 57
0
 def test_notebook_runner_expose1(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebookexpose1_")
     keepnote = ls_notebooks("expose")
     assert len(keepnote) > 0
     res = execute_notebooks(
         temp,
         keepnote,
         lambda i, n: "velib" not in n and "paris_parcours" not in n and
         "ml_table_mortalite" not in n,
         fLOG=fLOG,
         clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
Exemplo n.º 58
0
    def test_translate(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")
        data = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data")
        russe = os.path.join(data, "russe.txt")
        assert os.path.exists(russe)
        with open(russe, "r", encoding="utf8") as f:
            lines = f.readlines()
        lines = [_.strip(" \r\n\t") for _ in lines]
        lines = [_ for _ in lines if not _.startswith("#") and len(_) > 0]

        temp = os.path.join(data, "..", "temp_translate")
        if not os.path.exists(temp):
            os.mkdir(temp)
        dest = os.path.join(temp, "out_russe_en.txt")

        if not os.path.exists(dest):
            import goslate
            gs = goslate.Goslate()
            tlines = []
            for i, l in enumerate(lines):
                fLOG("try", len(l), len(tlines))
                try:
                    tt = gs.translate(l, 'en', 'ru')
                except socket.timeout:
                    # we miss some of the lines
                    continue
                except urllib.error.HTTPError:
                    # service unavailable (503)
                    continue
                except urllib.error.URLError:
                    # time out
                    continue
                tlines.append(tt)

                if i > len(lines) / 2 and len(tlines) == 0:
                    # some connections did not work
                    break

            with open(dest, "w", encoding="utf8") as f:
                f.write("\n\n".join(tlines))

        assert os.path.exists(dest)
        assert len(lines) > 0  # just to be sure we got one
Exemplo n.º 59
0
 def test_notebook_runner_correction_9_10(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook1a_correction_9_10")
     keepnote = ls_notebooks("td1a")
     assert len(keepnote) > 0
     res = execute_notebooks(
         temp,
         keepnote,
         lambda i, n: "_12" not in n and "session1." not in n and
         "session2." not in n and "session3." not in n and "session4."
         not in n and "session5." not in n and "session6." not in n and
         "session7." not in n and "session8." not in n and "session_11."
         not in n and "correction" in n,
         fLOG=fLOG,
         clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)