From c78ee48d36bac7fe76ae500ed3aa3f5e20a9f74b Mon Sep 17 00:00:00 2001 From: nfnty Date: Thu, 26 Jan 2017 06:13:29 +0100 Subject: Only use `hasattr` where absolutely needed Use `try-except` when it's an explicit error. Fixes #780 --- ranger/config/commands.py | 2 +- ranger/container/directory.py | 4 ---- ranger/container/fsobject.py | 4 ++-- ranger/core/actions.py | 2 +- ranger/core/fm.py | 3 +-- ranger/gui/colorscheme.py | 3 +-- ranger/gui/displayable.py | 3 +-- ranger/gui/ui.py | 8 +++----- ranger/gui/widgets/browsercolumn.py | 2 +- 9 files changed, 11 insertions(+), 20 deletions(-) diff --git a/ranger/config/commands.py b/ranger/config/commands.py index cd9e0444..b88d1e24 100755 --- a/ranger/config/commands.py +++ b/ranger/config/commands.py @@ -463,7 +463,7 @@ class default_linemode(Command): self.fm.default_linemodes.appendleft(entry) # Redraw the columns - if hasattr(self.fm.ui, "browser"): + if self.fm.ui.browser: for col in self.fm.ui.browser.columns: col.need_redraw = True diff --git a/ranger/container/directory.py b/ranger/container/directory.py index 22901006..d3c0e885 100644 --- a/ranger/container/directory.py +++ b/ranger/container/directory.py @@ -557,11 +557,7 @@ class Directory( # pylint: disable=too-many-instance-attributes,too-many-public Accumulator.move_to_obj(self, arg, attr='path') def search_fnc(self, fnc, offset=1, forward=True): - if not hasattr(fnc, '__call__'): - return False - length = len(self) - if forward: generator = ((self.pointer + (x + offset)) % length for x in range(length - 1)) diff --git a/ranger/container/fsobject.py b/ranger/container/fsobject.py index 59807dfb..facf3b96 100644 --- a/ranger/container/fsobject.py +++ b/ranger/container/fsobject.py @@ -19,9 +19,9 @@ from ranger.ext.lazy_property import lazy_property from ranger.ext.human_readable import human_readable # Python 2 compatibility -if hasattr(str, 'maketrans'): +try: maketrans = str.maketrans # pylint: disable=invalid-name,no-member -else: +except AttributeError: from string import maketrans # pylint: disable=no-name-in-module diff --git a/ranger/core/actions.py b/ranger/core/actions.py index 6c9e419b..84f0fdde 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -589,7 +589,7 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m def pager_close(self): if self.ui.pager.visible: self.ui.close_pager() - if hasattr(self.ui.browser, 'pager') and self.ui.browser.pager.visible: + if self.ui.browser.pager and self.ui.browser.pager.visible: self.ui.close_embedded_pager() def taskview_open(self): diff --git a/ranger/core/fm.py b/ranger/core/fm.py index 7ad2cbdf..5ab3e5b2 100644 --- a/ranger/core/fm.py +++ b/ranger/core/fm.py @@ -350,7 +350,6 @@ class FM(Actions, # pylint: disable=too-many-instance-attributes ui = self.ui throbber = ui.throbber loader = self.loader - has_throbber = hasattr(ui, 'throbber') zombies = self.run.zombies ranger.api.hook_ready(self) @@ -358,7 +357,7 @@ class FM(Actions, # pylint: disable=too-many-instance-attributes try: # pylint: disable=too-many-nested-blocks while True: loader.work() - if has_throbber: + if throbber: if loader.has_work(): throbber(loader.status) else: diff --git a/ranger/gui/colorscheme.py b/ranger/gui/colorscheme.py index 75a82146..4e26c81e 100644 --- a/ranger/gui/colorscheme.py +++ b/ranger/gui/colorscheme.py @@ -132,8 +132,7 @@ def _colorscheme_name_to_class(signal): # pylint: disable=too-many-branches __import__(scheme_supermodule, globals(), locals(), [scheme_name], 0), scheme_name) if usecustom: allow_access_to_confdir(ranger.args.confdir, False) - if hasattr(scheme_module, 'Scheme') \ - and is_scheme(scheme_module.Scheme): + if hasattr(scheme_module, 'Scheme') and is_scheme(scheme_module.Scheme): signal.value = scheme_module.Scheme() else: for var in scheme_module.__dict__.values(): diff --git a/ranger/gui/displayable.py b/ranger/gui/displayable.py index 926fd857..1f4b7397 100644 --- a/ranger/gui/displayable.py +++ b/ranger/gui/displayable.py @@ -103,8 +103,7 @@ class Displayable( # pylint: disable=too-many-instance-attributes def destroy(self): """Called when the object is destroyed.""" - if hasattr(self, 'win'): - del self.win + self.win = None def contains_point(self, y, x): """Test whether the point lies inside this object. diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py index cbdcabf0..cb45377f 100644 --- a/ranger/gui/ui.py +++ b/ranger/gui/ui.py @@ -167,9 +167,7 @@ class UI( # pylint: disable=too-many-instance-attributes,too-many-public-method def handle_key(self, key): """Handles key input""" - - if hasattr(self, 'hint'): - self.hint() + self.hint() if key < 0: self.keybuffer.clear() @@ -366,7 +364,7 @@ class UI( # pylint: disable=too-many-instance-attributes,too-many-public-method def draw_images(self): if self.pager.visible: self.pager.draw_image() - elif hasattr(self.browser, 'pager'): + elif self.browser.pager: if self.browser.pager.visible: self.browser.pager.draw_image() else: @@ -439,7 +437,7 @@ class UI( # pylint: disable=too-many-instance-attributes,too-many-public-method self.status.hint = text def get_pager(self): - if hasattr(self.browser, 'pager') and self.browser.pager.visible: + if self.browser.pager and self.browser.pager.visible: return self.browser.pager return self.pager diff --git a/ranger/gui/widgets/browsercolumn.py b/ranger/gui/widgets/browsercolumn.py index b0435785..5a656ba0 100644 --- a/ranger/gui/widgets/browsercolumn.py +++ b/ranger/gui/widgets/browsercolumn.py @@ -387,7 +387,7 @@ class BrowserColumn(Pager): # pylint: disable=too-many-instance-attributes self.color_reset() def _get_index_of_selected_file(self): - if self.fm.ui.viewmode == 'multipane' and hasattr(self, 'tab'): + if self.fm.ui.viewmode == 'multipane' and self.tab: return self.tab.pointer return self.target.pointer -- cgit 1.4.1-2-gfad0