mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-16 21:45:03 +02:00
Use "with suppress()" to suppress python exceptions
Using
```Python
with suppress(OSError):
os.remove('somefile.tmp')
```
instead of
```Python
try:
os.remove('somefile.tmp')
except OSError:
pass
```
makes the code more compact and more readable IMO.
This pattern was recommended by Raymond Hettinger, a Python Core
Developer in his talk "Transforming Code into Beautiful, Idiomatic Python" at https://www.youtube.com/watch?v=OSGv2VnC0go. The transcript is available at https://github.com/JeffPaine/beautiful_idiomatic_python
This commit is contained in:
@@ -6,6 +6,7 @@ import os
|
||||
import sys
|
||||
import zlib
|
||||
from base64 import standard_b64encode
|
||||
from contextlib import suppress
|
||||
|
||||
write = getattr(sys.stdout, 'buffer', sys.stdout).write
|
||||
|
||||
@@ -62,10 +63,8 @@ def main():
|
||||
move_cursor(0, 21)
|
||||
print('Photo...')
|
||||
display_png_file(photo)
|
||||
try:
|
||||
with suppress(EOFError, KeyboardInterrupt):
|
||||
input()
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user