How to update time for each data received in Python

Viewed 226

I am a beginner in python and I am writing a script to save time for a data received from an algorithm

In the script I have an algorithm which accepts few parameters and returns the id of the data it detected. Below are few of its output:

S.No    Data Id         Time
1       0               2018-11-16 15:00:00
2       0, 1            2018-11-16 15:00:02
3       0, 1            2018-11-16 15:00:03
4       0, 1, 2         2018-11-16 15:00:05
5       0, 1, 2         2018-11-16 15:00:06
6       0, 2            2018-11-16 15:00:08

From the above output, we can see that at 1st attempt it detected data of id 0. In the 2nd attempt it detected the data of id 1 so total data id detected 0, 1. In 4th attempt, it detected 2 id. This keeps on going as it is running in while True. From above we can say that the time period for 0 id is 8 sec, for 1 time period is 4 sec and for 2 it is 3 sec. I need to calculate these time periods. For this I have written below code:

data_dict = {}       # To store value of time for each data id
data_dict_sec = {}   # To store value of seconds for each data id

data = amc_data_retention()   # data contains the data id

for dataID in data.items():
    if run_once:
        run_once = False
        data_dict[dataID] = time.time()
        data_dict_sec[dataID] = 0     


for dataID in data.items():
    if dataID in data_dict:
        sec = time.time() - data_dict[dataID]
        data_dict_sec[dataID] += sec
        data_dict[dataID] = time.time()

    else:
        print("New data detected")

The first for loop run once and it saves the value of time of dataID in dict. In the next for loop, that time is subtracted with current time and total seconds are saved in data_dict_sec. In first iteration, total seconds will be 0 but from the next iteration, it will start saving the correct seconds. This works fine only if there is 1 data id. As soon as 2nd data comes, it do not record time for it.

Can anyone please suggest a good way of writing this. Main objective is to keep saving the values of time period for each data id. Please help. Thanks

1 Answers

The only time this adds data_ID keys to the data_dict is in the first run. It should add each new data_ID is sees. I'm not seeing that the first for-loop is needed, where it only adds data_ID keys in the first run. It may do what you need if you moved the dictionary key initialization into the second for-loop where it checks if the data_ID is in the data_dict. If it isn't, then initialize it.

Perhaps this would do what you need:

data_dict = {}       # To store value of time for each data id
data_dict_sec = {}   # To store value of seconds for each data id

data = amc_data_retention()   # data contains the data id

for dataID in data.items():
    if dataID in data_dict:
        sec = time.time() - data_dict[dataID]
        data_dict_sec[dataID] += sec
        data_dict[dataID] = time.time()

    else:
        print("New data detected")
        data_dict[dataID] = time.time()
        data_dict_sec[dataID] = 0  
Related