函数不可调用@python3

函数不可调用@python3,python,function,Python,Function,我正在完成一项家庭作业,我需要一些帮助。我的文件名是pgresu.py 总体目标是制作一个python版本的grep,它的运行方式如下: @CodeTrace.trace() def matchLine(self, line): $cat input.txt |./pgresu.py“the*” 我需要制作一个调试跟踪可调用函数,我应该按如下方式调用: @CodeTrace.trace() def matchLine(self, line): 然而,Pycharm告诉我,“Trace”是不可

我正在完成一项家庭作业,我需要一些帮助。我的文件名是pgresu.py

总体目标是制作一个python版本的grep,它的运行方式如下:

@CodeTrace.trace()
def matchLine(self, line):
$cat input.txt |./pgresu.py“the*”

我需要制作一个调试跟踪可调用函数,我应该按如下方式调用:

@CodeTrace.trace()
def matchLine(self, line):
然而,Pycharm告诉我,“Trace”是不可调用的

我的命令行也出现了一个错误:NameError,没有定义名称“self”

请有人帮助我整理我的功能,并帮助我使他们可调用,提前道歉这项工作仍在进行中

ps请忽略我的评论,在下面的代码中

#!/usr/bin/python3

import sys
import re
import time
import datetime
import inspect
import getopt


class Grepper(object):
    def __init__(self, pattern):
        self.pattern = pattern

    @CodeTrace.Trace(self, line)
    def matchline(self):
        regex = re.compile(self.pattern)
        for line in sys.stdin:
            if regex.search(line):
                sys.stdout.write(line)
                #if option == -d print the following:
                (CodeTrace(line).Trace(line))
                print(locals().keys())
                print(str(self.pattern) + str(regex))


class CodeTrace(object):
    def __init__(self, line):
        self.line = line

    def Trace(self, line):
        #Creating Timestamp
        ts = time.time()
        ts = datetime.datetime.fromtimestamp(ts).strftime('[%Y-%m-%d %H:%M:%S:%f]')
        #Calling class and method
        stack = inspect.stack()
        the_class = stack[1][0].f_locals["self"].__class__
        the_method = stack[1][0].f_code.co_name
        calling_the_class = (" {}.{}() ".format(str(the_class), the_method))
        #Any parameters passed to the method
        #parameters = str((Grepper.matchline(Grepper).keys()))
        #return_value = sys.argv[1]
        (locals().keys())
        #Writing trace
        sys.stderr.write(ts + calling_the_class + "\n")
        """parameters + return_value"""


def main():
    pattern = str(sys.argv[1])
    Grepper(pattern).matchline()

if __name__ == "__main__":
    main()

如果你应该使用
@CodeTrace.trace()
,为什么你要写
@CodeTrace.trace(self,line)
?最初我不必使用@CodeTrace.trace(),我的新任务需要它,他们要求我使用旧代码并简单地更改它。虽然这对我来说并不简单,哈哈