TypeError: the first argument must be called

I am using python and planning a lib to create a cron-like job

class MyClass:

        def local(self, command):
                #return subprocess.call(command, shell=True)
                print "local"

        def sched_local(self, script_path, cron_definition):
                import schedule
                import time

                #job = self.local(script_path)

                schedule.every(1).minutes.do(self.local(script_path))

                while True:
                        schedule.run_pending()
                        time.sleep(1)

When calling this, basically

cg = MyClass()
cg.sched_local(script_path, cron_definition)

I got it:

local
Traceback (most recent call last):
  File "MyClass.py", line 131, in <module>
    cg.sched_local(script_path, cron_definition)
  File "MyClass.py", line 71, in sched_local
    schedule.every(1).minutes.do(self.local(script_path))
  File "/usr/local/lib/python2.7/dist-packages/schedule/__init__.py", line 271, in do
    self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable

When calling another method inside the class instead of sched_local, for example

def job(self):
    print "I am working"

The job works great.

+4
source share
1 answer

do expects the callee and any arguments it takes.

Therefore, your call doshould look like this:

schedule.every(1).minutes.do(self.local, script_path)

An implementation docan be found here .

def do(self, job_func, *args, **kwargs):
    """Specifies the job_func that should be called every time the
    job runs.

    Any additional arguments are passed on to job_func when
    the job runs.
    """
    self.job_func = functools.partial(job_func, *args, **kwargs)
    functools.update_wrapper(self.job_func, job_func)
    self._schedule_next_run()
    return self
+7
source

All Articles