简体   繁体   English

如何在python中区分“字符串”和“实际代码”?

[英]How to differentiate between “a string” and “a actual code” in python?

My works relates to instrumentation of code fragments in python code. 我的作品涉及python代码中的代码片段检测。 So in my work i would be writing a script in python such that I take another python file as input and insert any necessary code in the required place with my script. 因此,在我的工作中,我将使用python编写脚本,以便我将另一个python文件作为输入,并在脚本的所需位置插入任何必要的代码。

The following code is a sample code of a file which i would be instrumenting: 以下代码是我要检测的文件的示例代码:

A.py #normal un-instrumented code

statements
....
....

def move(self,a):
    statements
    ......
    print "My function is defined" 
    ......

statements 
......

My script what actually does is to check each lines in the A.py and if there is a "def" then a code fragment is instrumented on top of the code the def function 我的脚本的实际作用是检查A.py中的每一行,如果有“ def”,则将代码片段插入def函数的代码之上

The following example is how the final out put should be: 以下示例是最终输出的方式:

A.py #instrumented code

statements
....
....

@decorator    #<------ inserted code
def move(self,a):
    statements
    ......
    print "My function is defined" 
    ......

statements 
......

But I have been resulted with different output. 但是我得到的结果却不同。 The following code is the final output which i am getting: 以下代码是我得到的最终输出:

A.py #instrumented code A.py#仪器代码

statements
....
....

@decorator    #<------ inserted code
def move(self,a):
    statements
    ......
    @decorator #<------ inserted code [this should not occur]
    print "My function is defined" 
    ......

statements 
......

I can understand that in the instrumented code it recognizes "def" in the word "defined" and so it instruments the a code above it. 我可以理解,在已检测的代码中,它可以识别单词“ defined”中的“ def”,因此可以检测其上方的代码。

In realty the instrumented code has lots of these problems I was not able to properly instrument the given python file. 实际上,检测到的代码有很多这样的问题,我无法正确检测给定的python文件。 Is there any other way to differentiate the actual "def" from string? 还有其他方法可以将实际的“ def”与字符串区分开吗?

Thank you 谢谢

Use the ast module to parse the file properly. 使用ast模块正确解析文件。

This code prints the line number and column offset of each def statement: 此代码显示每个def语句的行号和列偏移量:

import ast
with open('mymodule.py') as f:
    tree = ast.parse(f.read())
for node in ast.walk(tree):
    if isinstance(node, ast.FunctionDef):
        print node.lineno, node.col_offset

You could use a Regular Expression. 您可以使用正则表达式。 To avoid def inside quotes then you can use negative look-arounds: 为了避免在引号中使用def ,那么您可以使用否定环顾:

import re

for line in open('A.py'):
    m = re.search(r"(?!<[\"'])\bdef\b(?![\"'])", line)
    if m:
        print r'@decorator    #<------ inserted code' 

    print line 

However, there might be other occurances of def that you or I can't think of, and if we are not careful we end-up writing the Python parser all over again. 但是,可能还有其他一些您可能无法想到的def发生,并且如果我们不小心的话,最终将重新编写Python解析器。 @Janne Karila's suggestion of using ast.parse is probably safer in the long term. 从长远来看,@ Janne Karila建议使用ast.parse可能更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM