Skip to content

Instantly share code, notes, and snippets.

@PaluMacil
Created September 21, 2021 03:18
Show Gist options
  • Save PaluMacil/6cb83842042bc7968913f5d21812d97e to your computer and use it in GitHub Desktop.
Save PaluMacil/6cb83842042bc7968913f5d21812d97e to your computer and use it in GitHub Desktop.
from itertools import groupby
from typing import Union
Registration = dict[str, Union[int, str]]
ALL_REGISTRATIONS = [
{'id': 0,
'hostname': 'lappytoppy',
'last_seen': '2021-09-27T18:00'},
{'id': 1,
'hostname': 'server01',
'last_seen': '2021-12-14T18:00'},
{'id': 2,
'hostname': 'boxbox',
'last_seen': '2021-09-29T18:00'},
{'id': 3,
'hostname': 'server01',
'last_seen': '2021-11-19T18:00'},
{'id': 4,
'hostname': 'server01',
'last_seen': '2021-11-30T18:00'},
{'id': 5,
'hostname': 'pi',
'last_seen': '2021-12-01T18:00'},
{'id': 6,
'hostname': 'server02',
'last_seen': '2021-12-22T18:00'},
{'id': 7,
'hostname': 'server02',
'last_seen': '2021-12-23T18:00'}
]
def dedup(registrations: list[Registration]):
key_func_hostname = lambda registration: registration.get('hostname')
key_func_last_seen = lambda registration: registration.get('last_seen')
registrations.sort(key=key_func_hostname)
most_recent = []
for hostname, duplicates in groupby(registrations, key_func_hostname):
sorted_duplicates = list(duplicates)
sorted_duplicates.sort(key=key_func_last_seen, reverse=True)
most_recent.append(sorted_duplicates[0])
return most_recent
if __name__ == '__main__':
no_repeats = dedup(ALL_REGISTRATIONS)
print(no_repeats)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment