[PATCH patatt 6/7] Reduce dictionary lookups

Tamir Duberstein <[email protected]>
Newsgroups org.kernel.linux.tools
Message-ID <[email protected]>
Use `dict.get` instead of separately `in` checks and lookups.

Signed-off-by: Tamir Duberstein <[email protected]>
---
 src/patatt/__init__.py | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/src/patatt/__init__.py b/src/patatt/__init__.py
index 7087479..b20074a 100644
--- a/src/patatt/__init__.py
+++ b/src/patatt/__init__.py
@@ -1024,8 +1024,8 @@ def get_data_dir() -> Path:
     Returns:
         Path to $XDG_DATA_HOME/patatt or ~/.local/share/patatt.
     """
-    if 'XDG_DATA_HOME' in os.environ:
-        datahome = Path(os.environ['XDG_DATA_HOME'])
+    if (xdg_data_home := os.environ.get('XDG_DATA_HOME')) is not None:
+        datahome = Path(xdg_data_home)
     else:
         datahome = Path.home() / '.local' / 'share'
     datadir = datahome / 'patatt'
@@ -1100,16 +1100,13 @@ def get_config_from_git(
                 continue
 
             if cfgkey in multivals:
-                if cfgkey not in gitconfig:
-                    values: List[str] = list()
+                cfgvalue = gitconfig.setdefault(cfgkey, list())
+                if isinstance(cfgvalue, str):
+                    values = [cfgvalue]
+                    gitconfig[cfgkey] = values
                 else:
-                    cfgvalue = gitconfig[cfgkey]
-                    if isinstance(cfgvalue, str):
-                        values = [cfgvalue]
-                    else:
-                        values = cfgvalue
+                    values = cfgvalue
                 values.append(value)
-                gitconfig[cfgkey] = values
             else:
                 gitconfig[cfgkey] = value
         except ValueError:
@@ -1423,8 +1420,8 @@ def get_algo_keydata(config: GitConfigType) -> Tuple[str, str]:
             'Identity must be a string, got %s' % type(identity).__name__
         )
 
-    if identity in KEYCACHE:
-        algo, keydata = KEYCACHE[identity]
+    if (t := KEYCACHE.get(identity)) is not None:
+        algo, keydata = t
         return algo, keydata
 
     if not config.get('signingkey'):

-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.