runtime
239 / 500
question 239 Debugging & Logging · medium

Duplicate events: drop, warn or stop

The GitHub event feed is delivered at least once, so the same event can arrive twice. Write dedupe_events(events) with this policy. An exact re-delivery (same id, identical event) is dropped silently. The same id with different content means the feed is inconsistent: keep the first, drop the later one and log a WARNING. An event with no id at all means the feed format has changed: raise ValueError.

Functiondedupe_events(events), logging with log = logging.getLogger("events")
eventsa list of dictionaries, each normally with id, type and repo
Returnsa new list with the first event for each id, in the original order
LogsWARNING event <id>: conflicting duplicate dropped for each later event whose content differs from the kept one; nothing for exact repeats
RaisesValueError when an event has no id, or an empty one
Must notchange the list it is given
Examples
calldedupe_events([{'id': '2489651045', 'type': 'PushEvent', 'repo': 'octo/api'}, {'id': '2489651051', 'type': 'WatchEvent', 'repo': 'octo/api'}, {'id': '2489651045', 'type': 'PushEvent', 'repo': 'octo/api'}, {'id': '2489651057', 'type': 'PushEvent', 'repo': 'rails/rails'}, {'id': '2489651051', 'type': 'ForkEvent', 'repo': 'octo/api'}])returns[{'id': '2489651045', 'type': 'PushEvent', 'repo': 'octo/api'}, {'id': '2489651051', 'type': 'WatchEvent', 'repo': 'octo/api'}, {'id': '2489651057', 'type': 'PushEvent', 'repo': 'rails/rails'}]
calldedupe_events([])returns[]

Loading your progress…

Duplicate events: drop, warn or stop — Python practice · Runtime