battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

the LivinGrimoire Artificial General Intelligence software design pattern forum

description[python] [java] [swift] Cron job classes Empty[python] [java] [swift] Cron job classes

more_horiz
we have classes to schedule events. here is an example:

Code:

class TrgEveryNMinutes(TrGEV3):
    # trigger returns true every minutes interval, post start time
    def __init__(self, startTime: str, minutes: int):
        self._playGround: PlayGround = PlayGround()
        self._minutes:int = minutes  # minute interval between triggerings
        self._timeStamp = startTime
        self._trgTime: TrgTime = TrgTime()
        self._trgTime.setTime(startTime)

    def setMinutes(self, minutes: int):
        if minutes > -1:
            self._minutes = minutes

    # override
    def trigger(self) -> bool:
        if self._trgTime.alarm():
            self._timeStamp = self._playGround.getFutureInXMin(self._minutes)
            self._trgTime.setTime(self._timeStamp)
            return True
        return False

    # override
    def reset(self):
        self._timeStamp = self._playGround.getCurrentTimeStamp()


in the c'tor: set the initial cron activation(time stamp) and minute interval between activations.
when the time comes the trigger() method will return true.

this works cross platform and no external installations are needed.

Last edited by Moti Barski on Mon Aug 28, 2023 11:51 am; edited 2 times in total

description[python] [java] [swift] Cron job classes EmptyRe: [python] [java] [swift] Cron job classes

more_horiz
this Cron cls activates every n minutes after initial time activation(startTime in the c'tor) for a limited amount of times:


Code:

class Cron(TrGEV3):
    # triggers true, limit times, after initial time, and every minutes interval
    # counter resets at initial time, assuming trigger method was run
    def __init__(self, startTime: str, minutes: int, limit:int):
        self._playGround: PlayGround = PlayGround()
        self._minutes:int = minutes  # minute interval between triggerings
        self._timeStamp = startTime
        self._initislTimeStamp = startTime
        self._trgTime: TrgTime = TrgTime()
        self._trgTime.setTime(startTime)
        self._counter:int = 0
        self._limit:int = limit
        if limit < 1:
            self._limit = 1

    def setMinutes(self, minutes: int):
        if minutes > -1:
            self._minutes = minutes

    def getLimit(self)->int:
        return self._limit

    def setLimit(self,limit:int):
        if limit>0:
            self._limit = limit

    def getCounter(self)->int:
        return self._counter

    def setMinutes(self,minutes:int):
        if minutes > -1:
            self._minutes = minutes

    # override
    def trigger(self) -> bool:
        # delete counter = 0 if you don't want the trigger to work the next day
        if self._counter == self._limit:
            self._trgTime.setTime(self._initislTimeStamp)
            self._counter = 0
            return False
        if self._trgTime.alarm():
            self._timeStamp = self._playGround.getFutureInXMin(self._minutes)
            self._trgTime.setTime(self._timeStamp)
            self._counter += 1
            return True
        return False

    # override
    def reset(self):
        # manual trigger reset
        self._counter = 0

king

Last edited by Moti Barski on Sun Aug 27, 2023 9:31 am; edited 1 time in total

description[python] [java] [swift] Cron job classes EmptyRe: [python] [java] [swift] Cron job classes

more_horiz
there are more classes.
see classes with names starting with Trg.
TrgTime for example triggers on specific time stamps(once per 24 hours)

there is also the Playground class for identifying time ranges and getting time data (date, times, time calculations, days of week...)

teleport
https://github.com/yotamarker/public-livinGrimoire/blob/master/livingrimoire%20start%20here/LivinGrimoire%20python/python%20files/AXPython.py

description[python] [java] [swift] Cron job classes Emptyexample cron jon python main

more_horiz

Code:

import time

from LivinGrimoireCoreV2 import *
from AXPython import *

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files,
# tool windows, actions, and settings.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    t1:Cron = Cron(PlayGround().getCurrentTimeStamp(),2,3)
    for i in range(0,20):
        print(t1.trigger())

in case you've been living under a rock:
PlayGround().getCurrentTimeStamp()
simply returns a time stamp, like : "09:49"

notice the import code line in the beginning of the code:
from LivinGrimoireCoreV2 import *
from AXPython import *

these are 2 files you copypaste into the python project:
https://github.com/yotamarker/public-livinGrimoire/tree/master/livingrimoire%20start%20here/LivinGrimoire%20python/python%20files

in the code snippet if you put a break point at print(t1.trigger()) and debug like each 30 seconds, you will notice
it returns true only once per 2 minutes and up to 3 times.

FRspeaker40x40  this is the way

description[python] [java] [swift] Cron job classes EmptyRe: [python] [java] [swift] Cron job classes

more_horiz
and the swift Cron class:


Code:

class Cron:TrGEV3{
    // triggers true, limit times, after initial time, and every minutes interval
    // counter resets at initial time, assuming trigger method was run
    private var minutes:Int // minute interval between triggerings
    private let pl:PlayGround = PlayGround()
    private var trgTime:TrgTime
    private var timeStamp:String = ""
    private var initialTimeStamp:String = ""
    private var limit:Int
    private var counter:Int = 0
    init(startTime:String, minutes:Int, limit:Int) {
        self.minutes = minutes
        self.timeStamp = startTime
        self.initialTimeStamp = startTime
        trgTime = TrgTime()
        trgTime.setTime(v1: startTime)
        self.limit = limit
        if limit<0{
            self.limit = 1
        }
    }
    func getIimit()->Int{
        return limit
    }
    func setLimit(lim:Int){
        if lim > -1{
            limit = lim
        }
    }
    func getCounter()->Int{
        return self.counter
    }
    func setMinutes(minutes:Int){
        // set interval between trigger times
        if minutes > -1{
            self.minutes = minutes
        }
    }
    override func trigger() -> Bool {
        // delete counter = 0 if you don't want the trigger to work the next day
        if counter == limit{
            trgTime.setTime(v1: initialTimeStamp)
            counter = 0
            return false
        }
        if trgTime.trigger() {
            timeStamp = pl.getFutureInXMin(extra_minutes: minutes)
            trgTime.setTime(v1: timeStamp)
            counter += 1
            return true
        }
        return false
    }
    override func reset() {
        // manual trigger reset
        counter = 0
    }
}


https://github.com/yotamarker/public-livinGrimoire/tree/master/livingrimoire%20start%20here/LivinGrimoire%20swift

import livingrimoire and auxiliary files and then it's possible to use Cron in the main swift file.
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply