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] keys_of() method #13

Open
Marco-Sulla opened this issue Feb 1, 2020 · 4 comments
Open

[FEATURE] keys_of() method #13

Marco-Sulla opened this issue Feb 1, 2020 · 4 comments
Labels
Effort: Medium How to waste a beautiful weekend Needs: Doc Improvements or additions to documentation Needs: Test Hey, it compiles! Ship it! Priority: Low Not a big problem... Type: Enhancement New feature or request

Comments

@Marco-Sulla
Copy link
Owner

Marco-Sulla commented Feb 1, 2020

keys_of(value, deep=False) method should return a generator, that checks in frozendict values if they are equal to value. If yes, the keys are yielded.

If deep=True, value does not match the current value and it's a iterable, that is not an iterator-like or a set-like, the function will search recursively for the value in the iterable and any sub-iterable. If the iterable is a sequence, the indexes of the value is yielded. If it's a map-like, the keys are yielded.

Example:

fd = frozendict({1: 2, 7:2, 2: {5: 2, 7: 2, 0: [2, 1]}, 3: [0, 4, 2, 1, 2]})
tuple(fd.keys_of(2))
# (1, 7)

for x in fd.keys_of(2, deep=True)):
    print(tuple(x))
# (1, )
# (7, )
# (2, 5)
# (2, 7)
# (2, 0, 0)
# (3, 2)
# (3, 4)

See also #14

@Marco-Sulla Marco-Sulla added the Type: Enhancement New feature or request label Feb 1, 2020
@lurch
Copy link
Contributor

lurch commented Feb 3, 2020

Are #13, #14 and similar all "feature-creep"? i.e. adding extra functions that you think that Python dictionaries should have?
Should frozendict 'just' be a frozen dict-like class with the same API as dict, or are you writing a frozendict++? 😜

@Marco-Sulla
Copy link
Owner Author

Marco-Sulla commented Feb 3, 2020

Yes, I think dict should have a keys_of(). It's similar to list.index() and I feel the need of it a couple of times. My hope is that it will be developed also in dict.

frozendict is a map-like object. It can't have the same API of dict, since dict is mutable. But it have all the API that does not change the object. So the APIs can't be ever equal.

Your observation about feature-creep is good. Software and libraries have the tendency of overdoing. See Netscape for example. And the the KISS and YAGNI principles are always valid.

I suppose the questions we have to answer to ourselves when we want to add another feature to our class / library / app are:

  1. Is it something useful?
  2. Is it inherent to the product or is out of scope?
  3. Is it something that can be delegated to another product in a more elegant manner (a third-party module, a subclass...)?

IMHO the answer to these questions, in this case, are yes, inherent, no.

@Marco-Sulla
Copy link
Owner Author

Closed per #17

@Marco-Sulla
Copy link
Owner Author

Reopening for implementing it also in the C Extension.

@Marco-Sulla Marco-Sulla reopened this Feb 25, 2023
@Marco-Sulla Marco-Sulla added Priority: Low Not a big problem... Effort: Medium How to waste a beautiful weekend Needs: Doc Improvements or additions to documentation Needs: Test Hey, it compiles! Ship it! and removed Status: Wontfix This will not be worked on labels Feb 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Medium How to waste a beautiful weekend Needs: Doc Improvements or additions to documentation Needs: Test Hey, it compiles! Ship it! Priority: Low Not a big problem... Type: Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants