在python上为特定时间创建计划程序

在python上为特定时间创建计划程序,python,slice,Python,Slice,您好,我正在尝试创建一个方法,该方法返回一个列表,其中包含程序应发送警报的小时数,对于所有情况,第一个警报需要在00:00发送,因此,如果使用120分钟(即每两小时一次)创建方法,则效果良好,但每4小时240分钟发送警报不起作用 这是我的密码 def candle_close_timings(candle): frame = None if candle >= 60: frame = candle/60 print (frame) minutes = []

您好,我正在尝试创建一个方法,该方法返回一个列表,其中包含程序应发送警报的小时数,对于所有情况,第一个警报需要在00:00发送,因此,如果使用120分钟(即每两小时一次)创建方法,则效果良好,但每4小时240分钟发送警报不起作用

这是我的密码

def candle_close_timings(candle):
    frame = None
    if candle >= 60: frame = candle/60
    print (frame)
    minutes = []
    hours = []
    for hours in range(0, 24):
        minute = 0
        while minute <= 59:
            minutes.append('%02d:%02d' % (hours, minute))
            minute = minute + candle
    del minutes[frame-1::frame]
    return minutes
print (candle_close_timings(120))
它应该会回来

[00:00 , 04:00, 08:00...]

有谁能帮我把这个列表切分一下吗?你把问题弄得太复杂了。这里是一个简单得多的解决方案,因为该方法只能工作精确的小时数,即60的倍数:

然后

结果

['00:00', '02:00', '04:00', '06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00']
['00:00', '04:00', '08:00', '12:00', '16:00', '20:00']

结果

['00:00', '02:00', '04:00', '06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00']
['00:00', '04:00', '08:00', '12:00', '16:00', '20:00']

你把问题复杂化了。这里是一个简单得多的解决方案,因为该方法只能工作精确的小时数,即60的倍数:

然后

结果

['00:00', '02:00', '04:00', '06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00']
['00:00', '04:00', '08:00', '12:00', '16:00', '20:00']

结果

['00:00', '02:00', '04:00', '06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00']
['00:00', '04:00', '08:00', '12:00', '16:00', '20:00']

正如评论者所指出的,如果在for循环中运行while循环,则解决方案过于复杂

我建议改为使用以下解决方案。其背后的逻辑是,一天正好有1440分钟,因此,只要循环遍历烛光*n个数字,您就可以轻松地以分钟为单位获得事件时间

然后我们可以使用两个简单的除法运算将分钟转换为时间:一个商将给出小时分钟/60,而剩余的分钟%60将给出剩余的分钟

所以

打印蜡烛\u关闭\u定时120

我会给你

['00:00','02:00','04:00','06:00','08:00','10:00','12:00','14:00','16:00','18:00','20:00','22:00']

打印蜡烛\u关闭\u定时110

会回来的

['00:00','01:50','03:40','05:30','07:20','09:10','11:00','12:50','14:40','16:30','18:20','20:10','22:00','23:50']

因此,完整的解决方案是:

import math
def candle_close_timings(candle):
    #one day has 1440 minutes
    schedule = ['00:00']
    # we use ceil here to make sure to add one last event if the event time does not perfectly fit into the day (1440 % candle != 0)
    for event in range(1, int(math.ceil(1440/candle))):
        # every event advances a preceeding even by candle minutes
        time_step = candle * event
        # we then get the hour of the event by dividing the minutes by 60 and taking the result
        hrs = int(time_step / 60)
        # we then get the hour of the event by dividing the minutes by 60 and taking the remainder
        mins = int(time_step % 60)
        # format the string into a hourly string
        schedule.append('%02d:%02d' % (hrs, mins))
    return schedule
print (candle_close_timings(120))

正如评论者所指出的,如果在for循环中运行while循环,则解决方案过于复杂

我建议改为使用以下解决方案。其背后的逻辑是,一天正好有1440分钟,因此,只要循环遍历烛光*n个数字,您就可以轻松地以分钟为单位获得事件时间

然后我们可以使用两个简单的除法运算将分钟转换为时间:一个商将给出小时分钟/60,而剩余的分钟%60将给出剩余的分钟

所以

打印蜡烛\u关闭\u定时120

我会给你

['00:00','02:00','04:00','06:00','08:00','10:00','12:00','14:00','16:00','18:00','20:00','22:00']

打印蜡烛\u关闭\u定时110

会回来的

['00:00','01:50','03:40','05:30','07:20','09:10','11:00','12:50','14:40','16:30','18:20','20:10','22:00','23:50']

因此,完整的解决方案是:

import math
def candle_close_timings(candle):
    #one day has 1440 minutes
    schedule = ['00:00']
    # we use ceil here to make sure to add one last event if the event time does not perfectly fit into the day (1440 % candle != 0)
    for event in range(1, int(math.ceil(1440/candle))):
        # every event advances a preceeding even by candle minutes
        time_step = candle * event
        # we then get the hour of the event by dividing the minutes by 60 and taking the result
        hrs = int(time_step / 60)
        # we then get the hour of the event by dividing the minutes by 60 and taking the remainder
        mins = int(time_step % 60)
        # format the string into a hourly string
        schedule.append('%02d:%02d' % (hrs, mins))
    return schedule
print (candle_close_timings(120))

这是一种非常麻烦的实现方法。你的方法是否应该在分钟数不是一小时的精确倍数的情况下工作?i、 e.candle_close_Timing 110是否有效?客户要求以分钟为单位键入时间,因此我需要将分钟转换为小时,客户还指定时间为2小时和4小时。这是一种非常麻烦的实现方法。你的方法是否应该在分钟数不是一小时的精确倍数的情况下工作?i、 e.candle_close_Timing 110是否有效?客户要求以分钟为单位键入时间,因此我需要将分钟转换为小时,客户还指定时间为2小时和4小时。这是对我整个问题的极好回答,我认为这是对我的问题的更优化的解决方案,因为您的解决方案包括稍后将出现的问题的答案,例如,当我的客户希望每3分钟设置一次警报时。我真的很感激,我不能给你检查,因为你的答案排在第二位,但你肯定得到了一个分数。如果你打算在生产环境中运行这段代码,请检查我的另一个答案:你可以用专用python工具以一种更具争议性、更高效、更简单的方式实现同样的结果。非常感谢,似乎是一个非常有趣的调度员,我太深入这个项目的开发面了,但对于其他项目,我会考虑到它。这是我整个问题的一个极好的答案,我认为是我的问题的一个更优化的解决方案,因为你的解决方案包括对稍后出现的问题的答案,例如,当我的客户希望每3分钟设置一次警报时。我真的很感激,我不能给你检查,因为你的答案排在第二位,但你肯定得到了一个分数。如果你打算在生产环境中运行这段代码,请检查我的另一个答案:你可以用专用python工具以一种更具争议性、更高效、更简单的方式实现同样的结果。非常感谢,似乎是一个非常有趣的调度器,我太深入到这个项目的开发面,但对于其他项目,我会考虑它。