【问题标题】:Create a for-loop that calls a function every x-time in Python在 Python 中创建一个每 x 次调用一个函数的 for 循环
【发布时间】:2021-05-05 18:17:23
【问题描述】:

我有这些数据:

granularity: 1h
start_time: 2021-4-28T1:1:1.342380Z
end_time: 2021-4-29T1:1:1.342360Z

我想创建一个类似这样的伪代码:

for start_time to end_time:
    call function on each interval of granularity

在给定的示例中,我将从 start_time 到 end_time 每 1 小时调用一次函数。时间和粒度参数可以更改,并且不固定为我的示例中给出的值。可以是 60 分 5 秒...

经过一番思考并在博客和 Stack Overflow 上进行搜索后,我不知道该怎么做。我可以尝试什么?

【问题讨论】:

    标签: python-3.x for-loop time


    【解决方案1】:

    我使用 datetime 库编写了这个,这是大多数 Python 安装的标准配置。它需要以秒为单位的时间,因此,60 秒为一分钟,3600 秒为一小时。


    代码:

    from datetime import datetime
    
    def alert(time):
        hourtime = time / 60
        print(f"It's Been {time} seconds or {hourtime} Hours")
    
    
    def timer(seconds, start_time, end_time):
        curr_time = datetime.now()
        formatted_time = curr_time.strftime('%S')
        lasttime = formatted_time
        localtimer = 0 
        while True:
            curr_time = datetime.now()
    
            formatted_time = curr_time.strftime('%S')
            time_ =  curr_time.strftime('%H%M')
    
    
            if int(time_) <= int(end_time) - 1: # Check to see if the current time is less than the end time
                if int(time_) >= int(start_time): # check to see if it's Greater or equal to the start time
                    if int(lasttime) != int(formatted_time): # if so it does the alert function every x seconds
                        lasttime = formatted_time
                        print(localtimer) # You can Keep or remove this line it's just for counting how many seconds have gone by.
                        localtimer += 1
                    if localtimer == int(seconds):
                        alert(seconds)
                        localtimer = 0
            
    
    timer(seconds=5, start_time=1547, end_time=1548) # It takes time in seconds, start and end time are both in the 24 hr format just without the ":" so instead of 15:44, it would be 1544
    

    (注意:OP 纠正了我它需要一个开始和结束时间,所以我添加了它)。


    这是没有开始和结束时间的旧代码:

    from datetime import datetime
    
    def alert(time):
        hourtime = time / 60
        print(f"It's Been {time} seconds or {hourtime} Hours")
    
    
    def timer(seconds):
        curr_time = datetime.now()
        formatted_time = curr_time.strftime('%S')
        lasttime = formatted_time
        localtimer = 0 
        while True:
            curr_time = datetime.now()
            formatted_time = curr_time.strftime('%S')
            if int(lasttime) != int(formatted_time):
                lasttime = formatted_time
                print(localtimer) # You can Keep or remove this line it's just for counting how many seconds have gone by.
                localtimer += 1
            if localtimer == int(seconds):
                alert(seconds)
                localtimer = 0
    
    
    timer(5) # It takes time in seconds so 3600 will be 3600 seconds which is an hour and 5 is 5 seconds, so a minute would be 60 and so on and so forth
    

    【讨论】:

    • 感谢您的创意!实际上,您的算法是从 datetime.now() 而不是开始时间开始的。此外,您还没有考虑结束时间,因为日期应该在开始时间和结束时间之间。但无论如何感谢你的想法:)
    • 哦,抱歉,我一定看错了,我看看能不能一分钟解决。
    • @CatarinaNogueira 我在原始答案中添加了开始和结束时间。
    猜你喜欢
    • 2023-02-06
    • 2020-03-13
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2018-01-16
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多