0
I wrote a little program to send a message on my father’s Whatsapp to remind him to take his meds. Below I present the code that can be replicated by anyone with a mobile number and a record in the twilio API. in addition to the spreadsheet medications.csv
(see below)
My problem is: I want my Father to receive a single message for every remedy in the database medications.csv
, but I don’t know how to specify it. A hackish Solution What I found was to specify the exact second the message should be sent. However, I believe that there should be a better solution since if the program runs on a PC faster than mine it is likely that the logical test and the loop run at a speed of more than half a second, in which case my Father would receive n identical messages (where n is the number of complete operations per second).
How do I run this algorithm only once independent of hardware capability?
from twilio.rest import Client
import pandas as pd
from datetime import datetime
import re
from time import sleep
account_sid = '' #insira seu sid aqui
auth_token = '' #insira seu token aqui
client = Client(account_sid, auth_token)
def to_list_integer(dt_time):
return [dt_time.year,dt_time.month,dt_time.day, dt_time.hour,dt_time.minute]
df=pd.read_csv('medications.csv')
while True:
data_lista = to_list_integer(datetime.now())
x=datetime(*data_lista)
hora = x.strftime("%Hh%M")
for i in range(len(df)):
if (datetime.now().hour==df['hour'][i]) & (datetime.now().minute==5) & (datetime.now().second==1):
text = '''*Lembrete: {0}*\n
Olá sr(a). Fulano, agora são {1},
\nestá na hora de tomar {2} comprimido(s) de {0}
\n\n*recomendação médica: {3}*'''.format(df['name'][i],hora,re.search(r'\d+',df['recomendation'][i]).group(),
df['recomendation'][i])
for person in ['whatsapp:+55XXXXXXXXXXX','whatsapp:+XXXXXXXXXXX']:#insira o seu número e do familiar aqui. Oseu é apenas pra controlar
message = client.messages.create(
body=text,
from_='whatsapp:+XXXXXXXXXXX',#insira seu número twilio aqui
to=person
)
print(message.sid)
medications.csv
:
name,dosage,recomendation,hour,minute
remédio 1,100mg,Tomar 1 comprimido pela manhã,6,9
remédio 1 Plus,20/12.5 mg,Tomar 1 comprimido pela manhã,6,10
remédio 1 XR,500mg,Tomar 2 comprimidos após o café e após a última refeição da noite,7,10
remédio 2,75mg,Tomar 1 comprimido após o almoço,16,5
remédio 3,100mg,Tomar 1 comprimido após o almoço,16,6
remédio 1,10mg,Tomar 2 comprimidos à noite,20,10
remédio 1 XR,500mg,Tomar 2 comprimidos após o café e após a última refeição da noite,20,11
Rosuvastatina,20mg,Tomar 1 comprimido após a última refeição da noite,20,12
P.S: It was hard to summarize my doubt in the question’s title. I accept suggestions to make it clearer and more objective
Interesting. Soon more I will look calmly your solution. On your last point, I initially used the
time.sleep()
, but it comes into the same problem: fix the time, delay or run, does not guarantee that different computers do the same number of operations within thefor loop
– Lucas
The number of operations within a loop is determined by the code within the loop. The runtime that is set by the hardware. Any computer runs that loop in milliseconds or less. Also I don’t see why you think that running time is something critical to your program. If you look at the code you will see that the shot happens when 'now()' is longer than the time of the shot, if you use without delay, the shot occurs only milliseconds dps that passed the time when I think it solves your problem well. And you can guarantee that the shot is unique by the control variable.
– Thales Almeida
In the case of my
for loop
the number of operations depends on how many operations the PC does while the conditional remains true. To better understand what I mean, run my code without specifying the seconds. As the condition ofif (datetime.now().hour==df['hour'][i]) & (datetime.now().minute==5)
will be true for an entire minute, the PC will perform the operation within thefor loop
a numbern
sometimes, but thisn
will depend on the computer’s processing capacity. Note that specify thesegundos
does not solve the problem, only decreases the average ofn
.– Lucas
thank you so much for your help. Unfortunately, I was not clear enough. I think the best thing to do is to delete this question and ask another one with a minimal replicable example and present what I am thinking abstractly with some simple equations. Soon I will
– Lucas
Now I understand what you mean. Precisely because of this problem of fixing an equal hour. That I suggested a control structure with inequality > and a boolean control variable to ensure single firing.
– Thales Almeida
If you are interested, see the discussion of the question I asked in the American stack: https://stackoverflow.com/questions/60555797/how-can-i-make-the-number-of-runs-of-an-infinite-algorithm-fixed-regardless-of-m. I tried to do here, but they closed
– Lucas