-
Notifications
You must be signed in to change notification settings - Fork 93
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
Add maybe_apply
function
#223
Conversation
What do you think about naming it just |
Actually, I just remembered that there's pydash/src/pydash/chaining/chaining.py Lines 268 to 287 in 292517c
that has the same basic idea as So wondering now if renaming Otherwise, if wanting to do a breaking change, then rename Thoughts? |
I don't think it is too appropriate for the function of this PR. The naming And if we have Imo I like it more as I was not sure about the |
I guess what's weird about My main point was more about not having two functions that do the same thing but have different names (e.g. if we kept I would be in fine with renaming For the specific naming of Thoughts? |
This sounds good About the naming, one really good point of having the apply functions prefixed with |
Let's go with prefixing with For renaming
Open to other ideas too. |
So I updated the PR:
Since we are going this Let me know what you think |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some nits on naming, but everything else looks good.
src/pydash/objects.py
Outdated
... | ||
|
||
|
||
def apply_ignore_excs(obj, func, excs, fallback=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about naming this apply_safely()
or apply_with_catch()
?
For argument naming:
excs -> exceptions
: Prefer the full name here (we're usingexceptions
in another function as well)fallback -> default
:default
is used elsewhere to signify the value to return when can't return an actual value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking some more on this function. Wondering if we should have a way to specify that obj should be returned on exception without having to pass it in explicitly. For example, if using this in chaining, then the value of obj won't necessarily be known when the method is called so won't be able to pass it as the default/fallback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have the default value be UNSET that would return obj on exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very good points, I applied everything.
About the naming I named it apply_catch
. I find apply_safely
a bit misleading as it only catches specific exceptions and apply_with_catch
sounds a bit funny to me. Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me!
a4ea5bc
to
f8f69ed
Compare
f8f69ed
to
606c0b4
Compare
2308df1
to
3b4af7c
Compare
I pinned the version of |
Looks like a pytest 8.1.0 thing: pytest-dev/pytest#11779 I opened an issue with |
One thing that would be cool to have in the library is a maybe apply function kinda thing.
pydash.get
is really cool but I am a bit sad that it is not typable in the current python type system. One very common use case of it I believe is to get data from an optional value:But we could have a
maybe_apply
function thing that would take a callable:this is typable.
And it is not restricted to attribute or key getting, we can just apply anything to an optional value, it abstracts this pattern:
And with the chaining interface I think it would look really cool, eg: