示例#1
0
    def test_get_source_parts(self):
        source_lines = [
            "def f1():",
            "    \"\"\"this is a docstring\"\"\"",
            "    return 1",
            "",
            "def f2():",
            "    f1()",
            "    return 2",
            "",
            "f2()",
            "x = 2+3",
            "",
            "def f3():",
            "    \"\"\"this is a docstring\"\"\"",
            "    wont_work_but_should_parse()"
        ]
        result_str = extract.extract_source_parts("\n".join(source_lines), [
            {"name": "f1", "start":0, "end":3},
            {"name": "f3", "start":11, "end":14}
        ])

        expected_source_lines = [
            "def f1():",
            "    \"\"\"this is a docstring\"\"\"",
            "    return 1",
            "",
            "def f3():",
            "    \"\"\"this is a docstring\"\"\"",
            "    wont_work_but_should_parse()"
        ]
        self.assertEqual("\n".join(expected_source_lines) + "\n", result_str)
示例#2
0
def extract_functions_from_ipynb_files(glob_path):
    result_str = ""
    paths = glob.glob(glob_path)
    for path in sorted(paths):
        ipynb_python_source_str = ipynb_to_python_source_str(path)
        result_str += "# === functions from {} ===\n\n".format(path)
        print "# === functions from {} ===\n\n".format(path)
        result_str += extract.extract_source_parts(ipynb_python_source_str, extract.find_functions_in_source_having_docstring(ipynb_python_source_str))
        result_str += "\n\n\n\n"
    return result_str