From 77b083e2191c27b848b484a27e65dfbba301e9dd Mon Sep 17 00:00:00 2001 From: Bananaman <38923130+Bananaman@users.noreply.github.com> Date: Fri, 31 Mar 2023 15:24:02 +0200 Subject: [PATCH] A few minor code cleanups and fixes (#47) * Avoid exception if trying to delete missing "removed" key `.pop(key)` throws an exception if that `key` is missing. Using a default value (in this case None) means we don't throw any errors when deleting a missing key. Which is what we want here, for safety. We just want to delete the key. We don't care if it's existing or not. There are other places in the codebase that also use `.pop(key)`, but all of those first check the validity of the key before popping, so this was the only one that needed fixing. * Improve performance by removing keys() calls Every `.keys()` call create a new `[list...]` of all keys from the given dictionary. It's a total waste of performance, since we can already check if a key exists in a dictionary by just using `if "key" in the_dict`. * Use more pythonic "not in" syntax The syntax is supposed to be `if "thing" not in other_thing`. We already use this proper `not in` syntax everywhere else in the codebase. Just fixing this location. --- src/game.py | 8 ++++---- src/preferences.py | 4 ++-- src/utils/importer.py | 2 +- src/window.py | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/game.py b/src/game.py index 484c492..24a9ef1 100644 --- a/src/game.py +++ b/src/game.py @@ -53,9 +53,9 @@ class game(Gtk.Box): # pylint: disable=invalid-name self.hidden = data["hidden"] self.last_played = data["last_played"] self.name = data["name"] - self.developer = data["developer"] if "developer" in data.keys() else None - self.removed = "removed" in data.keys() - self.blacklisted = "blacklisted" in data.keys() + self.developer = data["developer"] if "developer" in data else None + self.removed = "removed" in data + self.blacklisted = "blacklisted" in data self.pixbuf = self.get_cover() @@ -125,7 +125,7 @@ class game(Gtk.Box): # pylint: disable=invalid-name def get_cover(self): # If the cover is already in memory, return - if self.game_id in self.parent_widget.pixbufs.keys(): + if self.game_id in self.parent_widget.pixbufs: return self.parent_widget.pixbufs[self.game_id] # Create a new pixbuf diff --git a/src/preferences.py b/src/preferences.py index 8af5263..cb3c91c 100644 --- a/src/preferences.py +++ b/src/preferences.py @@ -285,7 +285,7 @@ class PreferencesWindow(Adw.PreferencesWindow): def undo_remove_all(self, _widget, _unused): for game_id in self.removed_games: data = get_games([game_id])[game_id] - if "removed" in data.keys(): + if "removed" in data: data.pop("removed") save_game(data) self.parent_widget.update_games(self.removed_games) @@ -294,7 +294,7 @@ class PreferencesWindow(Adw.PreferencesWindow): def remove_all_games(self, _widget): for game in get_games().values(): - if not "removed" in game.keys(): + if "removed" not in game: self.removed_games.append(game["game_id"]) game["removed"] = True save_game(game) diff --git a/src/utils/importer.py b/src/utils/importer.py index 4ca0a86..e3850b5 100644 --- a/src/utils/importer.py +++ b/src/utils/importer.py @@ -57,7 +57,7 @@ class Importer: self.games_no += 1 save_game(values) self.parent_widget.update_games([values["game_id"]]) - if "blacklisted" in values.keys(): + if "blacklisted" in values: self.games_no -= 1 self.queue -= 1 diff --git a/src/window.py b/src/window.py index 6e7cb3c..fb7021d 100644 --- a/src/window.py +++ b/src/window.py @@ -87,7 +87,7 @@ class CartridgesWindow(Adw.ApplicationWindow): ) current_games = get_games() for current_game in current_games: - if "removed" in current_games[current_game].keys(): + if "removed" in current_games[current_game]: os.remove( os.path.join( os.getenv("XDG_DATA_HOME") @@ -421,7 +421,7 @@ class CartridgesWindow(Adw.ApplicationWindow): except IndexError: return data = get_games([game_id])[game_id] - data.pop("removed") + data.pop("removed", None) save_game(data) self.update_games([game_id]) self.toasts[game_id].dismiss()