1.1 --- a/imipweb/data.py Tue Sep 12 22:09:24 2017 +0200
1.2 +++ b/imipweb/data.py Tue Sep 12 23:07:14 2017 +0200
1.3 @@ -343,6 +343,23 @@
1.4
1.5
1.6
1.7 +# Form period processing.
1.8 +
1.9 +def get_active_periods(periods):
1.10 +
1.11 + "Return a mapping of non-replaced periods to counts, given 'periods'."
1.12 +
1.13 + active_periods = {}
1.14 + for p in periods:
1.15 + if not p.replaced:
1.16 + if not active_periods.has_key(p):
1.17 + active_periods[p] = 1
1.18 + else:
1.19 + active_periods[p] += 1
1.20 + return active_periods
1.21 +
1.22 +
1.23 +
1.24 # Form field extraction and serialisation.
1.25
1.26 def get_date_control_inputs(args, name, tzid_name=None):
1.27 @@ -545,4 +562,52 @@
1.28 set_date_control_values(all_starts, args, start_name)
1.29 set_date_control_values(all_ends, args, end_name, tzid_name=start_name)
1.30
1.31 +
1.32 +
1.33 +# Utilities.
1.34 +
1.35 +def filter_duplicates(l):
1.36 +
1.37 + """
1.38 + Return collection 'l' filtered for duplicate values, retaining the given
1.39 + element ordering.
1.40 + """
1.41 +
1.42 + s = set()
1.43 + f = []
1.44 +
1.45 + for value in l:
1.46 + if value not in s:
1.47 + s.add(value)
1.48 + f.append(value)
1.49 +
1.50 + return f
1.51 +
1.52 +def remove_from_collection(l, indexes, fn):
1.53 +
1.54 + """
1.55 + Remove from collection 'l' all values present at the given 'indexes' where
1.56 + 'fn' applied to each referenced value returns a true value. Values where
1.57 + 'fn' returns a false value are added to a list of deferred removals which is
1.58 + returned.
1.59 + """
1.60 +
1.61 + still_to_remove = []
1.62 + correction = 0
1.63 +
1.64 + for i in indexes:
1.65 + try:
1.66 + i = int(i) - correction
1.67 + value = l[i]
1.68 + except (IndexError, ValueError):
1.69 + continue
1.70 +
1.71 + if fn(value):
1.72 + del l[i]
1.73 + correction += 1
1.74 + else:
1.75 + still_to_remove.append(value)
1.76 +
1.77 + return still_to_remove
1.78 +
1.79 # vim: tabstop=4 expandtab shiftwidth=4