Last active
July 13, 2019 03:06
-
-
Save podhmo/739dcdb815e78db6e912c532717141d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lineno:4 code:return "[]" node:Node(simple_stmt, [Node(return_stmt, [Leaf(1, 'return'), Leaf(3, '"[]"')]), Leaf(4, '\n')]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pycomment.parse import parse_file, node_name, PyTreeVisitor, token | |
class Visitor(PyTreeVisitor): | |
def __init__(self, lineno): | |
self.lineno = lineno | |
self.r = [] | |
# todo: performance | |
def visit(self, node): | |
if node.get_lineno() == self.lineno and node.type != token.INDENT: | |
self.r.append(node) | |
return | |
super().visit(node) | |
def run(filename: str, *, lineno: int) -> None: | |
visitor = Visitor(lineno) | |
t = parse_file(filename) | |
visitor.visit(t) | |
node = next(iter(visitor.r)) | |
print(f"lineno:{node.get_lineno()} code:{str(node).rstrip()} node:{node!r}") | |
filename = "target00.py" | |
run(filename, lineno=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A: | |
def f(self, x): | |
if x is None: | |
return "[]" | |
else: | |
return f"[{x}]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment