[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
On Sunday, May 23, 2021, 02:23:05 PM GMT+1, Shivam Saini wrote: >> Like the first example in which I am sending an log, which isn't important. If the log is not important, then why are you sending it? ___ 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/5OSBF3W6MIZJ4TBIVH4DGUPV6GIMQMYN/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
23.05.21 12:42, Shivam Saini пише: > except: > pass Don't do this. Never write a bare except handler which does not re-raise an exception. There are few exceptions of this rule, but it is unlikely that you will see them in first years of your practice. It is an anti-pattern, and a feature that facilitates its use will never be added. Always specify exceptions which you expect to catch. If you want to look cool, you can use contextlib.suppress(), although it can make further refactoring more difficult. ___ 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/RMAQZUPACCB7IZGTIWBQ7DRDPCZ6EN7M/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
23.05.21 16:22, Shivam Saini пише: > After all, python is known for one liners It is not Python that is known for one liners. Python syntax is rather opposed to one liners. It encourages and sometimes forces a user to write well-indented code. ___ 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/75RY5BWJEJDV36EHIKPWPKQ5BZL5BSWM/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
On Sun, May 23, 2021, 12:02 PM Damian Shaw wrote: > FYI, > > Something very similar already exists in the standard library, > contextlib.suppress: > https://docs.python.org/3/library/contextlib.html#contextlib.suppress > > It makes a nice 2+ liner for a lot of situations: > > with suppress(Exception): > ... > > Seems more flexible than OPs keyword suggestion as you can fit an entire > block of code in there, not just a single expression. And it's quite > semantically expressive. > > Damian > (he/him) > I agree this is better than a decorator or the OP's idea. Didn't know this existed but I'll definitely be making use of it. > ___ 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/OL26EVXX72QAMCYGS2NDMGJNIFPW4QNR/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
On Sun, May 23, 2021, 9:35 AM Stestagg wrote: > FYI, default here is unused. > Thanks! Yes I had put that at the first and intended to remove it. ___ 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/FEKTTEBVVNYIO23DVS6ISDU5NWLNGLM6/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
On 2021-05-24 at 01:34:29 +1000, Steven D'Aprano wrote: > On Sun, May 23, 2021 at 06:52:38PM +0530, Shivam Saini wrote: > > > After all, python is known for one liners and this would be an another > > great one liner if implemented. > > Python isn't known for one-liners. You might be thinking of Perl. I agree. > Being known for one-liners is a bad thing. It means that your language > is famous for being written in an obfuscated, hard to read, hard to > maintain, style. Being known for one-liners can (*can*, not must) mean that my language has the right features, abstractions, and APIs for common use cases and task(s) at hand. Most programs are composed of multiple one-liners. ;-) Any program can be one line long if I put all the details into a library function. That said, a [standard] library that contains everything is not a good goal. ___ 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/A7UJZXGCSKMZAVFWHXKNX7FCOAVCD33C/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
FYI, Something very similar already exists in the standard library, contextlib.suppress: https://docs.python.org/3/library/contextlib.html#contextlib.suppress It makes a nice 2+ liner for a lot of situations: with suppress(Exception): ... Seems more flexible than OPs keyword suggestion as you can fit an entire block of code in there, not just a single expression. And it's quite semantically expressive. Damian (he/him) On Sun, May 23, 2021 at 9:28 AM Ricky Teachey via Python-ideas < python-ideas@python.org> wrote: > I think you can already do all of this with a custom exception-swallowing > decorator function. > > Something like this: > > from functools import wraps > > def swallow(*exceptions, default=Exception, result=None): > if not exceptions: > exceptions = Exception, > def decorator(func): > @wraps(func) > def wrapped(*args, **kwargs): > try: > return func(*args, **kwargs) > except exceptions: > return result > return wrapped > return decorator > > Then just write: > > @swallower() > def some_func(): > log_to_telegram('This is a not important message. It's okay even if > it isn\'t delivered.') > > For divide by zero errors you need to put the division operation in a > lambda function: > > safe_divide = swallow(DivideByZero, result=0) > division = safe_divide(lambda: a/b) > > I didn't test any of this but it should be working. > > On Sun, May 23, 2021, 8:54 AM Shivam Saini wrote: > >> Sometimes, we need to execute a statement, which might throw an exception >> and we want to return None if it causes an exception. In short, we want to >> run that command as failsafe mode, so that it doesn't cause abnormal >> termination in case of any exception. >> >> *Approach till now:* >> >> def log_to_telegram(text): >> ''' Suppose you want to send some log to some telegram channel using >> telegram bot api. It might raise Network Error, or Invalid Bot token error. >> ''' >> send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a >> function wrapper for telegram bot api. >> >> def some_func(): >> try: >> log_to_telegram('This is a not important message. It's okay even >> if it isn\'t delivered.') >> except: >> pass >> # Next work >> >> *Suggested approach:* >> >> # consider the same log_to_telegram function as defined in previous >> example >> >> def some_func(): >> safe log_to_telegram('This is a not important message. It's okay even >> if it isn\'t delivered.') >> # safe keyword ensures that it raises no exception and the process >> isn't terminated even if log_to_telegram function raises an exception. >> >> *Other examples:* >> >> *1. By default, it will return None.* >> *Example: * >> try: >>file = open('some_file') >>except: >>file = None >> *Can be rewritten as:* >>file = safe open('some_file') >> >> *2. We can provide a return value explicitly which should be returned in >> case of some exception.* >> *Example: * >> try: >>division = a/b >>except: >>division = 0 >> *Can be rewritten as:* >> division = safe(0) a/b >> >> *3. We can set what exceptions we want to be 'safed'.* >> *Example: * >> try: >>element = some_list[index]/divisor >>except ZeroDivisionError: >>element = 0 # It will make element zero if divisor is zero, >> but it will not except IndexError in case index is out of range for >> some_list >> *Can be rewritten as:* >>element = safe(0, ZeroDivisionError) some_list[index]/divisor >> >> *I think this keyword should be there in python, it will make code short >> and readable. * >> ___ >> 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/5SEDN6AUM7LZPZZJJZ3RMAKHKHN6N665/ >> Code of Conduct: http://python.org/psf/codeofconduct/ >> >> ___ > 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/ORKXAAH2EZFYHJMVU74H5R6BSJVMMER7/ > Code of Conduct: http://python.org/psf/codeofconduct/ > ___ 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/D5SCJFV3ASC5VJTCQ2ENGCVGALQCOXGT/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
On Sun, May 23, 2021 at 06:52:38PM +0530, Shivam Saini wrote: > After all, python is known for one liners and this would be an another > great one liner if implemented. Python isn't known for one-liners. You might be thinking of Perl. Being known for one-liners is a bad thing. It means that your language is famous for being written in an obfuscated, hard to read, hard to maintain, style. -- Steve ___ 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/WMKBEFPOQGQJPAOALXWD23H77IORWXS6/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
FYI, default here is unused. On Sun, 23 May 2021 at 14:29, Ricky Teachey via Python-ideas < python-ideas@python.org> wrote: > I think you can already do all of this with a custom exception-swallowing > decorator function. > > Something like this: > > from functools import wraps > > def swallow(*exceptions, default=Exception, result=None): > if not exceptions: > exceptions = Exception, > def decorator(func): > @wraps(func) > def wrapped(*args, **kwargs): > try: > return func(*args, **kwargs) > except exceptions: > return result > return wrapped > return decorator > > Then just write: > > @swallower() > def some_func(): > log_to_telegram('This is a not important message. It's okay even if > it isn\'t delivered.') > > For divide by zero errors you need to put the division operation in a > lambda function: > > safe_divide = swallow(DivideByZero, result=0) > division = safe_divide(lambda: a/b) > > I didn't test any of this but it should be working. > > On Sun, May 23, 2021, 8:54 AM Shivam Saini wrote: > >> Sometimes, we need to execute a statement, which might throw an exception >> and we want to return None if it causes an exception. In short, we want to >> run that command as failsafe mode, so that it doesn't cause abnormal >> termination in case of any exception. >> >> *Approach till now:* >> >> def log_to_telegram(text): >> ''' Suppose you want to send some log to some telegram channel using >> telegram bot api. It might raise Network Error, or Invalid Bot token error. >> ''' >> send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a >> function wrapper for telegram bot api. >> >> def some_func(): >> try: >> log_to_telegram('This is a not important message. It's okay even >> if it isn\'t delivered.') >> except: >> pass >> # Next work >> >> *Suggested approach:* >> >> # consider the same log_to_telegram function as defined in previous >> example >> >> def some_func(): >> safe log_to_telegram('This is a not important message. It's okay even >> if it isn\'t delivered.') >> # safe keyword ensures that it raises no exception and the process >> isn't terminated even if log_to_telegram function raises an exception. >> >> *Other examples:* >> >> *1. By default, it will return None.* >> *Example: * >> try: >>file = open('some_file') >>except: >>file = None >> *Can be rewritten as:* >>file = safe open('some_file') >> >> *2. We can provide a return value explicitly which should be returned in >> case of some exception.* >> *Example: * >> try: >>division = a/b >>except: >>division = 0 >> *Can be rewritten as:* >> division = safe(0) a/b >> >> *3. We can set what exceptions we want to be 'safed'.* >> *Example: * >> try: >>element = some_list[index]/divisor >>except ZeroDivisionError: >>element = 0 # It will make element zero if divisor is zero, >> but it will not except IndexError in case index is out of range for >> some_list >> *Can be rewritten as:* >>element = safe(0, ZeroDivisionError) some_list[index]/divisor >> >> *I think this keyword should be there in python, it will make code short >> and readable. * >> ___ >> 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/5SEDN6AUM7LZPZZJJZ3RMAKHKHN6N665/ >> Code of Conduct: http://python.org/psf/codeofconduct/ >> >> ___ > 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/ORKXAAH2EZFYHJMVU74H5R6BSJVMMER7/ > Code of Conduct: http://python.org/psf/codeofconduct/ > ___ 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/HRY4LIZHVXUAGSRVUDEA3IHBPW3YB7MR/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
That wont be an oneliner still. We can add decorator to a function, and that don't even be very readable. If we can convert that decorator to an inbuilt keyword that would work as an one liner and would be very readable too. On Sun, 23 May 2021, 18:56 Ricky Teachey, wrote: > I think you can already do all of this with a custom exception-swallowing > decorator function. > > Something like this: > > from functools import wraps > > def swallow(*exceptions, default=Exception, result=None): > if not exceptions: > exceptions = Exception, > def decorator(func): > @wraps(func) > def wrapped(*args, **kwargs): > try: > return func(*args, **kwargs) > except exceptions: > return result > return wrapped > return decorator > > Then just write: > > @swallower() > def some_func(): > log_to_telegram('This is a not important message. It's okay even if > it isn\'t delivered.') > > For divide by zero errors you need to put the division operation in a > lambda function: > > safe_divide = swallow(DivideByZero, result=0) > division = safe_divide(lambda: a/b) > > I didn't test any of this but it should be working. > > On Sun, May 23, 2021, 8:54 AM Shivam Saini wrote: > >> Sometimes, we need to execute a statement, which might throw an exception >> and we want to return None if it causes an exception. In short, we want to >> run that command as failsafe mode, so that it doesn't cause abnormal >> termination in case of any exception. >> >> *Approach till now:* >> >> def log_to_telegram(text): >> ''' Suppose you want to send some log to some telegram channel using >> telegram bot api. It might raise Network Error, or Invalid Bot token error. >> ''' >> send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a >> function wrapper for telegram bot api. >> >> def some_func(): >> try: >> log_to_telegram('This is a not important message. It's okay even >> if it isn\'t delivered.') >> except: >> pass >> # Next work >> >> *Suggested approach:* >> >> # consider the same log_to_telegram function as defined in previous >> example >> >> def some_func(): >> safe log_to_telegram('This is a not important message. It's okay even >> if it isn\'t delivered.') >> # safe keyword ensures that it raises no exception and the process >> isn't terminated even if log_to_telegram function raises an exception. >> >> *Other examples:* >> >> *1. By default, it will return None.* >> *Example: * >> try: >>file = open('some_file') >>except: >>file = None >> *Can be rewritten as:* >>file = safe open('some_file') >> >> *2. We can provide a return value explicitly which should be returned in >> case of some exception.* >> *Example: * >> try: >>division = a/b >>except: >>division = 0 >> *Can be rewritten as:* >> division = safe(0) a/b >> >> *3. We can set what exceptions we want to be 'safed'.* >> *Example: * >> try: >>element = some_list[index]/divisor >>except ZeroDivisionError: >>element = 0 # It will make element zero if divisor is zero, >> but it will not except IndexError in case index is out of range for >> some_list >> *Can be rewritten as:* >>element = safe(0, ZeroDivisionError) some_list[index]/divisor >> >> *I think this keyword should be there in python, it will make code short >> and readable. * >> ___ >> 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/5SEDN6AUM7LZPZZJJZ3RMAKHKHN6N665/ >> Code of Conduct: http://python.org/psf/codeofconduct/ >> >> ___ 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/CF5OOOSVZJUGFQF7B3EDEPKIRCCEYGQD/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
I think you can already do all of this with a custom exception-swallowing decorator function. Something like this: from functools import wraps def swallow(*exceptions, default=Exception, result=None): if not exceptions: exceptions = Exception, def decorator(func): @wraps(func) def wrapped(*args, **kwargs): try: return func(*args, **kwargs) except exceptions: return result return wrapped return decorator Then just write: @swallower() def some_func(): log_to_telegram('This is a not important message. It's okay even if it isn\'t delivered.') For divide by zero errors you need to put the division operation in a lambda function: safe_divide = swallow(DivideByZero, result=0) division = safe_divide(lambda: a/b) I didn't test any of this but it should be working. On Sun, May 23, 2021, 8:54 AM Shivam Saini wrote: > Sometimes, we need to execute a statement, which might throw an exception > and we want to return None if it causes an exception. In short, we want to > run that command as failsafe mode, so that it doesn't cause abnormal > termination in case of any exception. > > *Approach till now:* > > def log_to_telegram(text): > ''' Suppose you want to send some log to some telegram channel using > telegram bot api. It might raise Network Error, or Invalid Bot token error. > ''' > send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a > function wrapper for telegram bot api. > > def some_func(): > try: > log_to_telegram('This is a not important message. It's okay even > if it isn\'t delivered.') > except: > pass > # Next work > > *Suggested approach:* > > # consider the same log_to_telegram function as defined in previous example > > def some_func(): > safe log_to_telegram('This is a not important message. It's okay even > if it isn\'t delivered.') > # safe keyword ensures that it raises no exception and the process > isn't terminated even if log_to_telegram function raises an exception. > > *Other examples:* > > *1. By default, it will return None.* > *Example: * > try: >file = open('some_file') >except: >file = None > *Can be rewritten as:* >file = safe open('some_file') > > *2. We can provide a return value explicitly which should be returned in > case of some exception.* > *Example: * > try: >division = a/b >except: >division = 0 > *Can be rewritten as:* > division = safe(0) a/b > > *3. We can set what exceptions we want to be 'safed'.* > *Example: * > try: >element = some_list[index]/divisor >except ZeroDivisionError: >element = 0 # It will make element zero if divisor is zero, > but it will not except IndexError in case index is out of range for > some_list > *Can be rewritten as:* >element = safe(0, ZeroDivisionError) some_list[index]/divisor > > *I think this keyword should be there in python, it will make code short > and readable. * > ___ > 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/5SEDN6AUM7LZPZZJJZ3RMAKHKHN6N665/ > Code of Conduct: http://python.org/psf/codeofconduct/ > > ___ 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/ORKXAAH2EZFYHJMVU74H5R6BSJVMMER7/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
That's what I think it should be for. I know safe open(...) isn't a really good example for this, but I had just used that for demonstration purposes. Instead what I am saying is that sometimes we just don't care even if an statement raises exception. Like the first example in which I am sending an log, which isn't important. And in any case, the original try except keywords will still be there. Also I read the similar idea you mentioned, and I guess that is also a good way to implement it. We can replace *safe telegram_log("...")* with one liner: *telegram_log("...") except: None* After all, python is known for one liners and this would be an another great one liner if implemented. ___ 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/RU3LPYKTYSTJCXUCEIUSG2GCBPZAP6UY/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.
sounds very much like https://www.python.org/dev/peps/pep-0463/#rejection-notice I'm concerned with the `safe` defaulting to a bare `except:` which will also catch CancelledError other errors that should be re-raised also ``` file = safe open('some_file') ``` does not provide a way to manage the file with a context manager: ``` f = safe open("some_file") if f is None: # do something else: with f: # do something else ``` seems no improvement than the current: ``` try: f = open("some_file") except OSError: # do something else: with f: # do something else ``` ___ 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/7MN4G5INW2CZXVMC7KHHUMXIZ3ZZNDBZ/ Code of Conduct: http://python.org/psf/codeofconduct/