当前位置: 首页>>代码示例>>Python>>正文


Python nbformat.read函数代码示例

本文整理汇总了Python中nbformat.read函数的典型用法代码示例。如果您正苦于以下问题:Python read函数的具体用法?Python read怎么用?Python read使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了read函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main_diff

def main_diff(afn, bfn, dfn=None):
    for fn in (afn, bfn):
        if not os.path.exists(fn):
            print("Missing file {}".format(fn))
            return 1

    a = nbformat.read(afn, as_version=4)
    b = nbformat.read(bfn, as_version=4)

    # TODO: Split lines here?
    #a = split_lines(a)
    #b = split_lines(b)

    d = diff_notebooks(a, b)

    verbose = True
    if verbose:
        pretty_print_notebook_diff(afn, bfn, a, d)

    if dfn:
        with open(dfn, "w") as df:
            # Compact version:
            #json.dump(d, df)
            # Verbose version:
            json.dump(d, df, indent=2, separators=(",", ": "))
    return 0
开发者ID:ijstokes,项目名称:nbdime,代码行数:26,代码来源:nbdiffapp.py

示例2: main

def main():
    if len(sys.argv) > 1:
        if sys.argv[1] in ['help', '-h', '--help']:
            print(__doc__, file=sys.stderr)
            sys.exit(1)
        if sys.argv[1] in ['install', '--install']:
            sys.exit(install())

        force = False
        filenames = sys.argv[1:]
        if filenames[0] in ['-f', '--force']:
            force = True
            filenames.pop(0)

        for filename in filenames:
            if not force and not filename.endswith('.ipynb'):
                continue
            try:
                with io.open(filename, 'r', encoding='utf8') as f:
                    nb = read(f, as_version=NO_CONVERT)
                nb = strip_output(nb)
                with io.open(filename, 'w', encoding='utf8') as f:
                    write(nb, f)
            except Exception:
                # Ignore exceptions for non-notebook files.
                print("Could not strip '{}'".format(filename))
                raise
    else:
        write(strip_output(read(sys.stdin, as_version=NO_CONVERT)), sys.stdout)
开发者ID:nimanshr,项目名称:nbstripout,代码行数:29,代码来源:nbstripout.py

示例3: main_diff

def main_diff(args):
    afn = args.base
    bfn = args.remote
    dfn = args.output

    for fn in (afn, bfn):
        if not os.path.exists(fn):
            print("Missing file {}".format(fn))
            return 1

    a = nbformat.read(afn, as_version=4)
    b = nbformat.read(bfn, as_version=4)

    d = diff_notebooks(a, b)

    if dfn:
        with io.open(dfn, "w", encoding="utf8") as df:
            # Compact version:
            # json.dump(d, df)
            # Verbose version:
            json.dump(d, df, indent=2, separators=(",", ": "))
    else:
        # This printer is to keep the unit tests passing,
        # some tests capture output with capsys which doesn't
        # pick up on sys.stdout.write()
        class Printer:
            def write(self, text):
                print(text, end="")

        pretty_print_notebook_diff(afn, bfn, a, d, Printer())

    return 0
开发者ID:jupyter,项目名称:nbdime,代码行数:32,代码来源:nbdiffapp.py

示例4: setUp

 def setUp(self):
     self.data_dir = tempfile.mkdtemp()
     self.notary = sign.NotebookNotary(
         db_file=':memory:',
         secret=b'secret',
         data_dir=self.data_dir,
     )
     with self.fopen(u'test3.ipynb', u'r') as f:
         self.nb = read(f, as_version=4)
     with self.fopen(u'test3.ipynb', u'r') as f:
         self.nb3 = read(f, as_version=3)
开发者ID:Carreau,项目名称:nbformat,代码行数:11,代码来源:test_sign.py

示例5: main

