0

I have a list of dictionary, where I need to calculate the exit time by adding time delta to the entry time. I then need to update the entry time of the next row with the exit time of the previous row, and add another delta to it, which then becomes the exit time.

lst = [
{'entry_time': [datetime.time(8, 20)],'exit_time': [0]},
{'entry_time': [],'exit_time': []},
{'entry_time': [0],'exit_time': [0]},
{'entry_time': [],'exit_time': []}
]

delta_lst = [datetime.timedelta(0, 53),
datetime.timedelta(0, 32),
datetime.timedelta(0, 32),
datetime.timedelta(0, 32)]

I have spend many days on this, but I feel that I am missing something for such a simultaneous operation. I dont know much about other data structures, but I should I be considering heap or queues or stack? Here's the code I have.

for idx,l in enumerate(lst):
    for key,val in l.items():
        if key == 'entry_time':
            try:
                entry_time = l[key][0]
                cal_ExitTime(entry_time,idx)
                l['exit_time'] = [exit_time]
            except:
                entry_time = lst[idx-1]['exit_time'][0]
                l['entry_time'] = [exit_time]
            cal_ExitTime(entry_time,idx)
            l['exit_time'] = [exit_time]

There's a function cal_ExitTime which is written as such -

def cal_ExitTime(entry_time,idx):
    converted_start_time = datetime.datetime.combine(datetime.date(1,1,1),entry_time)
    exit_time = (converted_start_time + delta_lst[idx]).time()    
    return exit_time

When I run my program, 1st dictionary entry in my list gets updated, and the rest of my entry_time and exit_time gets updated with the exit_time of my 1st row.

It may seem that the entries of my dictionary of list is not consistent (ie empty list or 0 value), but the source of my data is as such.

My expected code as follows -

lst = [
{'entry_time': [datetime.time(8, 20)],'exit_time':[datetime.time(8, 20, 53)]}, 
{'entry_time': [datetime.time(8, 20,53)],'exit_time': [datetime.time(8, 21, 25)]},
{'entry_time': [datetime.time(8, 21, 25)],'exit_time': [datetime.time(8, 21, 57)]},
{'entry_time': [datetime.time(8, 21, 57)],'exit_time': [datetime.time(8, 22, 29)]}
]

Thanks for your valuable insights and help.

Zoozoo
  • 240
  • 4
  • 13

1 Answers1

1

I think what you're describing is something like this:

last = None
for row, delta in zip(lst, delta_lst):
    if last is not None:
        row["entry_time"] = last["exit_time"]

    row["exit_time"] = row["entry_time"] + delta
    last = row
Batman
  • 8,571
  • 7
  • 41
  • 80
  • it worked with some time object conversion, but I dont get it. Can you please explain a bit of what the syntax last = None referring to? I dont understand this checking of None, and then setting last = row. – Zoozoo Oct 10 '18 at 14:00
  • 1
    It's basically just book keeping. The first time through the loop you don't have a previous row, so you can't set the entry time for that row to be the exit time of the last row. For all the other rows though, that's exactly what you want to do. So at the end of each iteration you set the last row that you saw to be the current row. Then on the next run through the loop you see that there is indeed a previous row, and set the entry time to be the exit time of that previous row. – Batman Oct 10 '18 at 15:00
  • 1
    thanks for the code. I cant believe how clean and few the lines are compared to my verbose code. I guess practice reduces lines. :) – Zoozoo Oct 10 '18 at 18:39
  • It definitely does. – Batman Oct 11 '18 at 00:19