Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclamation mark not correctly handled #437

Closed
ftalbrecht opened this issue Feb 14, 2020 · 4 comments · Fixed by #439
Closed

exclamation mark not correctly handled #437

ftalbrecht opened this issue Feb 14, 2020 · 4 comments · Fixed by #439
Milestone

Comments

@ftalbrecht
Copy link

I am using

pip freeze | grep jupytext
jupytext==1.2.4

and the following MWE file (say Untitled.md)

try:
    !echo jo
except:
    pass

(which is intended to be run as a jupyter notebook inside a jupyter server using the jupytext extension, which works well, btw.) is not correctly converted to python on the command line. I.e., the output of

jupytext --to py Untitled.ipynb

yields the following Untitled.py:

cat Untitled.py 
# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.4'
#       jupytext_version: 1.2.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

try:
    !echo jo
except:
    pass

I would expect the !echo jo statement to be commented out, but the presence of the try: seems to hinder this...
This is related to #149, but not the same.

@mwouts mwouts added this to the 1.3.4 milestone Feb 14, 2020
@mwouts
Copy link
Owner

mwouts commented Feb 14, 2020

Hello @ftalbrecht , thanks for reporting this.

That should be easy to fix... we just need to allow spaces before the exclamation mark in this regular expression:

# Commands starting with a question or exclamation mark have to be escaped
_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*(\?|!)\s*[A-Za-z]")

i.e. probably replace it with

_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*\s*(\?|!)\s*[A-Za-z]")

But then, is the empty try statement valid in Python?

@mwouts
Copy link
Owner

mwouts commented Feb 16, 2020

@ftalbrecht , I'll do the fix. Just to mention, when the !echo command is commented out, the Python code is not valid any more, so you will have to add a pass to make the Python representation valid:

image

image

@ftalbrecht
Copy link
Author

@ftalbrecht , I'll do the fix. Just to mention, when the !echo command is commented out, the Python code is not valid any more, so you will have to add a pass to make the Python representation valid:

Sure, that was just meant to be the smallest failing example I could think of. Thx for the fix!

@mwouts
Copy link
Owner

mwouts commented Jul 6, 2020

I am having an issue with

if True:
    !rm file 1
    # !rm file 2

which is not stable over round trip. Preserve the indentation when commenting out should help... let me see...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants