0

Background:

I have a python script to check working hours of employees. Each employee has morning and afternoon shifts, with lunch time in between, and each time they put the finger a new timestamp is recorded.

So, depending on the time of each day, there may be from zero to four timestamps in the list for that day.

The question: "How can I 'unpack' timestamps to the respective variables avoiding all this ugly, duplicated code?"

morning_entry = None
morning_leave = None
afternoon_entry = None
afternoon_leave = None

timestamps = get_timestamps()

if timestamps:
    morning_entry = timestamps.pop(0)

if timestamps:
    morning_leave = timestamps.pop(0)

if timestamps:
    afternoon_entry = timestamps.pop(0)

if timestamps:
    afternoon_leave = timestamps.pop(0)
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • `pop()` removes the last element from the list. Not obvious design choice imo. – vaultah Oct 09 '14 at 12:51
  • @PadraicCunningham: but `timestamps.pop()` is mutating `timestamps`. – DSM Oct 09 '14 at 12:53
  • @PadraicCunningham: as DSM pointed out, I am taking off one element at a time and assigning it. This works exactely as I want, but is obviously very ugly and violates DRY badly (imagine a longer list of variables...) – heltonbiker Oct 09 '14 at 12:56
  • @PadraicCunningham I cannot see how your suggestion is better. Please post an answer with some code so that I can better understand it and perhaps accept it. – heltonbiker Oct 09 '14 at 12:59

2 Answers2

1

A simple solution but may not be that elegant

morning_entry,morning_leave,afternoon_entry,afternoon_leave=(timestamps+[None]*4)[:4]

Just pad it with Nones before the list and then slice

  • I actually used `= timestamps + [None]*(4-len(timestamps))`, but that is personal preference. Either approach works! Thanks! – heltonbiker Oct 09 '14 at 13:08
  • (also, note that my actual code should use `pop(0)`, which I forgot to type the first time I wrote the question, but now that is corrected. That is important because otherwise I get the wrong order) – heltonbiker Oct 09 '14 at 13:10
  • 1
    Ah!! actually I thought that while typing my answer but then saw that u r poping it from backside, then I changed the variable order – Sainath Motlakunta Oct 09 '14 at 13:14
0

An itertools-based version of Sainath's answer:

from itertools import chain, repeat

(morning_entry,
 morning_leave,
 afternoon_entry,
 afternoon_leave) = chain(timestamps, repeat(None, 4))

Whether it is more elegant is debatable; it does have the minor improvement that timestamps is not restricted to any particular type of iterable.

Community
  • 1
  • 1
chepner
  • 497,756
  • 71
  • 530
  • 681