You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I can't do node.text.split(' '). "Since tokens on hidden channels (e.g. whitespace or comments) are not added to the parse trees, they will not appear in the output of this method." So, I get fromdatetimeimportdatetime,date when traversing from datetime import datetime, date, for example.
I'd want to do something like node.getTokens(), but getTokens(x) takes a ttype as a parameter that I haven't seen documentation for.
I tried just plugging in incremental integers for ttype, but that didn't give me any tokens.
node text fromdatetimeimportdatetime,date
for ttype -1 getTokens returns []
for ttype 0 getTokens returns []
for ttype 1 getTokens returns []
for ttype 2 getTokens returns []
for ttype 3 getTokens returns []
for ttype 4 getTokens returns []
for ttype 5 getTokens returns []
for ttype 6 getTokens returns []
for ttype 7 getTokens returns []
for ttype 8 getTokens returns []
for ttype 9 getTokens returns []
for ttype 10 getTokens returns []
for ttype 11 getTokens returns []
for ttype 12 getTokens returns []
for ttype 13 getTokens returns []
for ttype 14 getTokens returns []
for ttype 15 getTokens returns []
for ttype 16 getTokens returns []
for ttype 17 getTokens returns []
for ttype 18 getTokens returns []
for ttype 19 getTokens returns []
for ttype 20 getTokens returns []
for ttype 21 getTokens returns []
for ttype 22 getTokens returns []
for ttype 23 getTokens returns []
for ttype 24 getTokens returns []
for ttype 25 getTokens returns []
for ttype 26 getTokens returns []
for ttype 27 getTokens returns []
for ttype 28 getTokens returns []
...
I've not used the getTokens method really, but it looks like it doesn't do deep searching, so may not be what you're after. The constants for token types are on Python3Parser though - e.g. Python3Parser.STRING.
To actually investigate that sort of piece of code, I'd look at using the walk or visitor APIs to pick the type of element you're looking for first - e.g.
This snippet still doesn't handle the full complexity of python imports, so e.g. renamed imports would need something different, but this might set you on the right path, depending on what you're trying to do.
Activity
lexanth commentedon Mar 23, 2023
I've not used the
getTokensmethod really, but it looks like it doesn't do deep searching, so may not be what you're after. The constants for token types are onPython3Parserthough - e.g.Python3Parser.STRING.To actually investigate that sort of piece of code, I'd look at using the walk or visitor APIs to pick the type of element you're looking for first - e.g.
This snippet still doesn't handle the full complexity of python imports, so e.g. renamed imports would need something different, but this might set you on the right path, depending on what you're trying to do.
hirehamir commentedon Mar 23, 2023
That snippet definitely helps. Thanks for posting it.
Do you by any chance have any suggestions on how to go about extracting a renamed import from
import datetime as dt, for example?This is what I tried.
This was the output.