简体   繁体   English

如何知道调用另一个函数的函数所在的脚本名称?

[英]How to know the script name in which the function which calls another function is in?

I'm trying to write a Python script which should basically act as a text parsing command line tool. 我正在尝试编写一个Python脚本,该脚本基本上应充当文本解析命令行工具。 The intent of the script is to search for the function name which is provided as the input argument to the script and also the functions which call the function (my input argument) and give the script names containing the functions along with the line numbers as the output. 脚本的目的是搜索作为脚本输入参数提供的函数名称,以及调用该函数的函数(我的输入参数),并给出包含函数以及行号作为脚本名称的脚本名称。输出。

I have figured out how to get where the input function name is called in the scripts but don't know how to get the name of the functions which call the actual function that I'm searching for. 我已经弄清楚了如何获取脚本中调用输入函数名称的位置,但不知道如何获取调用我正在搜索的实际函数的函数名称。 Can anyone let me know how to proceed? 谁能让我知道如何进行?

import os, fnmatch

searchStr = "PressButton"
directory = 'C:\TFSMap\FalconPlatformTests\GLib'

# in which file to search.
pattern = "*_Main.php"


a = {}

def find_files(directory, pattern):

for root, dirs, files in os.walk(directory):
    for basename in files:
        if fnmatch.fnmatch(basename, pattern):
            filename = os.path.join(root, basename)
            yield filename

    for dir in dirs:
        find_files(directory+'/'+dir, pattern)


for filename in find_files(directory,pattern):
i = 0
with open(filename) as Sid:
    values = ''
    for line in Sid:
       i = i +1
       if searchStr in line:
            if filename in a:
                values = a[filename]
                a[filename] = values +','+ i.__str__()
            else:
                values = i.__str__()
                a[filename] = i.__str__()
    if values != "":
        print (filename+' ===== '+values)

If I understand correctly what you are trying to do, then it requires Python grammar parsing. 如果我正确理解您要执行的操作,则需要Python语法解析。 You aren't just simply searching for strings, but understanding the code to the point of being able to tell that there is a reference to function x in function y . 您不仅可以简单地搜索字符串,还可以理解代码,从而知道在函数y中存在对函数x的引用。 This may be more complex than you think, you need to be able to form a parse tree, decide how far up that tree you want to show when you find a reference, etc. Not to mention edge cases (eg If there is a call to function x in a lambda function, inside of what function will you report it being?) 这可能比您想象的要复杂,您需要能够形成一个分析树,确定找到参考时要显示的树的高度,等等。更不用说边缘情况了(例如,如果有电话在lambda函数中使用x的功能,您会在哪个函数内报告它的存在?)

In any case, you may want to use a Python parser. 无论如何,您可能想使用Python解析器。 Python gives access to its internal parser in the parser module, and there are other python-parsing-tools that you can use. Python允许在parser模块中访问其内部解析器,还有其他可以使用的python-parsing-tools

Note: I assume you are doing this for academic purposes, since essentially all IDE's provide this functionality. 注意:我假设您这样做是出于学术目的,因为基本上所有的IDE都提供此功能。

暂无
暂无

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

相关问题 如何识别在python中调用函数的文件的名称? - How to identify the name of the file which calls the function in python? 定义一个函数,该函数针对各种字符串python调用另一个函数 - defining a function which calls another function for a variety of strings python theano定义了重复调用另一个函数的函数? - theano define function which repeatedly calls another function? 在python中运行js函数调用另一个js函数 - run js function in python which calls another js function python如何执行调用从同一对象调用另一个方法的函数的对象方法 - python how to execute object method that calls a function which calls another method from same object 如何在运行时从模块内部知道哪个脚本在其中调用了函数 - How to know from inside a module, during run time, which script has called a function in it 如何知道列表中的哪个元素触发了 any() 函数? - How can I know which element in a list triggered an any() function? 我可以在Python Flask中调用带有相同文件并带有url请求的函数吗? - Can I call a function which calls another function with the same file with a url request in Python Flask? 是否可以调用一个 cuda.jit function 间接调用另一个 cuda.jit ZC1C1145268E683845 - Is it possible to call a cuda.jit function which indirectly calls another cuda.jit function? Tensorflow如何知道在minimal()中使用哪个导数函数? - How does Tensorflow know which derivative function to use in minimize()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM