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

Can't get return in assignments #96

Closed
b5y opened this issue Apr 1, 2016 · 4 comments
Closed

Can't get return in assignments #96

b5y opened this issue Apr 1, 2016 · 4 comments

Comments

@b5y
Copy link
Contributor

b5y commented Apr 1, 2016

Here is code"

a = \
    2

It is impossible to get return. Here is what I am doing:

red = RedBaron(_source_)
assignments = red.find_all('AssignmentNode')
for ind, body_line in enumerate(assignments.data):
    if body_line.second_formatting.data[0].value == '\n':
        print True

It doesn't print True. Is it bug or I am wrong? How can I detect that assignment is defined in a separated lines?

@Psycojoker
Copy link
Member

Hello,

The support regarding formatting modification isn't very well integrated into RedBaron yet but it's accessible. The way to go to find this kind of information is to use .help(). But, formatting informations aren't displayed by default in .help(), for that, you'll need to use this paramter: http://redbaron.readthedocs.org/en/latest/basics.html#with-formatting

Which gives this in your case:

In [2]: RedBaron("a = 1").help(with_formatting=True)
0 -----------------------------------------------------
AssignmentNode()
  # identifiers: assign, assignment, assignment_, assignmentnode
  operator=''
  target ->
    NameNode()
      # identifiers: name, name_, namenode
      value='a'
  value ->
    IntNode()
      # identifiers: int, int_, intnode
      value='1'
  first_formatting ->
    * SpaceNode()
        # identifiers: space, space_, spacenode
        value=' '
  second_formatting ->
    * SpaceNode()
        # identifiers: space, space_, spacenode
        value=' '

Therefor:

In [7]: RedBaron("a =\\\n    2")[0].second_formatting
Out[7]: 0   '\\\n    '

Hope that helps,

@b5y
Copy link
Contributor Author

b5y commented Apr 4, 2016

Hello,

Sorry for late answer. Your example can't be used in my project. I will explain why. When I compare '\n ' with body_line.second_formatting[0].value, it always gives False result. Even if I copy value from debugging console and past it to Watches window in PyCharm (print screen is attached).

Here is input test.

And my code:

from redbaron import RedBaron


def main():
    input = open('input_test.py', 'rb')
    red = RedBaron(input.read())
    assignments = red.find_all('AssignmentNode')
    for ind, body_line in enumerate(assignments.data):
        print body_line.second_formatting[0].value

if __name__ == '__main__':
    main()

Print screen of debugging process:
screenshot from 2016-04-04 15 28 31

I found another solution for my problem - getting position by absolute_bounding_box and comparing lines, but I want to know how to get comparison by returns. It still doesn't work like standard strings in Python.

@Psycojoker
Copy link
Member

If you only want to see if a "\n" is present in the string you can do it this way:

In [6]: "\n" in RedBaron("a =\\\n    2")[0].second_formatting.dumps()
Out[6]: True

And this other way works too but is less general:

In [7]: "\n" in RedBaron("a =\\\n    2")[0].second_formatting[0].value
Out[7]: True

@b5y
Copy link
Contributor Author

b5y commented Apr 5, 2016

Yes, it is what I want, but it doesn't work in this test case:

f = q\
     &t

Sorry for tediousness - I am implementing Python analyzer and need to check all test cases. absolute_bounding_box is still the only solution.
Anyway, thank you very much!

@b5y b5y closed this as completed Apr 6, 2016
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

No branches or pull requests

2 participants