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

Evaluate python expression even if it contains 'newline' character #306

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ramona-grad
Copy link
Contributor

@ramona-grad ramona-grad commented Aug 11, 2020

Evaluate python expression even if they contain 'newline' character.
Replace 'newline' characters ('\n') with empty string in order to be able to evaluate python expressions written on multiple lines.

@ramona-grad ramona-grad self-assigned this Aug 11, 2020
@ramona-grad ramona-grad changed the title Evaluate python expression even if they contain 'newlines' character Evaluate python expression even if they contain 'newline' character Aug 11, 2020
@ramona-grad ramona-grad changed the title Evaluate python expression even if they contain 'newline' character Evaluate python expression even if it contains 'newline' character Aug 11, 2020
@@ -100,7 +100,7 @@ public PythonEvaluationResult eval(String expression, String prepareEnvironmentS
try {
String pythonPath = checkPythonPath();
tempEvalEnvironment = generateTempResourcesForEval();
String payload = generatePayloadForEval(expression, prepareEnvironmentScript, context);
String payload = generatePayloadForEval(expression.replaceAll("[\\n\\r]", ""), prepareEnvironmentScript, context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this consistent? if the user is allowed in first place to write multi line expression, he will expect that multiline to work. If we replace the newline with blank/spaces, we will break the expression. (considering there are two statements, on separate lines)

Copy link
Member

@lucian-cm lucian-cm Aug 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexbajzat a valid expression should be resolved to a pyhton value and cannot have statements, we should replace with space and we should not break expression, it it is a statement then an expression we don't care, we allow this 'behaviour' in order to not have to have only single line expressions, but support a single expression written on multiple lines.

Copy link
Collaborator

@alexcampanu alexcampanu Aug 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python has native support of separating a statement into a multi-line one by simply using the \ character.
Additionally, you can also write multiple statements on one line if you separate them with semicolons.
This change feels like an unnecessary hack to me.
Additionally, if this is merged, there is a significant risk that it will probably break this native python support - users who write valid multi-line statements will end up not understanding why their valid python code does not work work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in the case when you are using that \ for splitting the expression into multiple lines it will work.
But we agreed that we want to let user to also write expression in multiple lines without \ (which is not supported in python).
This change is specially for cases like that.

Copy link
Collaborator

@alexcampanu alexcampanu Aug 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, in case of a multi-line string inside an expression (written with triple quotes), it will remove the completely valid \n characters and turn the user's string into a single line string at runtime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but how should we support then multiple lines expressions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcampanu nice catch!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ramona-grad you could use the tree in client to determine which tokens are strings and replace newlines only in non-string tokens.

@@ -100,7 +100,7 @@ public PythonEvaluationResult eval(String expression, String prepareEnvironmentS
try {
String pythonPath = checkPythonPath();
tempEvalEnvironment = generateTempResourcesForEval();
String payload = generatePayloadForEval(expression, prepareEnvironmentScript, context);
String payload = generatePayloadForEval(expression.replaceAll("[\\n\\r]", ""), prepareEnvironmentScript, context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is this unix/windows friendly ? \n\r is a windows stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we have:

Windows: '\r\n'
Unix/Linux: '\n'

It will replace all '\n' or '\r' characters in the string.

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 this pull request may close these issues.

5 participants