Typeerror: 'datetime.datetime' Object is not callable

Asked

Viewed 493 times

1

Python code to send sms only when the difference date of the query is equal to 7 days of the date of the current day.

# -*- coding: utf-8 -*-
import MySQLdb
import pycurl
import base64
import json
import datetime

date_now = datetime.datetime.now()
seven_days_ago = date_now + datetime.timedelta(days=7)

db = MySQLdb.connect(host="xxx.xxx.x.xx", user="xxxx", passwd="xxxxxxx", db="xxxxxx")

cursor = db.cursor()

cursor.execute("SELECT DataConsulta, Dias, HoraConsulta, HoraSaida, nome, Consulta, centrodb.LocalConsulta.Descricao, Contato FROM centrodb.RegistoConsultas LEFT OUTER JOIN centrodb.LocalConsulta ON centrodb.LocalConsulta.Id = centrodb.RegistoConsultas.`Local` LEFT OUTER JOIN centrodb.utentes ON centrodb.utentes.codigoutente = centrodb.RegistoConsultas.Utente LEFT OUTER JOIN centrodb.DiasSemana ON centrodb.DiasSemana.Id = centrodb.RegistoConsultas.DiaSemana")

myresult = cursor.fetchall()

for linha in myresult:
 DataConsulta = linha[0]
 Dias = linha[1]
 HoraConsulta = linha[2]
 HoraSaida = linha[3]
 nome = linha[4]
 Consulta = linha[5]
 Descricao = linha[6]
 Contato = linha[7]

 if DataConsulta == seven_days_ago():
  if __name__ == "__main__":
   url ="https://dashboard.360nrs.com/api/rest/sms"
   usrPass = "xxxxxx:xxxxxxx"
   data = json.dumps({
   "to":[Contato],
   "from":"CPA",
   "message":"Teste ",
   })
   b64Val = base64.b64encode(usrPass)
   headers=["Accept:Application/json","Authorization:Basic %s"%b64Val]
   c = pycurl.Curl()
   c.setopt(pycurl.URL, url)
   c.setopt(pycurl.HTTPHEADER,headers)
   c.setopt(pycurl.POST, 1)
   c.setopt(pycurl.POSTFIELDS, data)
   c.setopt(pycurl.SSL_VERIFYHOST, 0)
   c.setopt(pycurl.SSL_VERIFYPEER, 0)
   c.perform()
   http_code = c.getinfo(pycurl.HTTP_CODE)
   print(http_code)

When I run the script on the terminal I get this error:

Traceback (most recent call last): 
  File "/var/www/html/wordpress/ensms.py", line 29, in <module>
    if DataConsulta == seven_days_ago(): 
TypeError: 'datetime.datetime' object is not callable
  • What type of variable DataConsulta?

  • @Anderson Carlos Woss the variable DataConsulta it’s kind date

  • If you should send when the date is superior 7 days ago, why did you use == instead of >?

  • @Anderson Carlos Woss because it is not when it is superior, but when the difference is equal to 7 days, so I used the ==. I edited the question

  • 2

    "to send sms only when the date of the consultation is more than 7 days", has written exactly that in your question. Remember that datetime, as the name suggests, takes into account the date and time. If now is 2018-12-12 08:16:30, seven more days will be 2018-12-19 08:16:30. If you don’t execute exactly that second, you won’t send the SMS.

  • @Anderson Carlos Woss then the variable I create in datetime, seven_days_ago, has to be created in type format date, right?

Show 1 more comment

2 answers

2

You are calling the variable seven_days_ago as if it were a function (hence the error saying that the object datetime.datetime cannot be called), remove the parentheses and it will work.

And responding to your comment, the datetime returns a specific object and not a string, as in the database query:

>>> from datetime import datetime, timedelta
>>> a = datetime.now()
>>> a
datetime.datetime(2018, 12, 12, 22, 17, 10, 280298)
>>> b = a + timedelta(days=7)
>>> b
datetime.datetime(2018, 12, 19, 22, 17, 10, 280298)

Therefore you need to convert the datetime before I can compare it:

>>> b.isoformat()
'2018-12-19T22:17:10.280298'
>>> b.strftime('%d-%m-%Y')
'19-12-2018'

In this case you will need to check the date format that the database is returning in order to be able to format the date correctly (see documentation of this type of data to know exactly how to do it).

  • the parentheses in the seven_days_ago if the date is 7 days apart, it does not send the text or return an error and if it is more or less than 7 days old, it also does not send or return an error. It may help to understand the problem?

  • You need to compare "equal things", see the complement I made to the answer.

2


Solution found. As Anderson Carlos Woss said in relation to the variable datetime, then flip this variable to a variable of the type date.

Initial variable in datetime:

date_now = datetime.datetime.now()
seven_days_ago = date_now + datetime.timedelta(days=7)

has been replaced by a variable date:

today = datetime.date.today()
data = today + datetime.timedelta(days=7)

Thus executes the if provided that the difference between dates is equal to 7 days, if not send the text.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.