TypeError: 'datetime.datetime' object is not callable [closed]

1

Python code to send sms only when the difference date of the query is equal to 7 days from 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
    
asked by anonymous 12.12.2018 / 10:19

2 answers

1

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

Initial variable in datetime :

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

was replaced with a date variable:

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

This executes if with the condition that the difference between dates is equal to 7 days, otherwise it will not send sms.

    
12.12.2018 / 11:55
2

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

And responding to your comment, 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)

So you need to convert datetime before you 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 format the date correctly (see documentation of this data type to know exactly how to do it).

    
12.12.2018 / 10:42