Skip to content

Commit 23e5b39

Browse files
committed
Use dict.items() instead of repeatedly getting the value
Faster and shorter
1 parent 62f4af3 commit 23e5b39

File tree

13 files changed

+46
-46
lines changed

13 files changed

+46
-46
lines changed

easybuild/framework/easyblock.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,9 +2197,9 @@ def handle_iterate_opts(self):
21972197
self.log.info("Current iteration index: %s", self.iter_idx)
21982198

21992199
# pop first element from all iterative easyconfig parameters as next value to use
2200-
for opt in self.iter_opts:
2201-
if len(self.iter_opts[opt]) > self.iter_idx:
2202-
self.cfg[opt] = self.iter_opts[opt][self.iter_idx]
2200+
for opt, value in self.iter_opts.items():
2201+
if len(value) > self.iter_idx:
2202+
self.cfg[opt] = value[self.iter_idx]
22032203
else:
22042204
self.cfg[opt] = '' # empty list => empty option as next value
22052205
self.log.debug("Next value for %s: %s" % (opt, str(self.cfg[opt])))
@@ -2211,12 +2211,12 @@ def post_iter_step(self):
22112211
"""Restore options that were iterated over"""
22122212
# disable templating, since we're messing about with values in self.cfg
22132213
with self.cfg.disable_templating():
2214-
for opt in self.iter_opts:
2215-
self.cfg[opt] = self.iter_opts[opt]
2214+
for opt, value in self.iter_opts.items():
2215+
self.cfg[opt] = value
22162216

22172217
# also need to take into account extensions, since those were iterated over as well
22182218
for ext in self.ext_instances:
2219-
ext.cfg[opt] = self.iter_opts[opt]
2219+
ext.cfg[opt] = value
22202220

22212221
self.log.debug("Restored value of '%s' that was iterated over: %s", opt, self.cfg[opt])
22222222

@@ -4661,14 +4661,14 @@ def inject_checksums_to_json(ecs, checksum_type):
46614661

46624662
# actually inject new checksums or overwrite existing ones (if --force)
46634663
existing_checksums = app.get_checksums_from_json(always_read=True)
4664-
for filename in checksums:
4664+
for filename, checksum in checksums.items():
46654665
if filename not in existing_checksums:
4666-
existing_checksums[filename] = checksums[filename]
4666+
existing_checksums[filename] = checksum
46674667
# don't do anything if the checksum already exist and is the same
4668-
elif checksums[filename] != existing_checksums[filename]:
4668+
elif checksum != existing_checksums[filename]:
46694669
if build_option('force'):
46704670
print_warning("Found existing checksums for %s, overwriting them (due to --force)..." % ec_fn)
4671-
existing_checksums[filename] = checksums[filename]
4671+
existing_checksums[filename] = checksum
46724672
else:
46734673
raise EasyBuildError("Found existing checksum for %s, use --force to overwrite them" % filename)
46744674

easybuild/framework/easyconfig/easyconfig.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ def check_deprecated(self, path):
827827
if depr_msgs:
828828
depr_msg = ', '.join(depr_msgs)
829829

830-
depr_maj_ver = int(str(VERSION).split('.')[0]) + 1
830+
depr_maj_ver = int(str(VERSION).split('.', maxsplit=1)[0]) + 1
831831
depr_ver = '%s.0' % depr_maj_ver
832832

833833
more_info_depr_ec = " (see also https://docs.easybuild.io/deprecated-easyconfigs)"
@@ -842,8 +842,8 @@ def validate(self, check_osdeps=True):
842842
- check license
843843
"""
844844
self.log.info("Validating easyconfig")
845-
for attr in self.validations:
846-
self._validate(attr, self.validations[attr])
845+
for attr, valid_values in self.validations.items():
846+
self._validate(attr, valid_values)
847847

848848
if check_osdeps:
849849
self.log.info("Checking OS dependencies")
@@ -1207,8 +1207,8 @@ def dump(self, fp, always_overwrite=True, backup=False, explicit_toolchains=Fals
12071207
# templated values should be dumped unresolved
12081208
with self.disable_templating():
12091209
# build dict of default values
1210-
default_values = {key: DEFAULT_CONFIG[key][0] for key in DEFAULT_CONFIG}
1211-
default_values.update({key: self.extra_options[key][0] for key in self.extra_options})
1210+
default_values = {key: value[0] for key, value in DEFAULT_CONFIG.items()}
1211+
default_values.update({key: value[0] for key, value in self.extra_options.items()})
12121212

12131213
self.generate_template_values()
12141214
templ_const = {quote_py_str(const[1]): const[0] for const in TEMPLATE_CONSTANTS}

easybuild/tools/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,12 +592,12 @@ def init_build_options(build_options=None, cmdline_options=None):
592592
# seed in defaults to make sure all build options are defined, and that build_option() doesn't fail on valid keys
593593
bo = {}
594594
for build_options_by_default in [BUILD_OPTIONS_CMDLINE, BUILD_OPTIONS_OTHER]:
595-
for default in build_options_by_default:
595+
for default, options in build_options_by_default.items():
596596
if default == EMPTY_LIST:
597-
for opt in build_options_by_default[default]:
597+
for opt in options:
598598
bo[opt] = []
599599
else:
600-
bo.update({opt: default for opt in build_options_by_default[default]})
600+
bo.update({opt: default for opt in options})
601601
bo.update(active_build_options)
602602

603603
# BuildOptions is a singleton, so any future calls to BuildOptions will yield the same instance

easybuild/tools/docs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,8 +807,8 @@ def list_software(output_format=FORMAT_TXT, detailed=False, only_installed=False
807807

808808
# rebuild software, only retain entries with a corresponding available module
809809
software, all_software = {}, software
810-
for key in all_software:
811-
for entry in all_software[key]:
810+
for key, entries in all_software.items():
811+
for entry in entries:
812812
if entry['mod_name'] in avail_mod_names:
813813
software.setdefault(key, []).append(entry)
814814

easybuild/tools/environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def write_changes(filename):
5454
"""
5555
try:
5656
with open(filename, 'w') as script:
57-
for key in _changes:
58-
script.write('export %s=%s\n' % (key, shell_quote(_changes[key])))
57+
for key, changed_value in _changes.items():
58+
script.write('export %s=%s\n' % (key, shell_quote(changed_value)))
5959
except IOError as err:
6060
raise EasyBuildError("Failed to write to %s: %s", filename, err)
6161
reset_changes()

