1

I try to call the get_registers function on a schedule. I get an error TypeError: the first argument must be callable.

import requests, time, datetime, json, schedule 


class Registration:

    url = 'http://127.0.0.1:8000/api/reg/'

    def get_registers(self):
        now = datetime.datetime.now()
        date = now.strftime("%d-%m-%Y")
        full_page = requests.get(self.url, auth=("admin","admin"))
        pars=json.loads(full_page.content.decode('utf-8'))
        a=sorted(pars, key=lambda pars: pars['time_visit'])
        count=0
        for i in a:
            if i['date_visit']==date:
                count +=1
                print(i["number_car"], i['time_visit'])


reg = Registration()
schedule.every(1).minutes.do(reg.get_registers())

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

I just can’t understand what I missed.

1 Answer 1

3
schedule.every(1).minutes.do(reg.get_registers())

Should be

schedule.every(1).minutes.do(lambda: reg.get_registers)

The difference is the following. In the first case, you schedule to execute reg.get_registers() result (which is None since you do not return anything from that method) every minute. In the second case, you schedule to execute reg.get_registers itself every minute.

Sign up to request clarification or add additional context in comments.

2 Comments

it's that simple ...Thanks for the clarification. If I return values, I will need to use schedule.every(1).minutes.do(reg.get_registers()) ?
@ВадимШаройкин most likely no. You need to use reg.get_registers() if you return a function from that method - in case you want to have really complex behaviour which should be altered in runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.