Re: [Python-ideas] Exception for developer errors?
On 10Apr2019 23:09, Stefano Borini wrote: I occasionally found situations where I want to raise an exception for errors that can only arise because the developer made a mistake, for example: - an abstract method is supposed to be reimplemented and its execution is supposed to leave some internal constraints of an object unchanged, but these are instead violated. As mentioned, AssertionErrors are good for this. I also use the icontract PyPI library, which provides decorators for annotating functions with preconditions and postconditions, and which are disabled in the same circumstances where assertions are disabled. It produces nice exception messages, too, aiding debugging. Also, it inspects the function definition, and so your preconditions use the same parameter names as in the function header. - an impossible "else" condition after an if/elif, where the else cannot simply happen unless someone really screwed up the internal state of the object. That I tend to use RuntimeError for. I accept that my criteria for this difference are nebulous. I think in my mind: from icontract import require @require(lambda s: s in ('a', 'b', 'c')) def f(s, z): if s == 'a': ... elif s == 'b': ... else: raise RuntimeError("valid input s=%r, but unhandled!" % s) The @require is an assertion that the _caller_ used us correctly. The RuntimeError means that _I_, the function implementor, have screwed up right here instead of in the larger programme. Cheers, Cameron Simpson ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Exception for developer errors?
On Thu, Apr 11, 2019 at 8:15 AM Jeroen Demeyer wrote: > > On 2019-04-11 00:09, Stefano Borini wrote: > > I occasionally found situations where I want to raise an exception for > > errors that can only arise because the developer made a mistake, for > > example: > > I use AssertionError for this. An assertion failure means "this is a > bug", so that seems the right choice to me. You don't need to use an > actual assert statement, you can manually raise AssertionError too. > Agreed. It's worth noting that AssertionError isn't affected by the -O flag - only the assert *statement*. Also, anything that says "except AssertionError:" (outside of unit testing) should be considered a major bug, which in turn means that this should *only* be raised when you truly expect that normal usage cannot ever hit this. Which is perfect for the use-case you describe. ChrisA ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Exception for developer errors?
That's quite a good idea, but then I think it should be more explicit in the documentation that the purpose goes beyond the assert statement failure. I've never seen AssertionError raised manually. On Wed, 10 Apr 2019 at 23:15, Jeroen Demeyer wrote: > > On 2019-04-11 00:09, Stefano Borini wrote: > > I occasionally found situations where I want to raise an exception for > > errors that can only arise because the developer made a mistake, for > > example: > > I use AssertionError for this. An assertion failure means "this is a > bug", so that seems the right choice to me. You don't need to use an > actual assert statement, you can manually raise AssertionError too. > > > Jeroen. > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ -- Kind regards, Stefano Borini ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Exception for developer errors?
On 2019-04-11 00:09, Stefano Borini wrote: I occasionally found situations where I want to raise an exception for errors that can only arise because the developer made a mistake, for example: I use AssertionError for this. An assertion failure means "this is a bug", so that seems the right choice to me. You don't need to use an actual assert statement, you can manually raise AssertionError too. Jeroen. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Exception for developer errors?
You have AssertionError for that. Elazar בתאריך יום ה׳, 11 באפר׳ 2019, 1:10, מאת Stefano Borini < stefano.bor...@gmail.com>: > I occasionally found situations where I want to raise an exception for > errors that can only arise because the developer made a mistake, for > example: > > - an abstract method is supposed to be reimplemented and its execution > is supposed to leave some internal constraints of an object unchanged, > but these are instead violated. > - an impossible "else" condition after an if/elif, where the else > cannot simply happen unless someone really screwed up the internal > state of the object. > > In general, when these cases happen, I use RuntimeError, but other > people may choose otherwise. I've also seen ValueError, plain > Exception or a specifically made subclass used in these cases. > Whatever the choice is, it generally lacks clarity of communication to > whoever receives it. > RuntimeError is a rather generic exception according to the > documentation, and you can only rely on the message, which relies on > writing something appropriate for the situation: > > ``` > exception RuntimeError > Raised when an error is detected that doesn’t fall in any of the other > categories. The associated value is a string indicating what precisely > went wrong. > ``` > > while it would be useful to communicate clearly to whoever is using or > modifying the code "listen, it's your mistake, you misunderstood how I > work or you ruined my internals, leading me to an impossible state". > Also, customers seeing this kind of exception would understand without > a doubt that the application is broken because of an internal error. > > I tried some search on the mailing list but could not find anything at > a glance about this topic. Was this already discussed in the past? > > Thanks > > -- > Kind regards, > > Stefano Borini > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Exception for developer errors?
I occasionally found situations where I want to raise an exception for errors that can only arise because the developer made a mistake, for example: - an abstract method is supposed to be reimplemented and its execution is supposed to leave some internal constraints of an object unchanged, but these are instead violated. - an impossible "else" condition after an if/elif, where the else cannot simply happen unless someone really screwed up the internal state of the object. In general, when these cases happen, I use RuntimeError, but other people may choose otherwise. I've also seen ValueError, plain Exception or a specifically made subclass used in these cases. Whatever the choice is, it generally lacks clarity of communication to whoever receives it. RuntimeError is a rather generic exception according to the documentation, and you can only rely on the message, which relies on writing something appropriate for the situation: ``` exception RuntimeError Raised when an error is detected that doesn’t fall in any of the other categories. The associated value is a string indicating what precisely went wrong. ``` while it would be useful to communicate clearly to whoever is using or modifying the code "listen, it's your mistake, you misunderstood how I work or you ruined my internals, leading me to an impossible state". Also, customers seeing this kind of exception would understand without a doubt that the application is broken because of an internal error. I tried some search on the mailing list but could not find anything at a glance about this topic. Was this already discussed in the past? Thanks -- Kind regards, Stefano Borini ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Starap function exists but it seems there's not such thing as "doublestarmap"
10.04.19 12:55, Krokosh Nikita пише: I need smth like starstarmap('{a} / {b}/ {c}'.format, [{a:1, b:2, c:3}, {a:4, b:5, c:6}, ...]) Use the format_map method of str. >>> list(map('{a} / {b}/ {c}'.format_map, [{'a':1, 'b':2, 'c':3}, {'a':4, 'b':5, 'c':6}])) ['1 / 2/ 3', '4 / 5/ 6'] ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Starap function exists but it seems there's not such thing as "doublestarmap"
> On 10 Apr 2019, at 11:55, Krokosh Nikita wrote: > > I need smth like starstarmap('{a} / {b}/ {c}'.format, [{a:1, b:2, c:3}, {a:4, > b:5, c:6}, ...]) Seems overly specific. Why not merge the dicts then call formal like normal? >> On 4/10/19 7:48 PM, Anders Hovmöller wrote: >> I don't really understand. You can do: >> >> '{a} {b}'.format(**{'a': 1}, **{'b': 2}) >> >> Is that what you want? >> >>> On 10 Apr 2019, at 11:09, Krokosh Nikita wrote: >>> >>> Hello. I have a following question: How come there's no such thing in >>> Python like starmap but which unpacks dicts as kwargs for fuction? >>> >>> For example I have a format string like "{param1}, {param2}" and want to >>> get results passing list of dicts for it's .format(). >>> >>> Of course I can do that with genexpr or some lambda with map. >>> >>> But can you clarify why starmap version of map fuction exists and >>> doublestarmap doesn't? >>> >>> >>> Best Regards. >>> >>> ___ >>> Python-ideas mailing list >>> Python-ideas@python.org >>> https://mail.python.org/mailman/listinfo/python-ideas >>> Code of Conduct: http://python.org/psf/codeofconduct/ ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Starap function exists but it seems there's not such thing as "doublestarmap"
robertvandeneynde.be Le mer. 10 avr. 2019 à 12:55, Krokosh Nikita a écrit : > I need smth like starstarmap('{a} / {b}/ {c}'.format, [{a:1, b:2, c:3}, > {a:4, b:5, c:6}, ...]) > That's def starstarmap(f, it): return (f(**x) for x in it) That looks like a recipe, not a basis function ^^ ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Starap function exists but it seems there's not such thing as "doublestarmap"
I need smth like starstarmap('{a} / {b}/ {c}'.format, [{a:1, b:2, c:3}, {a:4, b:5, c:6}, ...]) On 4/10/19 7:48 PM, Anders Hovmöller wrote: I don't really understand. You can do: '{a} {b}'.format(**{'a': 1}, **{'b': 2}) Is that what you want? On 10 Apr 2019, at 11:09, Krokosh Nikita wrote: Hello. I have a following question: How come there's no such thing in Python like starmap but which unpacks dicts as kwargs for fuction? For example I have a format string like "{param1}, {param2}" and want to get results passing list of dicts for it's .format(). Of course I can do that with genexpr or some lambda with map. But can you clarify why starmap version of map fuction exists and doublestarmap doesn't? Best Regards. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/ ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Starap function exists but it seems there's not such thing as "doublestarmap"
I don't really understand. You can do: '{a} {b}'.format(**{'a': 1}, **{'b': 2}) Is that what you want? > On 10 Apr 2019, at 11:09, Krokosh Nikita wrote: > > Hello. I have a following question: How come there's no such thing in Python > like starmap but which unpacks dicts as kwargs for fuction? > > For example I have a format string like "{param1}, {param2}" and want to get > results passing list of dicts for it's .format(). > > Of course I can do that with genexpr or some lambda with map. > > But can you clarify why starmap version of map fuction exists and > doublestarmap doesn't? > > > Best Regards. > > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Starap function exists but it seems there's not such thing as "doublestarmap"
Hello. I have a following question: How come there's no such thing in Python like starmap but which unpacks dicts as kwargs for fuction? For example I have a format string like "{param1}, {param2}" and want to get results passing list of dicts for it's .format(). Of course I can do that with genexpr or some lambda with map. But can you clarify why starmap version of map fuction exists and doublestarmap doesn't? Best Regards. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Sorted lists
It surely would be helpful. I still find it a bit too single-case oriented. Maybe having an equivalent __sorting_algo__ property with a value of the current sorting algorithm would be more general? There could be a SortingAlgo base class, which could be extended into classes like: - SortingAlgoNone(SortingAlgo) or SortingAlgoUnsorted(SortingAlgo) which would be the default for non-sorted lists (or just the value None) - SortingAlgoAscending(SortingAlgo) - SortingAlgoAscendingNumbers(SortingAlgoAscending) - MyCustomSortingAlgo(SortingAlgo) - ... It would allow to mark a list as sorted with any algorithm, and of course, any code that would use these lists would be able to read/write this __sorting_algo__. And complementary idea, we could have an extra arg to sort() (and other functions like this one) like `trust_declared_algo=True`, that if set would only sort the list if its list.__sorting_algo__ is compatible (a subclass of the sorting algo it uses, or the class itself). The rest of the behaviours (when the __sorting_algo__ would be set or reset) would be as described by Steven in the original proposal. -Brice Le 8/4/19 à 4:32, Steven D'Aprano a écrit : There are quite a few important algorithms which require lists to be sorted. For example, the bisect module, and for statistics median and other quantiles. Sorting a list is potentially expensive: while Timsort is very efficient, it is still ultimately an O(N log N) algorithm which limits how efficient it can be. Checking whether a list is sorted is O(N). What if we could check that lists were sorted in constant time? Proposal: let's give lists a dunder flag, __issorted__, that tracks whether the list is definitely sorted or not: - Empty lists, or lists with a single item, are created with __issorted__ = True; lists with two or more items are created with the flag set to False. - Appending or inserting items sets the flag to False. - Deleting or popping items doesn't change the flag. - Reversing the list doesn't change the flag. - Sorting it sets the flag to True. (The sort method should NOT assume the list is sorted just because the flag is set.) Functions that require the list to be sorted can use the flag as a quick check: if not alist.__issorted__: alist.sort() ... The flag will be writable, so that functions such as those in bisect can mark that they have kept the sorted invariant: bisect.insort(alist, x) assert alist.__issorted__ Being writable, the flag is advisory, not a guarantee, and "consenting adults" applies. People can misuse the flag: alist = [1, 4, 2, 0, 5] alist.__issorted__ = True but then they have nobody to blame but themselves if they shoot themselves in the foot. That's no worse than the situation we have now, were you might pass an unsorted list to bisect. The flag doesn't guarantee that the list is sorted the way you want (e.g. biggest to smallest, by some key, etc) only that it has been sorted. Its up to the user to ensure they sort it the right way: # Don't do this and expect it to work! alist.sort(key=random.random) bisect.insort(alist, 1) If you really want to be sure about the state of the list, you have to make a copy and sort it. But that's no different from the situation right now. But for those willing to assume "consenting adults", you might trust the flag and avoid sorting. Downsides: - Every list grows an extra attribute; however, given that lists are already quite big data structures and are often over-allocated, I don't think this will matter much. - insert(), append(), extend(), __setitem__() will be a tiny bit slower due to the need to set the flag. Thoughts? ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/