On Fri, Aug 7, 2020 at 5:24 PM Paul Moore <p.f.mo...@gmail.com> wrote:
> On Fri, 7 Aug 2020 at 16:21, David Mertz <me...@gnosis.cx> wrote: > > > > On Fri, Aug 7, 2020 at 4:58 AM Brendan Barnwell <brenb...@brenbarn.net> > wrote: > >> > >> It seems that the rationale that was used in the PEP was fairly > >> narrowly focused on the comparison with things like dict.get() and the > >> idea of EAFP. A somewhat broader justification might be something along > >> these lines: > > > > > > For an example. Anyone is free to use, but I'm not claiming it's > necessarily the best. This is from... well, probably not yesterday like I > said in other comment, but a couple days ago. The module `jsonschema` has > an API where it raises an exception if `validate()` doesn't succeed (None > if things are happy). I don't love that API, so want to wrap it. > > > > def not_valid(instance, schema): > > try: > > return validate(instance, schema) > > except ValidationError as err: > > return str(err) > > > > I really wanted that to be one line rather than a helper function, and > it really feels like it should be possible... and yet. > > I did basically the same yesterday: > > def is_valid_specifier(s): > try: > packaging.specifiers.SpecifierSet(s) > return True > except packahing.specifiers.InvalidSpecifier: > return False > > The function was only for use in a [s for s in strings if > is_valid_specifier(s)] comprehension, so an in-line expression would > have been ideal. > > Paul > > David, your example involves capturing the exception which was deferred in the PEP: https://www.python.org/dev/peps/pep-0463/#capturing-the-exception-object Paul, do you want to write `[s for s in strings if (packaging.specifiers.SpecifierSet(s) except packaging.specifiers.InvalidSpecifier: False)]`? That's a mouthful.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/IE6NQ467ASSBJSCLQNHWX7UWR4YRNRPN/ Code of Conduct: http://python.org/psf/codeofconduct/