Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: to_dict(key, duplicated=reducer) #14

Merged
merged 1 commit into from
Aug 26, 2022
Merged

Conversation

kitsuyui
Copy link
Owner

@kitsuyui kitsuyui commented Aug 26, 2022

Allow to pass reducer to to_dict.
This makes it possible to select items according to specific conditions when keys are duplicated.
For example, it will be possible to take out the largest item and take out the smallest item.

from dataclasses import dataclass
from richset import RichSet


@dataclass(frozen=True)
class Something:
    id: int
    name: str


rs = RichSet.from_list(
    [
        Something(1, "john"),
        Something(2, "jane"),
        Something(3, "john"),  # name duplicated
    ]
)

rs.to_dict(
    lambda r: r.name,
    duplicated=lambda items: min(items, key=lambda item: item.id),
)
# => {"john": Something(1, "john"), "jane": Something(2, "jane")}
rs.to_dict(
    lambda r: r.name,
    duplicated=lambda items: max(items, key=lambda item: item.id),
)
# => {"john": Something(3, "john"), "jane": Something(2, "jane")}

Close: #13

@kitsuyui kitsuyui force-pushed the to-dict-with-reducer branch from 6601b0f to 25b7767 Compare August 26, 2022 07:14
Allow to pass reducer to to_dict.
This makes it possible to select items according to specific conditions when keys are duplicated.
For example, it will be possible to take out the largest item and take out the smallest item.
@kitsuyui kitsuyui force-pushed the to-dict-with-reducer branch from 25b7767 to 1c887d1 Compare August 26, 2022 07:15
@codecov-commenter
Copy link

Codecov Report

Merging #14 (1c887d1) into main (287a68f) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main       #14   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines          195       196    +1     
=========================================
+ Hits           195       196    +1     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
richset/__init__.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@kitsuyui kitsuyui merged commit 00e5f68 into main Aug 26, 2022
@kitsuyui kitsuyui deleted the to-dict-with-reducer branch August 26, 2022 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

to_dict(key, duplicated=reducer)
2 participants