Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_identity_source_write_read(nb_file):
"""Test that writing the notebook with jupytext, and read again,
is the same as removing outputs"""
with open(nb_file) as fp:
nb1 = nbformat.read(fp, as_version=4)
py = jupytext.writes(nb1, 'py')
nb2 = jupytext.reads(py, 'py')
compare_notebooks(nb2, nb1)
def test_identity_source_write_read(nb_file, ext):
"""
Test that writing the notebook with R, and read again,
is the same as removing outputs
"""
with open(nb_file) as fp:
nb1 = nbformat.read(fp, as_version=4)
R = jupytext.writes(nb1, ext)
nb2 = jupytext.reads(R, ext)
compare_notebooks(nb2, nb1)
def test_active_ipynb_rspin(no_jupytext_version_number):
nb = jupytext.reads(ACTIVE_IPYNB_RSPIN['.R'], 'R:spin')
assert len(nb.cells) == 1
compare(jupytext.writes(nb, 'R:spin'), ACTIVE_IPYNB_RSPIN['.R'])
compare(nb.cells[0], ACTIVE_IPYNB_RSPIN['.ipynb'])
# Examples
```jldoctest
julia> cube(2)
8
""" function cube(x) x^3 end''' assert nb.cells[1].cell_type == 'code' assert nb.cells[1].source == 'cube(x)' assert nb.cells[2].cell_type == 'markdown' compare(nb.cells[2].source, 'And a markdown comment')
julia2 = jupytext.writes(nb, 'jl')
compare(julia2, julia)
def test_ipynb_to_percent_to_light(nb_file):
nb = jupytext.read(nb_file)
pct = jupytext.writes(nb, 'auto:percent')
auto_ext = auto_ext_from_metadata(nb.metadata)
comment = _SCRIPT_EXTENSIONS[auto_ext]['comment']
lgt = (pct.replace(comment + ' %%\n', comment + ' +\n')
.replace(comment + ' %% ', comment + ' + ')
.replace(comment + ' format_name: percent', comment + ' format_name: light'))
nb2 = jupytext.reads(lgt, auto_ext)
compare_notebooks(nb2, nb)
display_name: Python 3
language: python
name: python3
---
```python2
a = 1
print a
b = 2
print(b)
"""): md = jupytext.writes(nb, 'md') compare(md, text)
nb2 = jupytext.reads(md, 'md')
compare_notebooks(nb2, nb)
# it's better not to have Jupytext metadata in test notebooks:
if fmt == 'ipynb' and 'jupytext' in notebook.metadata: # pragma: no cover
notebook.metadata.pop('jupytext')
jupytext.write(nb_file, fmt=fmt)
create_mirror_file_if_missing(mirror_file, notebook, fmt)
# Compare the text representation of the two notebooks
if compare_notebook:
nb_mirror = jupytext.read(mirror_file)
compare(nb_mirror, notebook)
return
elif ext == '.ipynb':
notebook = jupytext.read(mirror_file)
fmt.update({'extension': org_ext})
actual = jupytext.writes(notebook, fmt)
with open(nb_file, encoding='utf-8') as fp:
expected = fp.read()
else:
actual = jupytext.writes(notebook, fmt)
with open(mirror_file, encoding='utf-8') as fp:
expected = fp.read()
if not actual.endswith('\n'):
actual = actual + '\n'
compare(actual, expected)
# Compare the two notebooks
if ext != '.ipynb':
notebook = jupytext.read(nb_file)
nb_mirror = jupytext.read(mirror_file, fmt=fmt)
key: value
---
""", target="""---
jupyter:
extra:
key: value
jupytext:
notebook_metadata_filter: extra
kernelspec:
display_name: Python 3
language: python
name: python3
---
"""):
nb = jupytext.reads(org, 'md')
text = jupytext.writes(nb, 'md')
compare(text, target)
pass
# %% a function
def f(x):
return 42 * x
"""):
nb = jupytext.reads(script, 'py')
assert len(nb.cells) == 5
for i in range(5):
assert nb.cells[i].cell_type == 'code'
assert not nb.cells[i].source.startswith('\n')
assert not nb.cells[i].source.endswith('\n')
script2 = jupytext.writes(nb, 'py:hydrogen')
compare(script2, script)
def test_read_julia_notebook(markdown="""```julia
1 + 1
"""): nb = jupytext.reads(markdown, 'md') assert nb.metadata['jupytext']['main_language'] == 'julia' assert len(nb.cells) == 1 assert nb.cells[0].cell_type == 'code' markdown2 = jupytext.writes(nb, 'md') compare(markdown2, markdown)