def main():
    parser = ArgumentParser(epilog=__doc__, formatter_class=RawDescriptionHelpFormatter)
    task = parser.add_mutually_exclusive_group()
    task.add_argument('--install', action='store_true',
                      help="""Install nbstripout in the current repository (set
                              up the git filter and attributes)""")
    task.add_argument('--uninstall', action='store_true',
                      help="""Uninstall nbstripout from the current repository
                              (remove the git filter and attributes)""")
    task.add_argument('--is-installed', action='store_true',
                      help='Check if nbstripout is installed in current repository')
    task.add_argument('--status', action='store_true',
                      help='Print status of nbstripout installation in current repository and configuration summary if installed')
    parser.add_argument('--attributes', metavar='FILEPATH', help="""Attributes
        file to add the filter to (in combination with --install/--uninstall),
        defaults to .git/info/attributes""")
    task.add_argument('--version', action='store_true',
                      help='Print version')
    parser.add_argument('--force', '-f', action='store_true',
                        help='Strip output also from files with non ipynb extension')
    parser.add_argument('files', nargs='*', help='Files to strip output from')
    args = parser.parse_args()

    if args.install:
        sys.exit(install(args.attributes))
    if args.uninstall:
        sys.exit(uninstall(args.attributes))
    if args.is_installed:
        sys.exit(status(verbose=False))
    if args.status:
        sys.exit(status(verbose=True))
    if args.version:
        print(__version__)
        sys.exit(0)

    for filename in args.files:
        if not (args.force or filename.endswith('.ipynb')):
            continue
        try:
            with io.open(filename, 'r', encoding='utf8') as f:
                nb = read(f, as_version=NO_CONVERT)
            nb = strip_output(nb)
            with io.open(filename, 'w', encoding='utf8') as f:
                write(nb, f)
        except Exception:
            # Ignore exceptions for non-notebook files.
            print("Could not strip '{}'".format(filename))
            raise
    if not args.files:
        write(strip_output(read(input_stream, as_version=NO_CONVERT)), output_stream)
开发者ID:oogali,项目名称:nbstripout,代码行数:50,代码来源:nbstripout.py

示例6: main_merge

def main_merge(args):
    bfn = args.base
    lfn = args.local
    rfn = args.remote
    mfn = args.output

    for fn in (bfn, lfn, rfn):
        if not os.path.exists(fn):
            print("Cannot find file '{}'".format(fn))
            print(_usage)
            return 1

    b = nbformat.read(bfn, as_version=4)
    l = nbformat.read(lfn, as_version=4)
    r = nbformat.read(rfn, as_version=4)

    # TODO: Split lines here?
    #b = split_lines(b)
    #l = split_lines(l)
    #r = split_lines(r)

    m, lc, rc = merge_notebooks(b, l, r, args)

    if lc or rc:
        print("Conflicts occured during merge operation.")
    else:
        print("Merge completed successfully with no unresolvable conflicts.")

    if mfn:
        if lc or rc:
            # Write partial merge and conflicts to a foo.ipynb-merge file
            result = {
                "merged": m,
                "local_conflicts": lc,
                "remote_conflicts": rc
                }
            with open(mfn+"-merge", "w") as mf:
                json.dump(result, mf)
        else:
            # Write fully completed merge to given foo.ipynb filename
            with open(mfn, "w") as mf:
                nbformat.write(m, mf)
    else:
        # FIXME: Display conflicts in a useful way
        if lc or rc:
            print("Local conflicts:")
            pprint(lc)
            print("Remote conflicts:")
            pprint(rc)
    return 0
开发者ID:gahjelle,项目名称:nbdime,代码行数:50,代码来源:nbmergeapp.py

示例7: main

def main():
    if len(sys.argv) > 1:
        if sys.argv[1] in ['help', '-h', '--help']:
            print(__doc__, file=sys.stderr)
            sys.exit(1)
        if sys.argv[1] in ['install', '--install']:
            sys.exit(install())
        filename = sys.argv[1]
        with io.open(filename, 'r', encoding='utf8') as f:
            nb = read(f, as_version=NO_CONVERT)
        nb = strip_output(nb)
        with io.open(filename, 'w', encoding='utf8') as f:
            write(nb, f)
    else:
        write(strip_output(read(sys.stdin, as_version=NO_CONVERT)), sys.stdout)
