Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

PJ API: Remove places with duplicated merchant_id from results #207

Merged
merged 1 commit into from
Feb 26, 2021

Conversation

amatissart
Copy link
Contributor

It has been noticed that different result pages from the API may contain duplicated POIs.

This PR suggests to remove these duplicated items. As a result, the number of places in the response, may be strictly less than the requested "size", even if more places would have been available in following pages.

Comment on lines +166 to +174
merchant_ids = set()
places = []
for p in api_places:
merchant_id = p.data.merchant_id
if merchant_id not in merchant_ids:
places.append(p)
if merchant_id:
merchant_ids.add(merchant_id)
return places
Copy link
Contributor

@remi-dupre remi-dupre Feb 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if there was a more declarative way to do it. I don't really think it is better (especially with black enforcing it as a one-liner) but still funny 🤩

EDIT: with a small difference since it is the order of the last occurrence that would be preserved

Suggested change
merchant_ids = set()
places = []
for p in api_places:
merchant_id = p.data.merchant_id
if merchant_id not in merchant_ids:
places.append(p)
if merchant_id:
merchant_ids.add(merchant_id)
return places
return list({place.data.merchant_id: place for place in api_places}.values())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking for something similar but discarded it because I thought the order was lost in a dictionary… but apparently since Python 3.7 it is not.

Actually I hoped to find something as elegant as Ramda.uniqBy :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that writing 9 lines for this does not look satisfying. But the one-liner would require a double reversed to keep the first occurrence, and it also collapses None keys (they are not supposed to exist, but are not duplicates per se).

@amatissart amatissart merged commit 6dab2b9 into master Feb 26, 2021
@amatissart amatissart deleted the dedupe-pj-listing branch April 29, 2021 09:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants