Skip to content

Instantly share code, notes, and snippets.

@PaluMacil
Created April 26, 2022 23:34
Show Gist options
  • Save PaluMacil/11c8e3db2a8f971805a2d6beb6c01cf9 to your computer and use it in GitHub Desktop.
Save PaluMacil/11c8e3db2a8f971805a2d6beb6c01cf9 to your computer and use it in GitHub Desktop.
deduplicate by object field
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'},
{'id': 8,
'hostname': 'server03',
'last_seen': None},
{'id': 9,
'hostname': None,
'last_seen': '2021-12-26T18:00'},
]
def dedup(registrations: list[Registration]):
valid_registrations = []
no_hostname_registrations = []
for registration in registrations:
if not registration.get('hostname'):
no_hostname_registrations.append(registration)
continue
if not registration.get('last_seen'):
registration.update({'last_seen': '1970-01-01T00:00'})
valid_registrations.append(registration)
key_func_hostname = lambda registration: registration.get('hostname')
key_func_last_seen = lambda registration: registration.get('last_seen')
valid_registrations.sort(key=key_func_hostname)
most_recent = []
for hostname, duplicates in groupby(valid_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.extend(no_hostname_registrations)
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