How to constantly update time variables in a Python Script? - python

I have an if statement below that I want to execute at exactly 11:45 am every single day. The problem is, when I run my Python script, result.tm_min and result.tm_hour are static, holding whatever time it was when I started the script in th first place. I need some way for these values to change in real-time with the clock. So when the time changes from 11:44 to 11:45, result.tm_min also changes from 44 to 45, allowing for the below if statement to execute. If I could get any help with this, that would be awesome.
I'm currently using the time and datetime libraries for this.
if result.tm_hour == 11:
if result.tm_min == 45:
post_number = random.randint(1, 5)
noun_number = random.randint(1, noun_expand_count)
verb_number = random.randint(1, verb_expand_count)
noun_file = open("nouns.txt", "r")
get_noun_line = noun_file.readlines()
new_noun = get_noun_line[noun_number].strip()
noun_file.close()
verb_file = open("verbs.txt", "r")
get_verb_line = verb_file.readlines()
new_verb = get_verb_line[verb_number].strip()
verb_file.close()
post_file = open("things_to_do.txt", "r")
get_post_line = post_file.readlines()
new_post = get_post_line[post_number].strip()
post_file.close
message = "#joerogan Hello Joe, today's top two priorities are to:", new_post, new_verb, new_noun
print(message)
#api.update_status(message)
Edit: Okay, I did a pip install for the schedule module, tried to rewrite some code, but I'm not getting any output, at all.
def post():
post_number = random.randint(1, 5)
noun_number = random.randint(1, noun_expand_count)
verb_number = random.randint(1, verb_expand_count)
noun_file = open("nouns.txt", "r")
get_noun_line = noun_file.readlines()
new_noun = get_noun_line[noun_number].strip()
noun_file.close()
verb_file = open("verbs.txt", "r")
get_verb_line = verb_file.readlines()
new_verb = get_verb_line[verb_number].strip()
verb_file.close()
post_file = open("things_to_do.txt", "r")
get_post_line = post_file.readlines()
new_post = get_post_line[post_number].strip()
post_file.close
message = "#joerogan Hello Joe, today's top two priorities are to:", new_post, new_verb, new_noun
print(message)
#api.update_status(message)
return
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
global noun_expand_count, verb_expand_count
status = status._json['text']
schedule.every().minute.do(post)

Recalculate the current time immediately before checking:
current = datetime.now()
if current.hour == 11 and current.minute == 45:
# annoy Joe Rogan
However, as others have commented, it might be better to use a purpose-built task scheduling system such as cron.

Related

Edit a variable from list (Cannot use dictionary)

