I have a function to do some work. The code should repeat based on schedule. I used Croniter module. the schedule should be in cron type syntax. It works with every minutes. But it doesn't work with seconds. I added seconds at last in schedule but it says croniter syntax is not acceptable. How do I add seconds in syntax to make it work for seconds also?
Here is my code.
import yaml
from croniter import croniter
from datetime import datetime, timedelta
import time
def fun():
for i in items:
print (i)
def roundDownTime(dt=None, dateDelta=timedelta(minutes=1)):
roundTo = dateDelta.total_seconds()
if dt == None : dt = datetime.now()
seconds = (dt - dt.min).seconds
rounding = (seconds+roundTo/2) // roundTo * roundTo
return dt + timedelta(0,rounding-seconds,-dt.microsecond)
def getNextCronRunTime(schedule):
return croniter(schedule, datetime.now()).get_next(datetime)
def sleepTillTopOfNextMinute():
t = datetime.utcnow()
sleeptime = 60 - (t.second + t.microsecond/1000000.0)
time.sleep(sleeptime)
items =[2,4,6,7]
schedule = "*/1 * 7 2 * "
nextRunTime = getNextCronRunTime(schedule)
while True:
roundedDownTime = roundDownTime()
if (roundedDownTime == nextRunTime):
fun()
nextRunTime = getNextCronRunTime(schedule)
elif (roundedDownTime > nextRunTime):
# We missed an execution. Error. Re initialize.
nextRunTime = getNextCronRunTime(schedule)
sleepTillTopOfNextMinute()
Related
I wrote a program that will close the hard drive with BitLocker in Windows by using libraries datetime and subprocess but I cannot get the correct result.
Here is my code:
import subprocess
import datetime
now = datetime.datetime.today()
delta = datetime.timedelta(hours=2)
time_delta = now + delta
# print(time_delta)
# print(now)
try:
if now == time_delta:
close = subprocess.check_output(['manage-bde', '-lock', 'D:', '-ForceDismount'])
close.decode('utf-8').split('\n')
print('done')
except Exception:
pass
That is because you are checking the current time now = datetime.datetime.today(), then add 2 hours to it and immediately after check, if time_delta is equal to now.
Thus the if statement always results in false and the code in it does not get executed.
You need to put something in place to wait. Something like that.
import subprocess
import datetime
import time
now = datetime.datetime.today()
delta = datetime.timedelta(hours=2)
time_delta = now + delta
while now != time_delta:
time.sleep(1) # wait for a second
now = datetime.datetime.today()
# when now == time_delta we continue here:
try:
close = subprocess.check_output(['manage-bde', '-lock', 'D:', '-ForceDismount'])
close.decode('utf-8').split('\n')
print('done')
except Exception:
pass
This is not the best way to schedule a task, but it should be working.
When I measure the time manually, it is less than the time that I got through this script:
import time import os
def getTimes():
try:
times = []
if(exists("1472205483589.png",60)):
click("1472192774056.png")
wait("1472040968178.png",10)
click("1472036591623.png")
click("1472036834091.png")
click("1472036868986.png")
if(exists("1472192829443.png",5)):
click("1472192829443.png")
u = time.time()
click("1472539655695.png")
wait("1472042542247.png",120)
v = time.time()
print("Open File to when views list appear  (sec) : " , int(v-u))
times.append(int(v-u))
u = time.time()
click("1472042542247.png")
wait("1472108424071.png",120)
mouseMove("1472108424071.png")
wait("1472108486171.png",120)
v = time.time()
print("Opening view (sec) : ",int(v-u))
times.append(int(v-u))
u = time.time()
click("1472109163884.png")
wait("1472042181291.png",120)
v = time.time()
print("Clicking element (sec) : ", float(v-u))
times.append(int(v-u))
return times
except FindFailed as ex:
print("Failed. Navigator might have stopped working")
if(exists("1472204045678.png",10)):
click("1472204045678.png")
return -1
file = open(r"C:\BSW\SikulixScripts\NavigatorAutoTesting\log.txt",'w') ret = getTimes() if (ret == -1):
file.write("-1")
exit() str = " ".join(str(x) for x in ret) file.write(str) file.close()
By using time.time(), you are actually returning a number of seconds--the difference between "the epoch" and now. (The epoch is the same as gmtime(0)). Instead, try using datetime.now() which will give you a datetime object. You can add and subtract datetime objects freely, resulting in a timedelta object as per the Python docs
u = datetime.now()
click("1472539655695.png")
wait("1472042542247.png",120)
v = datetime.now()
tdelta = v-u
seconds = tdelta.total_seconds() #if you want the number of seconds as a floating point number... (available in Python 2.7 and up)
times.append(seconds)
This should yield more accuracy for you.
I am having trouble with the following scenario using python schedule module. Essentially I want to run a login event at time A, and then run the action at time B.
The code does not run as the intended behaviour describes and this is where I need help.
import sched
import datetime
today = datetime.datetime.today()
log = today.replace(hour=11, minute=59, second = 0)
action= today.replace(hour=12, minute=0, second = 0)
scheduler = sched.scheduler(datetime.datetime.today(), time.sleep)
def login_event(name):
print 'EVENT:', datetime.datetime.today(), name
def action_event(name):
print 'EVENT:' datetime.datetime.today(),name
print 'START:', time.time()
scheduler.enter(log, login_event, ('Login'))
scheduler.enter(action, login_event, ('Action'))
scheduler.run()
EDIT I have altered the code to the following but it still doesn't seem right in terms of how best to implement this behaviour.
import sched
import datetime
from datetime import timedelta
import datetime
import time
today = datetime.datetime.today()
log = datetime.datetime.now() + timedelta(minutes=1)# today.replace(hour=12, minute=46, second = 0)
action= log + timedelta(minutes=2)
scheduler = sched.scheduler(time.time, time.sleep)
print datetime.datetime.now
def login_event(name):
print 'Login:', datetime.datetime.now(), name
def action_event(name):
print 'Action:', datetime.datetime.now(), name
print 'Start:', datetime.datetime.now()
scheduler.enter(1, 1, login_event, ('first',))
scheduler.enter(60, 1, action_event, ('second',))
scheduler.run()
The following code hasn't been tested but should be work.
I've put your original code into comment so you can see where you got wrong.
You will probably need to refer the doc: https://docs.python.org/2/library/sched.html
import sched, time
import datetime
today = datetime.datetime.today()
log = today.replace(hour=11, minute=59, second = 0)
action= today.replace(hour=12, minute=0, second = 0)
#scheduler = sched.scheduler(datetime.datetime.today(), time.sleep)
#The first argument of sched.scheduler should be a function that return a number.
scheduler = sched.scheduler(time.time, time.sleep)
def login_event(name):
print 'EVENT:', datetime.datetime.today(), name
def action_event(name):
print 'EVENT:', datetime.datetime.today(),name
print 'START:', time.time()
scheduler.enter is used for relative delay. The correct function to use is scheduler.enterabs
You will need a function to convert datetime to POSIX timestamp.
This can be tricky in python 2.x due to timezone issue.
Refer to this question: Convert datetime to Unix timestamp and convert it back in python
Also, the function takes 4 arguments.
#scheduler.enter(log, login_event, ('Login'))
#scheduler.enter(action, login_event, ('Action'))
scheduler.enterabs(timestamp(log), 0, login_event, ('Login'))
scheduler.enterabs(timestamp(action), 0, action_event, ('Action'))
scheduler.run()
https://github.com/dbader/schedule
By following the pattern linked above I was able to create the desired behaviour using a slightly different schedule module
import schedule
import time
def job():
print("I'm working on job one...")
def job2():
print("I'm working on job two..")
schedule.every().day.at("10:30").do(job)
schedule.every().day.at("10:35").do(job2)
while True:
schedule.run_pending()
time.sleep(1)
I have imported the following modules at the top of my python script:
import os
import sys
import time
import datetime
import random
import pprint
from random import randint
from time import sleep
from datetime import datetime, timedelta
But, I am still getting a module object error for this part of my code:
def check(low,high):
with open('done.txt', 'r+') as done:
ts = time.time()
sttime = datetime.fromtimestamp(ts).strftime('%Y%m%d_%H:%M:%S - ')
done_completed = open('done_completed.txt', "a")
for line in done:
now = datetime.now()
now_time = now.time()
if now_time >= time(23,30) and now_time <= time(06,30):
print "sleeping"
sleep(5000)
else:
done_id = line.strip()[20:]
then = datetime.strptime(line.strip()[:17], '%Y%m%d_%H:%M:%S')
(There's another if elif elif under all that but I figured it isn't relevant to the error)
(Yes, I am a python beginner)
The error is:
File "/home/joe/Desktop/follomatic/follomatic.py", line 85, in check
if now_time >= time(23,30) and now_time <= time(06,30):
TypeError: 'module' object is not callable
Does anyone see what is wrong? Have I not specified to call the module in the right way?
Thanks :)
Python's datetime module is a bit of a rat's nest. :) And it can get even more confusing if you use from imports.
This code should clarify things a bit. Note that this is not a great way to do things. It's generally better to work with datetime.datetime objects rather than datetime.time objects, otherwise things get messy if you need to handle time intervals that include midnight (or multiple days). But I wrote this code to roughly correspond with the code in your question, and I wanted to make it easy to test at any time of day.
#!/usr/bin/env python
''' datetime.time manipulation demo
From http://stackoverflow.com/q/28605732/4014959
Written by PM 2Ring 2015.02.19
'''
from time import sleep
import datetime
now = datetime.datetime.now()
now_time = now.time()
#A datetime.time example
t1 = datetime.time(5, 30)
print 't1 is', t1
now = datetime.datetime.now()
now_time = now.time()
print 'now is %s, now_time is %s' % (now, now_time)
begin_datetime = now + datetime.timedelta(seconds=5)
end_datetime = now + datetime.timedelta(seconds=10)
begin_time = begin_datetime.time()
end_time = end_datetime.time()
print 'begin', begin_time
print 'end', end_time
for i in xrange(15):
now_time = datetime.datetime.now().time()
print now_time, begin_time <= now_time <= end_time
sleep(1)
typical output
t1 is 05:30:00
now is 2015-02-19 23:44:39.152786, now_time is 23:44:39.152786
begin 23:44:44.152786
end 23:44:49.152786
23:44:39.152905 False
23:44:40.153999 False
23:44:41.155191 False
23:44:42.156398 False
23:44:43.156614 False
23:44:44.157810 True
23:44:45.159028 True
23:44:46.160231 True
23:44:47.161444 True
23:44:48.162660 True
23:44:49.163869 False
23:44:50.165076 False
23:44:51.166650 False
23:44:52.167842 False
23:44:53.169053 False
I'm trying to make a scheduler to run scripts at certain times.
The code below is my own version, but there is a problem with when the time gets to 23:59:59 and goes across to 00:00:01 it for some reason doesn't continue idling... instead it calls the callscripts() once and then it will go back to idling...
from datetime import date, timedelta
from sched import scheduler
from time import time, sleep, strftime
import random
s = scheduler(time, sleep)
random.seed()
def periodically(runtime, intsmall, intlarge, function):
## Get current time
currenttime = strftime('%H:%M:%S')
## If currenttime is anywhere between 23:40 and 23:50 then...
if currenttime > '23:40:00' and currenttime < '23:50:00':
## Call clear
clear()
## Update time
currenttime = strftime('%H:%M:%S')
## Idle time
while currenttime > '23:40:00' and currenttime < '23:59:59' or currenttime > '00:00:00' and currenttime < '01:30:00':
## Update time
currenttime = strftime('%H:%M:%S')
runtime += random.randrange(intsmall, intlarge)
s.enter(runtime, 1, function, ())
s.run()
def callscripts():
print "Calling Functions"
main1()
main2()
def main1():
print "Main1"
def main2():
print "Main2"
def clear():
print "Clearing"
while True:
periodically(2, -1, +1, callscripts)
Anyone know how this can be fixed or know a better way to do this?
Many thanks AEA
This is just a concept on how you could go about things more efficiently than using strings.
import time, calendar
# Get the current time in a localtime manner (we need it for the day and year)
today = time.localtime(time.time())
# We put the day, month and year into a strftime -> localtime converter
# giving us the localtime version of 00:00:00 of today (there's cleaner version for this process)
today_strftime = time.strptime(str(today.tm_mday) + " " + time.strftime("%b") + " " + str(today.tm_year)[-2:], "%d %b %y")
# Then we take that localtime of 00:00:00 and put it into timegm which gives us the unix
# timestamp of 00:00:00 today, which we can subtract from time.time() to give us how many
# seconds of today has passed since 00:00:00.. Which we can use to compare integers with eachother (more efficient, and easy to compare)
unixtime_beginning_of_day = calendar.timegm(today_strftime)
time_passed_today = time.time() - unixtime_beginning_of_day
if time_passed_today > 3600:
# 60sec per min * 60 minutes per hour == 3600 seconds in one hour.
print "One hour has passed since the start of this day"