easybuild/tools/github.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,8 +1029,8 @@ def _easyconfigs_pr_common(paths, ecs, start_branch=None, pr_branch=None, start_
10291029
# only consider new easyconfig files for dependencies (not updated ones)
10301030
for idx in range(len(all_dep_info['ecs'])):
10311031
if all_dep_info['new'][idx]:
1032-
for key in dep_info:
1033-
dep_info[key].append(all_dep_info[key][idx])
1032+
for key, values in dep_info.items():
1033+
values.append(all_dep_info[key][idx])
10341034

10351035
# checkout target branch
10361036
if pr_branch is None:

easybuild/tools/module_naming_scheme/hierarchical_mns.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ def det_modpath_extensions(self, ec):
204204

205205
comp_name_ver = None
206206
if ec['name'] in extend_comps:
207-
for key in COMP_NAME_VERSION_TEMPLATES:
207+
for key, comp_tmpl in COMP_NAME_VERSION_TEMPLATES.items():
208208
comp_names = key.split(',')
209209
if ec['name'] in comp_names:
210-
comp_name, comp_ver_tmpl = COMP_NAME_VERSION_TEMPLATES[key]
210+
comp_name, comp_ver_tmpl = comp_tmpl
211211
comp_versions = {ec['name']: self.det_full_version(ec)}
212212
if ec['name'] == 'ifort':
213213
# 'icc' key should be provided since it's the only one used in the template

easybuild/tools/multidiff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ def multidiff(base, files, colored=True):
291291
offset -= 1
292292

293293
# construct the multi-diff based on the constructed dict
294-
for line_no in local_diff:
295-
for (line, filename) in local_diff[line_no]:
294+
for line_no, line_infos in local_diff.items():
295+
for (line, filename) in line_infos:
296296
mdiff.parse_line(line_no, line.rstrip(), filename, squigly_dict.get(line, '').rstrip())
297297

298298
return str(mdiff)

easybuild/tools/options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,9 +1365,9 @@ def show_system_info(self):
13651365
'',
13661366
"* GPU:",
13671367
])
1368-
for vendor in gpu_info:
1368+
for vendor, vendor_gpu in gpu_info.items():
13691369
lines.append(" -> %s" % vendor)
1370-
for gpu, num in gpu_info[vendor].items():
1370+
for gpu, num in vendor_gpu.items():
13711371
lines.append(" -> %sx %s" % (num, gpu))
13721372

13731373
lines.extend([

easybuild/tools/systemtools.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,9 +1367,9 @@ def extract_version(tool):
13671367
python_version = extract_version(sys.executable)
13681368

13691369
opt_dep_versions = {}
1370-
for key in EASYBUILD_OPTIONAL_DEPENDENCIES:
1370+
for key, opt_dep in EASYBUILD_OPTIONAL_DEPENDENCIES.items():
13711371

1372-
pkg = EASYBUILD_OPTIONAL_DEPENDENCIES[key][0]
1372+
pkg = opt_dep[0]
13731373
if pkg is None:
13741374
pkg = key.lower()
13751375

@@ -1395,8 +1395,8 @@ def extract_version(tool):
13951395
opt_deps_key = "Optional dependencies"
13961396
checks_data[opt_deps_key] = {}
13971397

1398-
for key in opt_dep_versions:
1399-
checks_data[opt_deps_key][key] = (opt_dep_versions[key], EASYBUILD_OPTIONAL_DEPENDENCIES[key][1])
1398+
for key, version in opt_dep_versions.items():
1399+
checks_data[opt_deps_key][key] = (version, EASYBUILD_OPTIONAL_DEPENDENCIES[key][1])
14001400

14011401
sys_tools_key = "System tools"
14021402
checks_data[sys_tools_key] = {}

0 commit comments

Comments
 (0)