在python中跟踪多次运行时间
举个例子,我定义了一个字典,其中包含一个特定的条形码和一个日期时间 我想从字典中的时间中减去当前时间,这对一个人有效,但我不知道如何才能对每个人都有效 所以我想让程序做的是跟踪时间,当时间达到0时,时间就会被超过,例如,一个警报就会打开。但我想在字典里把它们都记下来 这是我当前的代码,目前我保持它相当简单:在python中跟踪多次运行时间,python,dictionary,Python,Dictionary,举个例子,我定义了一个字典,其中包含一个特定的条形码和一个日期时间 我想从字典中的时间中减去当前时间,这对一个人有效,但我不知道如何才能对每个人都有效 所以我想让程序做的是跟踪时间,当时间达到0时,时间就会被超过,例如,一个警报就会打开。但我想在字典里把它们都记下来 这是我当前的代码,目前我保持它相当简单: from datetime import * import time dict = {'619999': '2017-04-22 11:40:00', '0991626': '2017-0
from datetime import *
import time
dict = {'619999': '2017-04-22 11:40:00', '0991626': '2017-05-20 11:40:00', '177797': '2017-06-15 11:40:00'}
def check_elapsed_time():
while True:
now = time.strftime('%Y-%m-%d %H:%M:%S')
current_time = datetime.strptime(now, '%Y-%m-%d %H:%M:%S')
component_max_time = datetime.strptime(dict['619999'], '%Y-%m-%d %H:%M:%S')
elapsed_time = component_max_time - current_time
time.sleep(1)
print(elapsed_time)
check_elapsed_time()
您只需在字典中迭代并计算每个值即可。我还向您的代码中添加了一个检查,如果达到您的字典条目之一,该检查将上升一个alamr
from datetime import *
import time
dict = {'619999': '2017-04-22 11:40:00', '0991626': '2017-05-20 11:40:00', '177797': '2017-06-15 11:40:00', '177795': '2017-03-22 14:05:00'}
def check_elapsed_time():
while True:
now = time.strftime('%Y-%m-%d %H:%M:%S')
current_time = datetime.strptime(now, '%Y-%m-%d %H:%M:%S')
for key in dict:
component_max_time = datetime.strptime(dict[key], '%Y-%m-%d %H:%M:%S')
elapsed_time = component_max_time - current_time
print(key + " " + str(elapsed_time))
if component_max_time == current_time:
print("ALARM!!!")
time.sleep(1)
check_elapsed_time()
编辑:为今天的字典添加了一个测试用例,只是为了测试您只需迭代字典并计算每个值。我还向您的代码中添加了一个检查,如果达到您的字典条目之一,该检查将上升一个alamr
from datetime import *
import time
dict = {'619999': '2017-04-22 11:40:00', '0991626': '2017-05-20 11:40:00', '177797': '2017-06-15 11:40:00', '177795': '2017-03-22 14:05:00'}
def check_elapsed_time():
while True:
now = time.strftime('%Y-%m-%d %H:%M:%S')
current_time = datetime.strptime(now, '%Y-%m-%d %H:%M:%S')
for key in dict:
component_max_time = datetime.strptime(dict[key], '%Y-%m-%d %H:%M:%S')
elapsed_time = component_max_time - current_time
print(key + " " + str(elapsed_time))
if component_max_time == current_time:
print("ALARM!!!")
time.sleep(1)
check_elapsed_time()
编辑:在您的字典中添加了一个测试用例,仅用于测试根据我的理解,当dict中的一个时间达到零时,即当我们的时间等于dict中的一个时间时,您的算法应该触发某个事件。我建议您选择“第一次达到零时”通过找到最小值并单独跟踪。 您可以通过以下方式实现:
# This program works.
from datetime import *
import time
dict = {'619999': '2017-03-22 17:44:40', '0991626': '2017-05-20 11:40:00', '177797': '2017-06-15 11:40:00'}
def reaches_zero_first():
times = dict.values()
first_zero = min(times)
return first_zero
def check_elapsed_time(min_time):
while True:
now = time.strftime('%Y-%m-%d %H:%M:%S')
current_time = datetime.strptime(now, '%Y-%m-%d %H:%M:%S')
component_max_time = datetime.strptime(min_time, '%Y-%m-%d %H:%M:%S')
elapsed_time = component_max_time - current_time
time.sleep(1)
print(elapsed_time)
if(elapsed_time.total_seconds() == 0.0):
print "Reached"
break
# TRIGGER SOMETHING HERE
first_to_zero = reaches_zero_first()
check_elapsed_time(first_to_zero)
根据我的理解,当dict中的一个时间达到零时,即当我们的时间等于dict中的一个时间时,您的算法应该触发某些事件。我建议您通过找到最小值并单独跟踪来选择“第一次达到零”。 您可以通过以下方式实现:
# This program works.
from datetime import *
import time
dict = {'619999': '2017-03-22 17:44:40', '0991626': '2017-05-20 11:40:00', '177797': '2017-06-15 11:40:00'}
def reaches_zero_first():
times = dict.values()
first_zero = min(times)
return first_zero
def check_elapsed_time(min_time):
while True:
now = time.strftime('%Y-%m-%d %H:%M:%S')
current_time = datetime.strptime(now, '%Y-%m-%d %H:%M:%S')
component_max_time = datetime.strptime(min_time, '%Y-%m-%d %H:%M:%S')
elapsed_time = component_max_time - current_time
time.sleep(1)
print(elapsed_time)
if(elapsed_time.total_seconds() == 0.0):
print "Reached"
break
# TRIGGER SOMETHING HERE
first_to_zero = reaches_zero_first()
check_elapsed_time(first_to_zero)
这就是我想要的。谢谢!这看起来比我想象的要简单得多。这就是我想要的。谢谢!这似乎比我想象的要简单得多。我想连续检查字典中的每个日期时间,如果任何时间达到零,一定会发出警报,但其他没有达到零的时间也应该继续计数,直到达到零。好的。我以为你只想让闹钟响一次。对不起,我的错。你不必道歉,我很高兴你能帮我!我知道我的英语并不完美。我想不断地检查字典中的每个日期时间,如果任何时间达到零,一定会发出警报,但其他没有达到零的时间也应该继续计数,直到达到零。好的。我以为你只想让闹钟响一次。对不起,我的错。你不必道歉,我很高兴你能帮我!我知道我的英语并不完美。