Advertisement
Romeech

custom zip

Sep 19th, 2018
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1.     @staticmethod
  2.     def __zip_prev_with_new(prev_groups, new_groups):
  3.         from itertools import ifilter
  4.  
  5.         def drop_tz_unsafe(date):
  6.             return date.replace(tzinfo=None)
  7.  
  8.         left = map(list, prev_groups)
  9.         right = map(list, new_groups)
  10.         zipped = []
  11.  
  12.         while len(left):
  13.             old_group = left.pop()
  14.             start_date = drop_tz_unsafe(old_group[0].billing_period_start_date)
  15.             new_groups_iter = ifilter(
  16.                 lambda (idx, grp): drop_tz_unsafe(grp[0].billing_period_start_date) == start_date,
  17.                 enumerate(right)
  18.             )
  19.             new_group_idx, _ = next(new_groups_iter, (-1, None))
  20.             pair = (old_group, right.pop(new_group_idx)) if new_group_idx >= 0 else (old_group, [])
  21.             zipped.append(pair)
  22.  
  23.         while len(right):
  24.             zipped.append(([], right.pop()))
  25.  
  26.         return zipped
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement