How to use the jupytext.formats.long_form_multiple_formats function in jupytext

To help you get started, we’ve selected a few jupytext examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mwouts / jupytext / tests / test_paired_paths.py View on Github external
def test_many_and_suffix():
    formats = long_form_multiple_formats('ipynb,.pct.py,_lgt.py')
    expected_paths = ['notebook.ipynb', 'notebook.pct.py', 'notebook_lgt.py']
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    with pytest.raises(InconsistentPath):
        paired_paths('wrong_suffix.py', 'py', formats)
github mwouts / jupytext / tests / test_paired_paths.py View on Github external
def test_prefix_on_root_174():
    short_formats = 'ipynb,python//py:percent'

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = ['/Untitled.ipynb', '/python/Untitled.py']
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))
github mwouts / jupytext / tests / test_formats.py View on Github external
def test_decompress_formats():
    assert long_form_multiple_formats('ipynb') == [{'extension': '.ipynb'}]
    assert long_form_multiple_formats('ipynb,md') == [{'extension': '.ipynb'}, {'extension': '.md'}]
    assert long_form_multiple_formats('ipynb,py:light') == [{'extension': '.ipynb'},
                                                            {'extension': '.py', 'format_name': 'light'}]
    assert long_form_multiple_formats(['ipynb', '.py:light']) == [{'extension': '.ipynb'},
                                                                  {'extension': '.py', 'format_name': 'light'}]
    assert long_form_multiple_formats('.pct.py:percent') == [
        {'extension': '.py', 'suffix': '.pct', 'format_name': 'percent'}]
github mwouts / jupytext / tests / test_formats.py View on Github external
def test_decompress_formats():
    assert long_form_multiple_formats('ipynb') == [{'extension': '.ipynb'}]
    assert long_form_multiple_formats('ipynb,md') == [{'extension': '.ipynb'}, {'extension': '.md'}]
    assert long_form_multiple_formats('ipynb,py:light') == [{'extension': '.ipynb'},
                                                            {'extension': '.py', 'format_name': 'light'}]
    assert long_form_multiple_formats(['ipynb', '.py:light']) == [{'extension': '.ipynb'},
                                                                  {'extension': '.py', 'format_name': 'light'}]
    assert long_form_multiple_formats('.pct.py:percent') == [
        {'extension': '.py', 'suffix': '.pct', 'format_name': 'percent'}]
github mwouts / jupytext / tests / test_paired_paths.py View on Github external
def test_simple_pair():
    formats = long_form_multiple_formats('ipynb,py')
    expected_paths = ['notebook.ipynb', 'notebook.py']
    compare(paired_paths('notebook.ipynb', 'ipynb', formats), list(zip(expected_paths, formats)))
    compare(paired_paths('notebook.py', 'py', formats), list(zip(expected_paths, formats)))
github mwouts / jupytext / tests / test_paired_paths.py View on Github external
def test_prefix_and_suffix():
    short_formats = 'notebook_folder/notebook_prefix_/_notebook_suffix.ipynb,' \
                    'script_folder//_in_percent_format.py:percent,' \
                    'script_folder//_in_light_format.py'

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = ['parent/notebook_folder/notebook_prefix_NOTEBOOK_NAME_notebook_suffix.ipynb',
                      'parent/script_folder/NOTEBOOK_NAME_in_percent_format.py',
                      'parent/script_folder/NOTEBOOK_NAME_in_light_format.py']
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # without the parent folder
    expected_paths = [path[7:] for path in expected_paths]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # Not the expected parent folder
    with pytest.raises(InconsistentPath):
        paired_paths('script_folder_incorrect/NOTEBOOK_NAME_in_percent_format.py', formats[1], formats)
github mwouts / jupytext / jupytext / contentsmanager.py View on Github external
def update_paired_notebooks(self, path, fmt, formats):
            """Update the list of paired notebooks to include/update the current pair"""
            if not formats:
                self.drop_paired_notebook(path)
                return

            new_paired_paths = paired_paths(path, fmt, formats)
            for alt_path, _ in new_paired_paths:
                self.drop_paired_notebook(alt_path)

            long_formats = long_form_multiple_formats(formats)
            if len(long_formats) == 1 and set(long_formats[0]) <= {'extension'}:
                return

            short_formats = short_form_multiple_formats(formats)
            for alt_path, alt_fmt in new_paired_paths:
                self.paired_notebooks[alt_path] = short_form_one_format(alt_fmt), short_formats
github mwouts / jupytext / jupytext / contentsmanager.py View on Github external
metadata = nbk.get('metadata')
                rearrange_jupytext_metadata(metadata)
                jupytext_metadata = metadata.setdefault('jupytext', {})
                jupytext_formats = jupytext_metadata.get('formats') or self.default_formats(path)

                if not jupytext_formats:
                    text_representation = jupytext_metadata.get('text_representation', {})
                    ext = os.path.splitext(path)[1]
                    fmt = {'extension': ext}

                    if ext == text_representation.get('extension') and text_representation.get('format_name'):
                        fmt['format_name'] = text_representation.get('format_name')

                    jupytext_formats = [fmt]

                jupytext_formats = long_form_multiple_formats(jupytext_formats, metadata,
                                                              auto_ext_requires_language_info=False)

                # Set preferred formats if not format name is given yet
                jupytext_formats = [preferred_format(f, self.preferred_jupytext_formats_save) for f in jupytext_formats]

                base, fmt = find_base_path_and_format(path, jupytext_formats)
                self.update_paired_notebooks(path, fmt, jupytext_formats)
                self.set_default_format_options(jupytext_metadata)

                if not jupytext_metadata:
                    metadata.pop('jupytext')

                # Save as ipynb first
                return_value = None
                value = None
                for fmt in jupytext_formats[::-1]:
github mwouts / jupytext / jupytext / cli.py View on Github external
def set_prefix_and_suffix(fmt, notebook, nb_file):
    """Add prefix and suffix information from jupytext.formats if format and path matches"""
    for alt_fmt in long_form_multiple_formats(notebook.metadata.get('jupytext', {}).get('formats')):
        if (alt_fmt['extension'] == fmt['extension']
                and fmt.get('format_name') == alt_fmt.get('format_name')):
            try:
                base_path(nb_file, alt_fmt)
                fmt.update(alt_fmt)
                return
            except InconsistentPath:
                continue