a = 10
b = 20
ls = [a, b]
How do I change the variable a to e.g. 20 using ls[0] as a parameter?
I remember there is some way to write code where you could input string and variables and it would turn into a line of code and run it.
This is how I faintly remember it, something like:
run(ls[0], "= 20")
Showing the whole code, instead of a condensed version, what I am trying to do is load and save variables from a text file. (Code is for a terminal game about making drugs.)
money = 11000
growtime = 45
growreward = 50
gramsweed = 0
growing = False
pots = 1
seeds = 0
cooktime = 90
cookreward = 20
gramsmeth = 0
cooking = False
tanks = 0
methlitres = 0
weedselling = 0
methselling = 0
currentlyselling = False
dealers = 1
dealtime = 120
dealamount = 15
stats = [money, growtime, growreward, gramsweed, growing, pots, seeds, cooktime,
cookreward, gramsmeth, cooking, tanks, methlitres, weedselling, methselling,
currentlyselling, dealers, dealtime, dealamount]
boolstats = [growing, cooking, currentlyselling]
def save():
f = open("save.txt", "w")
for stat in stats:
f.write(str(stat) + "\n")
f.close()
mainmenu()
def load():
i = 0
f = open("save.txt", "r")
for stat in stats:
print(stat)
stats[i] = f.readline(i)
i += 1
j = 0
for stat in boolstats:
if stat == "False": boolstats[j] = False
else: boolstats[j] = True
j += 1
f.close()
mainmenu()
Here's an example of how to store your game data in a dict and use the json module to easily save/load it. I'm going to start by just putting all of your variable declarations into a dict (note that I would normally use the {} syntax instead of the dict() constructor, but I'm copying and pasting your existing code and this makes it slightly easier):
stats = dict(
money = 11000,
growtime = 45,
growreward = 50,
gramsweed = 0,
growing = False,
pots = 1,
seeds = 0,
cooktime = 90,
cookreward = 20,
gramsmeth = 0,
cooking = False,
tanks = 0,
methlitres = 0,
weedselling = 0,
methselling = 0,
currentlyselling = False,
dealers = 1,
dealtime = 120,
dealamount = 15,
)
and now I can write your save and load functions in just a couple lines of code each:
import json
def save():
with open("save.txt", "w") as f:
json.dump(stats, f)
def load():
with open("save.txt") as f:
stats.update(json.load(f))
The json module takes care of reading the lines, parsing them, converting them to the right types, all of it, because it can just pull all the information it needs right out of the dictionary object. You can't get that same type of convenience and flexibility if you have a dozen different individual variables.
To suggest the ways you'd convert the other pieces of your game code to use a dict instead of individual variables I'd need to see that code, but hopefully this one example helps convince you that life can be much easier if you don't need to deal with variables one at a time!
It will not work as you desire since primitive types are passed by value. You can try:
globals()['a'] = 20 # works for global variables only, not local
or store your value in class variable, then it list will keep a reference to it.

Function with a Cooldown

Hello i have a Person Detector script i want to send an info if any person detected by mail.In order to prevent mail spamming i need a timer for sendMail function.Function might be triggered anytime but it will only respond if its not on cooldown.
I tried using async task but couldn't implemented because if a person detected it goes to a loop where it sends email every 5 minutes even if there isn’t any person detected after the first sight.
Example:
Person detection script is running.
Person detected on camera -> Send an email(start the 5 minute cooldown)
Person sighted again after 2 minutes(didn't send any email because there is still 3 minutes cooldown).
Person sighted after 6 minutes send another email(because 5 minute cooldown is over).
Summary of my code.(Necessary parts only detection and sending mail works cooldown (timer) doesn't work
async def sendAlert():
server.sendmail(sender_email, receiver_email, message)
print('sent!')
await asyncio.sleep(300)
if __name__ == "__main__":
while True:
for i in range(len(boxes)):
if classes[i] == 1 and scores[i] > threshold:
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
sendAlert(server)
box = boxes[i]
cv2.rectangle(img,(box[1],box[0]),(box[3],box[2]),(255,0,0),2)
If there is a person detected, script will send an alert by email.Afterwards if a person detected again in 5 minutes sendAlert function shouldn't respond until 5 minutes passed
I agree with #Prune that you need to create a small (minimal) use-case and present your code so that it is not only relevant to you, but also relevant to others. Additionally, your question should have a section with a verifiable example. Without these attributes, your question becomes hard for people to grasp, solve and/or suggest any verifiable solution.
However, as I understand, you have some action (sending email if a person is detected) that you would like to perform after certain cool-off period. So, in other words, you want a mechanism of keeping track of time. Hence, you would need the datetime library.
So, your pseudo code should look something like this:
Pseudo Code
import datetime
start = capture_timestamp()
cutoff = '00:05:00'
dt_cutoff = read_in_cutoff_as_timedelta(cutoff)
if person_detected:
now = capture_timestamp()
dt = now - start
if dt >= dt_cutoff:
# Send notification
send_email_notification()
else:
# Do not Send Notification
print('now: {} | dt: {}'.format(now, dt))
You could use datetime.datetime.utcnow() for timestamp. And datetime.timedelta() for defining dt_cutoff. For reading in a time-string as time you could do this:
tm = datetime.datetime.strptime(cutoff).time()
dt_cutoff = datetime.timedelta(hours = tm.hour, minutes = tm.minute, seconds = tm.second)
I hope this gives you some idea about how to model this.
Additional Resources
https://www.guru99.com/date-time-and-datetime-classes-in-python.html
https://docs.python.org/3/library/datetime.html
https://thispointer.com/python-how-to-convert-a-timestamp-string-to-a-datetime-object-using-datetime-strptime/
Complete Solution
Now, finally if you are in a hurry to use a ready made solution, you may use the following class object as shown. All you would need is to instantiate the class object by specifying your cool-off-period (timer_cutoff) and then call the method is_timeout(). If this returns True, then you send your notification. There is also an obj.istimeout attribute which stores this decision (True/False).
import time
# Set cutoff time to 2 seconds to test the output
# after 5 seconds: expect istimeout = True
# and instantiate the TimeTracker class object.
ttk = TimeTracker(timer_cutoff = '00:00:02') # 'HH:MM:SS'
# Wait for 3 seconds
time.sleep(3)
print('start timestamp: {}'.format(ttk.timestamp_start_str))
print('cutoff timestamp'.format(ttk.timestamp_cutoff_str))
print('timer_cutoff: {}'.format(ttk.timer_cutoff_str))
# Now check if cutoff time reached
ttk.is_timeout()
print('Send Notification: {}'.format(ttk.istimeout))
print('now_timestamp: {}'.format(ttk.timestamp_now_str))
class TimeTracker
Here is the class TimeTracker class:
import datetime
class TimeTracker(object):
def __init__(self,
timer_cutoff = '00:05:00',
cutoff_strformat = '%H:%M:%S'):
self.timer_cutoff_str = timer_cutoff
self.cutoff_strformat = cutoff_strformat
self.timestamp_start, self.timestamp_start_str = self.get_timestamp()
self.dt_cutoff = None # timedelta for cutoff
self.timestamp_cutoff = None
self.timestamp_cutoff_str = None
self.update_timestamp_cutoff()
self.timestamp_now = None
self.timestamp_now_str = None
self.dt_elapsed = None
self.istimeout = False
def get_timestamp(self):
ts = datetime.datetime.utcnow()
tss = str(ts)
return (ts, tss)
def readin_cutoff_as_timedelta(self):
td = datetime.datetime.strptime(self.timer_cutoff_str,
self.cutoff_strformat)
tdm = td.time()
self.dt_cutoff = datetime.timedelta(hours = tdm.hour,
minutes = tdm.minute,
seconds = tdm.second)
def update_timestamp_cutoff(self):
self.readin_cutoff_as_timedelta()
self.timestamp_cutoff = self.timestamp_start + self.dt_cutoff
self.timestamp_cutoff_str = str(self.timestamp_cutoff)
def time_elapsed(self):
self.dt_elapsed = self.timestamp_now - self.timestamp_start
def is_timeout(self):
self.timestamp_now, self.timestamp_now_str = self.get_timestamp()
self.time_elapsed()
if (self.dt_elapsed < self.dt_cutoff):
self.istimeout = False
else:
self.istimeout = True
return self.istimeout

Manage Python Multiprocessing with MongoDB

I'm trying to run my code with a multiprocessing function but mongo keep returning
"MongoClient opened before fork. Create MongoClient with
connect=False, or create client after forking."
I really doesn't understand how i can adapt my code to this.
Basically the structure is:
db = MongoClient().database
db.authenticate('user', 'password', mechanism='SCRAM-SHA-1')
collectionW = db['words']
collectionT = db['sinMemo']
collectionL = db['sinLogic']
def findW(word):
rows = collectionw.find({"word": word})
ind = 0
for row in rows:
ind += 1
id = row["_id"]
if ind == 0:
a = ind
else:
a = id
return a
def trainAI(stri):
...
if findW(word) == 0:
_id = db['words'].insert(
{"_id": getNextSequence(db.counters, "nodeid"), "word": word})
story = _id
else:
story = findW(word)
...
def train(index):
# searching progress
progFile = "./train/progress{0}.txt".format(index)
trainFile = "./train/small_file_{0}".format(index)
if os.path.exists(progFile):
f = open(progFile, "r")
ind = f.read().strip()
if ind != "":
pprint(ind)
i = int(ind)
else:
pprint("No progress saved or progress lost!")
i = 0
f.close()
else:
i = 0
#get the number of line of the file
rangeC = rawbigcount(trainFile)
#fix unicode
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
files = io.open(trainFile, "r", encoding="utf8")
str1 = ""
str2 = ""
filex = open(progFile, "w")
with progressbar.ProgressBar(max_value=rangeC) as bar:
for line in files:
line = line.replace("\n", "")
if i % 2 == 0:
str1 = line.translate(non_bmp_map)
else:
str2 = line.translate(non_bmp_map)
bar.update(i)
trainAI(str1 + " " + str2)
filex.seek(0)
filex.truncate()
filex.write(str(i))
i += 1
#multiprocessing function
maxProcess = 3
def f(l, i):
l.acquire()
train(i + 1)
l.release()
if __name__ == '__main__':
lock = Lock()
for num in range(maxProcess):
pprint("start " + str(num))
Process(target=f, args=(lock, num)).start()
This code is made for reading 4 different file in 4 different process and at the same time insert the data in the database.
I copied only part of the code for make you understand the structure of it.
I've tried to add connect=False to this code but nothing...
db = MongoClient(connect=False).database
db.authenticate('user', 'password', mechanism='SCRAM-SHA-1')
collectionW = db['words']
collectionT = db['sinMemo']
collectionL = db['sinLogic']
then i've tried to move it in the f function (right before train() but what i get is that the program doesn't find collectionW,collectionT and collectionL.
I'm not very expert of python or mongodb so i hope that this is not a silly question.
The code is running under Ubuntu 16.04.2 with python 2.7.12
db.authenticate will have to connect to mongo server and it will try to make a connection. So, even though connect=False is being used, db.authenticate will require a connection to be open.
Why don't you create the mongo client instance after fork? That's look like the easiest solution.
Since db.authenticate must open the MongoClient and connect to the server, it creates connections which won't work in the forked subprocess. Hence, the error message. Try this instead:
db = MongoClient('mongodb://user:password#localhost', connect=False).database
Also, delete the Lock l. Acquiring a lock in one subprocess has no effect on other subprocesses.
Here is how I did it for my problem:
import pathos.pools as pp
import time
import db_access
class MultiprocessingTest(object):
def __init__(self):
pass
def test_mp(self):
data = [[form,'form_number','client_id'] for form in range(5000)]
pool = pp.ProcessPool(4)
pool.map(db_access.insertData, data)
if __name__ == '__main__':
time_i = time.time()
mp = MultiprocessingTest()
mp.test_mp()
time_f = time.time()
print 'Time Taken: ', time_f - time_i
Here is db_access.py:
from pymongo import MongoClient
def insertData(form):
client = MongoClient()
db = client['TEST_001']
db.initialization.insert({
"form": form[0],
"form_number": form[1],
"client_id": form[2]
})
This is happening to your code because you are initiating MongoCLient() once for all the sub-processes. MongoClient is not fork safe. So, initiating inside each function works and let me know if there are other solutions.

Why do only the first three lines come up in my .txt file -?! Raspberry Pi - temperature humidity readings

I am a completely inexperienced A level student trying to get to grips with python to complete an assignment. I have been given a week to complete it - I have very little knowledge of what to do and have no experience with coding - I am truly stuck and will probably seem very stupid to people on his forum.
I have to create a temperature and humidity logger with a raspberry pi and DHT22 sensor. I am to write a script that produces a loop that sleeps for 10 seconds - I will run the script for two days to collect enough data to produce graphs. So far the code I have is this and it's not working - probably for some obvious reasons. The data needs to come out in two columns in a leafpad file. Nothing seems to be happening when I sudo python execute the script - no .txt file has been created in my ls (there is one with just this in it:
indoors
51.58778
-0.15944
But no error message in the LX terminal.. Am I doing something very obviously wrong?
# Assign header details to STRING variables - change manually
txt_studentid = '999'
txt_pi_location = 'indoors'
txt_pi_latitude = '51.58778'
txt_pi_longitude = '-0.15944'
import Adafruit_DHT
pin = 4
sensor = Adafruit_DHT.DHT22
# Import Time module import time
# open file to write
f = open('/home/pi/my_data.txt','w')
f.write(txt_studentid)
f.write('\n')
f.write(txt_pi_location)
f.write('\n')
f.write(txt_pi_latitude)
f.write('\n')
f.write(txt_pi_longitude)
f.write('\n')
f.close()
while True:
# store off the date and time details for this
sample num_month = time.localtime().tm_mon
num_day = time.localtime().tm_mday
num_hour = time.localtime().tm_hour
num_min = time.localtime().tm_min
num_sec = time.localtime().tm_sec
num_humidity, num_temperature = Adafruit_DHT.read_retry(sensor, pin)
txt_month = str(num_month)
txt_day = str(num_day)
txt_hour = str(num_hour)
txt_min = str(num_min)
txt_sec = str(num_sec)
txt_humidity = str(num_humidity)
txt_temperature = str(num_temperature)
f = open('/home/pi/my_data.txt','a')
f.write(txt_month)
f.write(',')
f.write(txt_day)
f.write(',')
f.write(txt_hour)
f.write(',')
f.write(txt_min)
f.write(',')
f.write(txt_sec)
f.write(',')
# write the temperature and humidity to file
f,write(txt_humidity)
f.write(',')
f,write(txt_temperature)
f.write(',')
# write new line
f.write('\n')
# close the file
f.close()
# wait for ten seconds
time.sleep(10)
You definitely want to at least include the file writing in the while loop; or keep track somehow of the readings for later saving.
I've modified your code to help you get started:
import Adafruit_DHT
import time
from datetime import datetime
pin = 4
sensor = Adafruit_DHT.DHT22
# Import Time module import time
# open file to write
f = open('/home/pi/my_data.txt','w')
f.write(txt_studentid)
f.write('\n')
f.write(txt_pi_location)
f.write('\n')
f.write(txt_pi_latitude)
f.write('\n')
f.write(txt_pi_longitude)
f.write('\n')
f.close()
f = open('/home/pi/my_data.txt','a')
begintime = datetime.now()
while True:
# store off the date and time details for this
sample_time = datetime.now()
sample num_month = time.localtime().tm_mon
num_day = time.localtime().tm_mday
num_hour = time.localtime().tm_hour
num_min = time.localtime().tm_min
num_sec = time.localtime().tm_sec
num_humidity, num_temperature = Adafruit_DHT.read_retry(sensor, pin)
txt_month = str(num_month)
txt_day = str(num_day)
txt_hour = str(num_hour)
txt_min = str(num_min)
txt_sec = str(num_sec)
txt_humidity = str(num_humidity)
txt_temperature = str(num_temperature)
f.write(txt_month)
f.write(',')
f.write(txt_day)
f.write(',')
f.write(txt_hour)
f.write(',')
f.write(txt_humidity)
f.write(',')
f.write(num_temperature)
f.write('\n')
time.sleep(10) #sleep for 10 seconds
timedelta = sample_time - begintime
if timedelta.days >= 2:
break
f.close()
I would try setting the timedelta requirement to something like 30 seconds to make sure it works as expected before going up to 2 days. You can do that by changing if timedelta.days >= 2: to if timedelta.seconds >= 30:
I guess you indentation is wrong. You'll get stuck in the while loop and will never write anything to the file. Try indenting everything from num_month = time.localtime().tm_mon to time.sleep(10)

Python multiprocessing sharedctype array - cant write

for testing reasons I start only 1 process. One given argument is an array that shall be changed from that process.
class Engine():
Ready = Value('i', False)
def movelisttoctypemovelist(self, movelist):
ctML = []
for zug in movelist:
ctZug = ctypeZug()
ctZug.VonReihe = zug.VonReihe
ctZug.VonLinie = zug.VonLinie
ctZug.NachReihe = zug.NachReihe
ctZug.NachLinie = zug.NachLinie
ctZug.Bewertung = zug.Bewertung
ctML.append(ctZug)
return ctML
def findbestmove(self, board, settings, enginesettings):
print ("Computer using", multiprocessing.cpu_count(),"Cores.")
movelist = Array(ctypeZug, [], lock = True)
movelist = self.movelisttoctypemovelist(board.movelist)
bd = board.boardtodictionary()
process = []
for i in range(1):
p = Process(target=self.calculatenullmoves, args=(bd, movelist, i, self.Ready))
process.append(p)
p.start()
for p in process:
p.join()
self.printctypemovelist(movelist, settings)
print ("Ready:", self.Ready.value)
def calculatenullmoves(self, boarddictionary, ml, processindex, ready):
currenttime = time()
print ("Process", processindex, "begins to work...")
board = Board()
board.dictionarytoboard(boarddictionary)
...
ml[processindex].Bewertung = 2.4
ready.value = True
print ("Process", processindex, "finished work in", time()-currenttime, "sec")
def printctypemovelist(self, ml):
for zug in ml:
print (zug.VonReihe, zug.VonLinie, zug.NachReihe, zug.NachLinie, zug.Bewertung)
I try to write 2.4 directly in the list, but no changing is shown when calling "printctypemovelist".
I set "Ready" to True and it works.
I used information from http://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.sharedctypes
I hope someone can find my mistake, if it is too difficult to read, please let me know.
The problem is that you're trying to share a plain Python list:
ctML = []
Use a proxy object instead:
from multiprocessing import Manager
ctML = Manager().list()
See Python doc on Sharing state between processes for more detail.

Categories

Resources