开发者ID:jayklumetrix,项目名称:nbstripout,代码行数:15,代码来源:nbstripout.py

示例8: execute_nb

def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=''):
    """
    Execute notebook in `src` and write the output to `dst`

    Parameters
    ----------
    src, dst: str
        path to notebook
    allow_errors: bool
    timeout: int
    kernel_name: str
        defualts to value set in notebook metadata

    Returns
    -------
    dst: str
    """
    import nbformat
    from nbconvert.preprocessors import ExecutePreprocessor

    with io.open(src, encoding='utf-8') as f:
        nb = nbformat.read(f, as_version=4)

    ep = ExecutePreprocessor(allow_errors=allow_errors,
                             timeout=timeout,
                             kernel_name=kernel_name)
    ep.preprocess(nb, resources={})

    with io.open(dst, 'wt', encoding='utf-8') as f:
        nbformat.write(nb, f)
    return dst
开发者ID:RogerThomas,项目名称:pandas,代码行数:31,代码来源:make.py

示例9: execute_nb

def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=None):
    """
    Execute notebook in `src` and write the output to `dst`

    Parameters
    ----------
    src, dst: str
        path to notebook
    allow_errors: bool
    timeout: int
    kernel_name: str
        defualts to value set in notebook metadata

    Returns
    -------
    dst: str
    """
    with io.open(src, encoding="utf-8") as f:
        nb = nbformat.read(f, as_version=4)

    ep = ExecutePreprocessor(allow_errors=allow_errors, timeout=timeout, kernel_name=kernel_name)
    ep.preprocess(nb, {"metadta": {"path": "notebooks/"}})

    with io.open(dst, "wt", encoding="utf-8") as f:
        nbformat.write(nb, f)
    return dst
开发者ID:statsmodels,项目名称:statsmodels,代码行数:26,代码来源:nbgenerate.py

示例10: test_iter_validation_error

    def test_iter_validation_error(self):
        with self.fopen(u'invalid.ipynb', u'r') as f:
            nb = read(f, as_version=4)

        errors = list(iter_validate(nb))
        assert len(errors) == 3
        assert {e.ref for e in errors} == {'markdown_cell', 'heading_cell', 'bad stream'}
开发者ID:dalejung,项目名称:nbformat,代码行数:7,代码来源:test_validator.py

示例11: _notebook_run

def _notebook_run(path):
    """
    Execute a notebook via nbconvert and collect output.

    Taken from
    https://blog.thedataincubator.com/2016/06/testing-jupyter-notebooks/

    Args:
        path (str): file path for the notebook object

    Returns: (parsed nb object, execution errors)

    """
    dirname, __ = os.path.split(path)
    os.chdir(dirname)
    with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
        args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
                "--ExecutePreprocessor.timeout=60",
                "--output", fout.name, path]
        subprocess.check_call(args)

        fout.seek(0)
        nb = nbformat.read(fout, nbformat.current_nbformat)

    errors = [output for cell in nb.cells if "outputs" in cell
              for output in cell["outputs"]\
              if output.output_type == "error"]

    return nb, errors
开发者ID:ardunn,项目名称:matminer_examples,代码行数:29,代码来源:test_examples.py

