From 93c18fafd5df1069242d976e4982c38d74629e17 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 20 Jan 2024 07:13:30 +0300 Subject: [PATCH] Refactor(.vim/python/virtualenv.py): Optimize code Check Python version once outside the loop. --- .vim/python/virtualenv.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/.vim/python/virtualenv.py b/.vim/python/virtualenv.py index ca66bf8..ed46739 100644 --- a/.vim/python/virtualenv.py +++ b/.vim/python/virtualenv.py @@ -1,22 +1,25 @@ -import sys, os +import sys, os # noqa: E401 multiple imports on one line virtualenv_dir = os.environ.get('VIRTUAL_ENV') if virtualenv_dir: - sys.path.insert(0, virtualenv_dir) - for activate_this in [ - os.path.join(virtualenv_dir, 'bin', 'activate_this.py'), - os.path.join(virtualenv_dir, 'Scripts', 'activate_this.py') - ]: - if not os.path.exists(activate_this): - continue - if sys.version_info[0] == 2: - if os.path.exists( - os.path.join(virtualenv_dir, 'lib', 'python2.7')): - execfile(activate_this, + if ( + (sys.version_info[0] == 2) + and os.path.exists( + os.path.join(virtualenv_dir, 'lib', 'python2.7')) + ) or ( + not os.path.exists( + os.path.join(virtualenv_dir, 'lib', 'python2.7')) + ): + for activate_this in [ + os.path.join(virtualenv_dir, 'bin', 'activate_this.py'), + os.path.join(virtualenv_dir, 'Scripts', 'activate_this.py') + ]: + if not os.path.exists(activate_this): + continue + if sys.version_info[0] == 2: + execfile(activate_this, # noqa: F821 undefined name 'execfile' dict(__file__=activate_this)) - else: - if not os.path.exists( - os.path.join(virtualenv_dir, 'lib', 'python2.7')): + else: exec(open(activate_this, 'r').read(), dict(__file__=activate_this)) - break + break -- 2.39.5