Skip to content

Commit

Permalink
Refactor breadcrumb ordering and dictionary duplication handling
Browse files Browse the repository at this point in the history
Simplified the breadcrumb ordering by removing the unnecessary key parameter. Refactored the 'distinct_by' function to raise an exception when no key field is present in the dictionary, instead of considering it as a non-duplicate. This change also simplifies the function by using a dictionary comprehension for uniqueness check, making it more efficient and readable. Updated corresponding tests to reflect these changes.
  • Loading branch information
drew2a committed May 27, 2024
1 parent ae99ee1 commit 898c72f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/tribler/core/sentry_reporter/sentry_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def _format_breadcrumbs(self, event: Optional[Dict]):
if breadcrumbs := event.get(BREADCRUMBS):
breadcrumbs_values = breadcrumbs[VALUES]
breadcrumbs_values = distinct_by(breadcrumbs_values)
breadcrumbs_values = order_by_utc_time(breadcrumbs_values, key='timestamp')
breadcrumbs_values = order_by_utc_time(breadcrumbs_values)

event[BREADCRUMBS][VALUES] = breadcrumbs_values
except Exception as e:
Expand Down
21 changes: 3 additions & 18 deletions src/tribler/core/sentry_reporter/sentry_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def distinct_by(list_of_dict: Optional[List[Dict]],
"""This function removes all duplicates from a list of dictionaries. A duplicate
here is a dictionary that have the same value of the given key.
If no key field is presented in the dictionary, then the dictionary will not be considered
as a duplicate.
If no key field is presented in the dictionary, then the exception will be raised.
Args:
list_of_dict: list of dictionaries
Expand All @@ -75,22 +74,8 @@ def distinct_by(list_of_dict: Optional[List[Dict]],
if not list_of_dict:
return list_of_dict

seen = set()
result = []

for d in list_of_dict:
try:
value = getter(d)
except (KeyError, TypeError):
result.append(d)
continue

if value not in seen:
result.append(d)

seen.add(value)

return result
distinct = dict((getter(d), d) for d in list_of_dict)
return [value for _, value in distinct.items()]


def format_version(version: Optional[str]) -> Optional[str]:
Expand Down
23 changes: 7 additions & 16 deletions src/tribler/core/sentry_reporter/tests/test_sentry_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,12 @@ def test_distinct_default():


def test_distinct_key_error():
# Test distinct_by with missing key, it should keep the items
# Test distinct_by with missing key in getter
values = [
{'message': 'message 1', },
{'message': 'message 1', },
{'timestamp': 'timestamp 1', },
{'timestamp': 'timestamp 1', },
]

expected = [
{'message': 'message 1', },
{'message': 'message 1', },
{'timestamp': 'timestamp 1', },
{'timestamp': 'timestamp 1', },
]
assert distinct_by(values) == expected
with pytest.raises(KeyError):
distinct_by(values)


def test_distinct_not_default():
Expand All @@ -112,17 +103,17 @@ def test_distinct_not_default():
]

expected = [
{'message': 'message 1', 'timestamp': 'timestamp 1'},
{'message': 'message 2', 'timestamp': 'timestamp 2'},
{'message': 'message 3', 'timestamp': 'timestamp 1'},
{'message': 'message 4', 'timestamp': 'timestamp 2'}
]
assert distinct_by(values, lambda b: b['timestamp']) == expected


def test_distinct_none_in_list():
# Test distinct_by with None in list
values = [None]
expected = [None]
assert distinct_by(values) == expected
with pytest.raises(TypeError):
distinct_by(values)


FORMATTED_VERSIONS = [
Expand Down

0 comments on commit 898c72f

Please sign in to comment.