传递自类参数python

传递自类参数python,python,timer,Python,Timer,这是我的类的定义,它充满了静态函数。我想在“sendLog”函数中使用它们,该函数以时间间隔(这里是10秒)调用自己。当我运行此解释器时,它告诉我“TypeError:sendLog()至少接受5个参数(给定0个)” 但如果我输入相同的参数,我将需要一次又一次地定义sendLog,因为它会重复调用自己。。我知道这不是办法,但我想不出来 class AccessLog: @staticmethod def backupAccessLog(target, source): newfile

这是我的类的定义,它充满了静态函数。我想在“sendLog”函数中使用它们,该函数以时间间隔(这里是10秒)调用自己。当我运行此解释器时,它告诉我“TypeError:sendLog()至少接受5个参数(给定0个)” 但如果我输入相同的参数,我将需要一次又一次地定义sendLog,因为它会重复调用自己。。我知道这不是办法,但我想不出来

class AccessLog:

@staticmethod
def backupAccessLog(target, source):
    newfile = os.path.splitext(source)[0] + "_" + time.strftime("%Y%m%d-%H%M%S") + os.path.splitext(source)[1]
    copyfile(source,newfile)
    shutil.move(newfile,target)

@staticmethod
def emptyAccessLog(filename):
    open(filename, 'w').close()

@staticmethod
def postLogstoElastic():
    fileLogs = open("example.log", "rw+")
    fileBackups = open("logs_of_accesslog.log","rw+")
    lines = fileLogs.read().splitlines()
    logging.basicConfig(format='%(asctime)s>>>%(message)s',filename='logs_exceptions.log',level=logging.DEBUG)
    es = Elasticsearch(['http://localhost:9200/'], verify_certs=True)
    #es.create(index="index_log23June", doc_type="type_log23June")
    es.indices.create(index='index_log23June', ignore=400)
    i=0
    for item in lines:
        try:
            i+=1
            if bool(item):
                es.index(index="index_log23June",doc_type="type_log23June", body={"Log":item})
            else:
                print "a speace line ignored. at line number:", i
                raise ValueError('Error occurred on this line: ', i)
            print "lines[",i,"]:",item,"\n"

        except ValueError as err:
            logging.error(err.args)

@staticmethod
def sendLog(interval, worker_functions, iterations=1):
    def call_worker_functions():
        for f in worker_functions:
            f() #ERROR: Msg: 'NoneType' object is not callable
    for i in range(iterations):
        threading.Timer(interval * i, call_worker_functions).start()
我想用这行代码调用这个方法:

try:
    AccessLog.AccessLog.sendLog(
    interval=10,
    worker_functions=(
        AccessLog.AccessLog.backupAccessLog("logbackups","example.log"),
        AccessLog.AccessLog.emptyAccessLog("example.log"),
        AccessLog.AccessLog.postLogstoElastic()
    ),
    iterations=999
)
except ValueError as err:
    logging.error(err.args)

“TypeError:sendLog()至少接受5个参数(给定0)”这看起来很正常,但我如何处理它?

您是否尝试将@staticmethod设置为与函数相同的级别?

显然,您希望
sendLog()
每隔10秒左右调用一次辅助函数。 下面是一个简单的方法:

class AccessLog:
    @staticmethod
    def sendLog(interval, worker_functions, iterations=1):
        def call_worker_functions():
            for f in worker_functions:
                f(*worker_functions[f])
        for i in range(iterations):
            threading.Timer(interval * i, call_worker_functions).start()
现在就这样使用它:

AccessLog.AccessLog.sendLog(
    interval=10,
    worker_functions={
        AccessLog.AccessLog.backupAccessLog: ("logbackups", "example.log"),
        AccessLog.AccessLog.emptyAccessLog: ("example.log",),
        AccessLog.AccessLog.postLogstoElastic: ()
    ),
    iterations=999
)

这只是许多方法中的一种,但不需要像您那样将函数作为其自身的参数传递。

您可以发布完整的回溯,以便我们知道缩进错误发生在哪一行吗?以及代码中的哪一行。还有,为什么要使用分号?这些在PythonAlso中是完全不必要的。所以,您使用的是Python,而不是POS语言。不要用分号结束你的行-它们是无用的噪音。将你的装饰器缩进到与函数相同的级别。@ArtOfWarfare我做到了,即使这对我来说是更好的:)谢谢你,但不是主要问题:(@MehmetYenerYILMAZ)如果您可以更新您的问题,使其包含尽可能少的代码,您是否可以在一个类中仅使用一个方法重新创建相同的问题,并且不涉及django?装饰器必须与它所装饰的函数具有相同的缩进。在代码示例中,情况并非如此。这应该由goo标记d编辑器。但这可能是他的问题。这肯定是问题所在。如果它们在同一级别上,你会得到相同的错误吗?是的,它们已经相同了。我如何将此函数本身作为参数传递?实际上,这是我的问题:)好的,我将进入这个问题。我编辑问题,在for循环中有一个名为f()的错误行“'NoneType'对象不可调用”消息…为什么它需要类型,尽管它们都是static@MehmetYenerYILMAZ尝试更新版本;您必须将函数传递给
sendLog()
函数,我们正在传递函数调用(=它们的返回值)