示例12: load_module

    def load_module(self, fullname):
        """import a notebook as a module"""
        path = find_notebook(fullname, self.path)

        # load the notebook object
        with io.open(path, 'r', encoding='utf-8') as f:
            nb = nbformat.read(f, 4)

        # create the module and add it to sys.modules
        # if name in sys.modules:
        #    return sys.modules[name]
        mod = types.ModuleType(fullname)
        mod.__file__ = path
        mod.__loader__ = self
        mod.__dict__['get_ipython'] = get_ipython
        sys.modules[fullname] = mod

        # extra work to ensure that magics that would affect the user_ns
        # actually affect the notebook module's ns
        save_user_ns = self.shell.user_ns
        self.shell.user_ns = mod.__dict__

        try:
            for cell in nb.cells:
                if cell.cell_type == 'code':
                    # transform the input to executable Python
                    code = self.shell.input_transformer_manager.transform_cell(cell.source)
                    # run the code in themodule
                    exec(code, mod.__dict__)
        finally:
            self.shell.user_ns = save_user_ns
        return mod
开发者ID:yuvipanda,项目名称:nbimport,代码行数:32,代码来源:__init__.py

示例13: _read_notebook

    def _read_notebook(self, os_path, as_version=4):
        """Read a notebook from an os path."""
        with self.open(os_path, 'r', encoding='utf-8') as f:
            try:
                return nbformat.read(f, as_version=as_version)
            except Exception as e:
                e_orig = e

            # If use_atomic_writing is enabled, we'll guess that it was also
            # enabled when this notebook was written and look for a valid
            # atomic intermediate.
            tmp_path = path_to_intermediate(os_path)

            if not self.use_atomic_writing or not os.path.exists(tmp_path):
                raise HTTPError(
                    400,
                    u"Unreadable Notebook: %s %r" % (os_path, e_orig),
                )

            # Move the bad file aside, restore the intermediate, and try again.
            invalid_file = path_to_invalid(os_path)
            # Rename over existing file doesn't work on Windows
            if os.name == 'nt' and os.path.exists(invalid_file):
                os.remove(invalid_file)
            os.rename(os_path, invalid_file)
            os.rename(tmp_path, os_path)
            return self._read_notebook(os_path, as_version)
开发者ID:AnddyWang,项目名称:notebook,代码行数:27,代码来源:fileio.py

示例14: test_mergedriver

def test_mergedriver(git_repo):
    p = filespath()
    # enable diff/merge drivers
    nbdime.gitdiffdriver.main(['config', '--enable'])
    nbdime.gitmergedriver.main(['config', '--enable'])
    # run merge with no conflicts
    out = get_output('git merge remote-no-conflict', err=True)
    assert 'nbmergeapp' in out
    with open('merge-no-conflict.ipynb') as f:
        merged = f.read()

    with open(os.path.join(p, 'multilevel-test-merged.ipynb')) as f:
        expected = f.read()

    # verify merge success
    assert merged == expected

    # reset
    call('git reset local --hard')

    # run merge with conflicts
    with pytest.raises(CalledProcessError):
        call('git merge remote-conflict')
    
    status = get_output('git status')
    assert 'merge-conflict.ipynb' in status
    out = get_output('git diff HEAD')
    assert 'nbdiff' in out
    # verify that the conflicted result is a valid notebook
    nb = nbformat.read('merge-conflict.ipynb', as_version=4)
    nbformat.validate(nb)
开发者ID:jupyter,项目名称:nbdime,代码行数:31,代码来源:test_cli_apps.py

示例15: execute_nb

def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=None):
    '''
    Execute notebook in `src` and write the output to `dst`

    Parameters
    ----------
    src, dst: str
        path to notebook
    allow_errors: bool
    timeout: int
    kernel_name: str
        defualts to value set in notebook metadata

    Returns
    -------
    dst: str
    '''
    with io.open(src, encoding='utf-8') as f:
        nb = nbformat.read(f, as_version=4)

    ep = ExecutePreprocessor(allow_errors=False,
                             timeout=timeout,
                             kernel_name=kernel_name)
    ep.preprocess(nb, {'metadata': {'path': SOURCE_DIR}})

    with io.open(dst, 'wt', encoding='utf-8') as f:
        nbformat.write(nb, f)
    return dst
开发者ID:ChadFulton,项目名称:statsmodels,代码行数:28,代码来源:nbgenerate.py


注:本文中的nbformat.read函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。