For with schedule in python

2

Good morning everyone! Can anyone help me if it is possible to create a for in Python that uses times?

Example:

  

Set a start time of for and end term and every 1 hour this for to run again.

from datetime import datetime
now = datetime.now()

atual = now.hour
hr_final = 10
i = 0

for i in range(int(hr_final - atual)):
    print (atual)
    
asked by anonymous 24.12.2018 / 13:11

3 answers

2

You can do this simply by using the sleep method of the time module

See an example code that runs every 1 hour.

from time import sleep
from datetime import datetime

hora = 3600

while True:
    print datetime.now()
    sleep(hora)

In the code above let's be the sleep method to "wait" for 3600 seconds of 1 hour

    
26.12.2018 / 11:01
3

I do not think it's possible to use a date with a date for the purpose you want.

Calculations, operations, and the like involving dada are famous for achieving a certain level of inherent 'complexity'.

I think you should increase the scope of the question and ask yourself:

  

'Is it possible to have a script run every 1 hour?'

There are several ways to do this, I think one of the simplest (I'll cite it as an example only for you to test) is using an appropriate library. The Twisted is an event-driven library that can be used to run code at time intervals:

from twisted.internet import reactor, task
from os.path import getsize

FILE = '/var/log/syslog'

def monitor(lastsize = [-1])
    size = getsize(FILE)
    if size <> lastsize[0]:
        print 'O tamanho do arquivo agora é %d kilobytes (%d bytes)' % \
              (round(size / 1024.0), size)
        lastsize[0] = size

if __name__ == '__main__'
    print 'Checando a cada um minuto o tamanho do arquivo "%s".' % FILE
    print 'Atenção: esse programa NUNCA termina.'
    l = task.LoopingCall(monitor)
    l.start(1.0)
    reactor.run()

This code, for example, checks the file size every 1 second. However the use of such a large and complex library just to use a simple NOT functionality is recommended, it would be like using a rocket to kill a mosquito.

For a solution deployed on its own, I strongly recommend that read this article posted in Python Brazil >. There are 3 examples of how to execute codes at predetermined intervals in a simple and well explained way.

I hope I have helped.

    
24.12.2018 / 14:29
0

So I understand you just want to increment the values, so use the object timedelta together with datetime to increase or decrease variables with date and time:

from datetime import datetime, timedelta

inicio = datetime.now()

for i in range(0,10):
    print inicio + timedelta(hours=i)

And get as a result:

2018-12-24 11:23:44.750000
2018-12-24 12:23:44.750000
2018-12-24 13:23:44.750000
2018-12-24 14:23:44.750000
2018-12-24 15:23:44.750000
2018-12-24 16:23:44.750000
2018-12-24 17:23:44.750000
2018-12-24 18:23:44.750000
2018-12-24 19:23:44.750000
2018-12-24 20:23:44.750000

You can use "seconds", "minutes", "days", "months" etc, alone or together to increment (or decrement) your datetime object.

    
24.12.2018 / 14:34