You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
1010 B
25 lines
1010 B
def slice(snapshots, tag, start=0, stop=None, reverse=True,
|
|
key=lambda snapshot: snapshot.serial()):
|
|
'''
|
|
Take list of 'snapshots' having 'tag'. Take set of 'key's from list,
|
|
sort the set (optionally in 'reverse'), slice it from 'start' to 'stop',
|
|
take list of 'snapshots' belonging to this set and return it. If 'stop'
|
|
is not specified, slice set from 'start' to end of set. If called with
|
|
first three arguments only, return list of snapshots older than the
|
|
'index'th snapshot in the set.
|
|
'''
|
|
|
|
# Find snapshots having 'tag'
|
|
matches = (s for s in snapshots if s.tag() == tag)
|
|
|
|
# Take ordered set of keys from matching snapshots
|
|
ordered_set = sorted(set(key(s) for s in matches), reverse=reverse)
|
|
|
|
# Take slice from ordered set of keys
|
|
keys = set(ordered_set[start:] if stop is None
|
|
else ordered_set[start:stop])
|
|
|
|
# Filter matches by keys in sliced set
|
|
result = (s for s in matches if key(s) in keys)
|
|
|
|
return result
|
|
|