mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-14 04:24:52 +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:
@@ -5,6 +5,7 @@
|
||||
import weakref
|
||||
from collections import deque, namedtuple
|
||||
from functools import partial
|
||||
from contextlib import suppress
|
||||
|
||||
from .borders import Borders
|
||||
from .child import Child
|
||||
@@ -28,10 +29,8 @@ def SpecialWindow(cmd, stdin=None, override_title=None, cwd_from=None, cwd=None,
|
||||
|
||||
|
||||
def add_active_id_to_history(items, item_id, maxlen=64):
|
||||
try:
|
||||
with suppress(ValueError):
|
||||
items.remove(item_id)
|
||||
except ValueError:
|
||||
pass
|
||||
items.append(item_id)
|
||||
if len(items) > maxlen:
|
||||
items.popleft()
|
||||
|
||||
Reference in New Issue
Block a user