Hey,
I just found a good example for the unpacking errors I suggested:
def topological_sort(graph: networkx.DiGraph) -> tuple:
try:
(zero_node,) = (node for node in graph if not
graph.neighbors(node))
except TooFewToUnpackError:
raise OverdefinedOrdering
except TooManyToUnpackError:
raise UnderdefinedOrdering
...
This is instead of the following code, which I find less elegant:
def topological_sort(graph: networkx.DiGraph) -> tuple:
zero_nodes = tuple(node for node in graph if not
graph.neighbors(node))
if not zero_nodes:
raise OverdefinedOrdering
elif len(zero_nodes) >= 2:
raise UnderdefinedOrdering
else:
(zero_node,) = zero_nodes
Besides elegance, the above code can be optimized with short-circuit logic
for the TooManyToUnpackError, assuming that doesn't break backward
compatibility.
...
On Sun, May 3, 2020 at 1:39 AM Soni L. <[email protected]> wrote:
>
>
> On 2020-05-02 7:29 p.m., Steven D'Aprano wrote:
> > On Sat, May 02, 2020 at 03:39:50PM -0300, Soni L. wrote:
> >
> > > def foo():
> > > yield from ()
> > > raise ValueError
> >
> > def foo():
> > yield from ()
> > raise ValueUnpackingError
> >
> > Does that help?
> >
> >
> the idea is that it'd become a RuntimeError when you unpack it.
> _______________________________________________
> 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/GQVFEDUZGVND7Q2ZCTVDSHOXWWUOCQDU/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
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/55J446VK7534LKCIJIZZOK37Z3T2QZ64/
Code of Conduct: http://python.org/psf/codeofconduct/