[Python-ideas] A better PEP 505 (test package: coalescing)
mertz at gnosis.cx
Mon Jul 23 23:24:25 EDT 2018
I proposed a very toy example of a coalescing class that would solve the
problem solved by brand new syntax in PEP 505. In my 5 minute version,
there were several faults or limitations. For one, the class I wrote was
misnamed, since it wasn't really about None-coalescing, but rather about
exception-free nested access.
I've written a more robust version at https://pypi.org/project/coalescing/.
I encourage issues or contributions at
https://github.com/DavidMertz/coalesce. Mind you, this is still a
few-hour-of-effort version, not rigorously tested.
I want to thank Antoine Pitrou for proposing using Graham Dumpleton's
warpt.ObjectProxy (which is truly magic). In almost no lines, I think I
have something perhaps even better than PyMaybe. In particular, most of
the time, there is no need to do an explicit `.unbox()` call... but at the
suggestion of several people, I changed my first API to optionally pass a
default value other than None within that method, if desired. I even made
laziness in getting that value the default (but switchable) behavior.
Dumpleton's magic means that boxed values can do pretty much anything the
raw values can, so can generally ignore the .unbox() call at the end.
Perhaps most magic of all, you can even ASSIGN into proxied values and that
gets written to the original value (not shown in the doctests below). This
adds something powerful that PEP 505 is completely unable to do.
I believe that I have captured ALL the semantics of PEP 505 with no changes
to Python syntax... and in a way that reads FAR better than all those new
Access messy nested data structures
GreedyAccess will keep marching down trees even if failure occurred earlier:
>>> from coalesce import GreedyAccess, make_test
>>> cfg = make_test()
<GreedyAccess proxy for 'Nightclubbing'>
>>> GreedyAccess(cfg).user.profile.song + ' and spam'
'Nightclubbing and spam'
<GreedyAccess proxy for None>
>>> GreedyAccess(cfg).user.profile.food.unbox('spam') + ' and spam'
'spam and spam'
NoneCoalesce only descends until a None is encountered. Accessing
or keys of None will still fail:
>>> from coalesce import NoneCoalesce
<NoneCoalesce proxy for 'Nightclubbing'>
Traceback (most recent call last):
AttributeError: 'types.SimpleNamespace' object has no attribute 'food'
<NoneCoalesce proxy for namespace(arms=2, song='Nightclubbing')>
>>> val = None
We provide for returning values other than None if some other default is
useful for your use case (a zero-argument lambda function would often be
>>> def say_spam():
... return "spam"
>>> GreedyAccess(cfg).user.profile.food.unbox(say_spam, lazy=False)
<function say_spam ...>
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas