I just want to make clear that safe navegator is enough to deal with this.
Now that this is clear, the use case is almost always the same. I received
some json as response and want to extract a nested value.
The way I'm doing this today is overloading an infix operator (I'm using
>>) to emulate safe navegator so I do
value = data >> get('foo') >> get(0) >> get('bar')
if value:
do something
This get can be found here
https://github.com/dhilst/geckones_twitter_bots/blob/e6aefe036d30f84835026054676bd018f3339801/utils.py#L91
I started with array implementation them extended to object too. I usually
has something like this on all my projects that need to extract data from
requests, or a function that receives a dotted path, anyway..
dict.get composes so nicely so why we don't have list.get too?
Well, the safe navigator can solve this as it will compose as well as any
adhoc solution with the advantage of short circuit and also it's brevity so
... For my use case, at last, safe navigator (or null coalescing) will fit
better than any list.get can do.
Regards
Em ter, 30 de jun de 2020 18:53, Juancarlo Añez <[email protected]>
escreveu:
> Hello!
>
> Which would be the use cases for this feature?
>
> I can't think of one.
>
> I think that "nice to have" leads to the ways of Perl.
>
> Regards,
>
> On Sat, Jun 27, 2020 at 9:34 AM Daniel. <[email protected]> wrote:
>
>> When I need to traverse nested dicts, is a common pattern to do
>>
>> somedict.get('foo', {}).get('bar', {})
>>
>> But there is no such equivalent for arrays, wouldn't be nice if we can
>> follow
>>
>> somedict.get('foo', {}).get('bar', []).get(10) ... ?
>>
>> What I do in this case is surround with try/except IndexError and set
>> some variable to None, or wrap the try/except block in a function. But this
>> is noise in my opinion, I just want to follow the same reasoning that I
>> have with dicts: I'm searching for some path deep nested, and want a
>> default if not found.
>>
>> What do you think?
>>
>> Regards
>> --
>> “If you're going to try, go all the way. Otherwise, don't even start.
>> ..."
>> Charles Bukowski
>> _______________________________________________
>> Python-ideas mailing list -- [email protected]
>> To unsubscribe send an email to [email protected]
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/[email protected]/message/LLK3EQ3QWNDB54SEBKJ4XEV4LXP5HVJS/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> --
> Juancarlo *Añez*
>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/6NRDIZ35DB4REHFRPUWW5VWA3DOONDMS/
Code of Conduct: http://python.org/psf/codeofconduct/