From 85c2c973d8817589b02ccc16ee8ff84b7b53a21c Mon Sep 17 00:00:00 2001 From: Roy-043 Date: Wed, 9 Aug 2023 23:31:31 +0200 Subject: [PATCH] Material: Avoid dictionary.keys() where possible --- src/Mod/Material/MaterialEditor.py | 6 +++--- src/Mod/Material/importFCMat.py | 6 +++--- src/Mod/Material/materialtools/cardutils.py | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Mod/Material/MaterialEditor.py b/src/Mod/Material/MaterialEditor.py index 655e8ff619..044bd2f2f5 100644 --- a/src/Mod/Material/MaterialEditor.py +++ b/src/Mod/Material/MaterialEditor.py @@ -159,7 +159,7 @@ class MaterialEditor: template_data = get_material_template(True) for group in template_data: - gg = list(group.keys())[0] # group dict has only one key + gg = list(group)[0] # group dict has only one key top = QtGui.QStandardItem(gg) model.appendRow([top]) self.groups.append(gg) @@ -253,13 +253,13 @@ class MaterialEditor: card_name_list = [] # [ [card_name, card_path, icon_path], ... ] if sort_by_resources is True: - for a_path in sorted(self.materials.keys()): + for a_path in sorted(self.materials): card_name_list.append([self.cards[a_path], a_path, self.icons[a_path]]) else: card_names_tmp = {} for path, name in self.cards.items(): card_names_tmp[name] = path - for a_name in sorted(card_names_tmp.keys()): + for a_name in sorted(card_names_tmp): a_path = card_names_tmp[a_name] card_name_list.append([a_name, a_path, self.icons[a_path]]) diff --git a/src/Mod/Material/importFCMat.py b/src/Mod/Material/importFCMat.py index 489fb2204c..f53ce996ef 100644 --- a/src/Mod/Material/importFCMat.py +++ b/src/Mod/Material/importFCMat.py @@ -190,7 +190,7 @@ def write(filename, dictionary, write_group_section=True): user = {} template_data = get_material_template() for group in template_data: - groupName = list(group.keys())[0] # group dict has only one key + groupName = list(group)[0] # group dict has only one key contents.append({"keyname": groupName}) if groupName == "Meta": header = contents[-1] @@ -202,7 +202,7 @@ def write(filename, dictionary, write_group_section=True): found = False for group in contents: if not found: - if k in group.keys(): + if k in group: group[k] = i found = True if not found: @@ -211,7 +211,7 @@ def write(filename, dictionary, write_group_section=True): for group in contents: # iterating over a dict and changing it is not allowed # thus it is iterated over a list of the keys - for k in list(group.keys()): + for k in list(group): if group[k] == '': del group[k] diff --git a/src/Mod/Material/materialtools/cardutils.py b/src/Mod/Material/materialtools/cardutils.py index bfc085bd5e..bde7157a20 100644 --- a/src/Mod/Material/materialtools/cardutils.py +++ b/src/Mod/Material/materialtools/cardutils.py @@ -231,11 +231,11 @@ def get_material_template(withSpaces=False): new_template = [] for group in template_data: new_group = {} - gg = list(group.keys())[0] # group dict has only one key + gg = list(group)[0] # group dict has only one key # iterating over a dict and changing it is not allowed # thus it is iterated over a list of the keys new_group[gg] = {} - for proper in list(group[gg].keys()): + for proper in list(group[gg]): new_proper = re.sub(r"(\w)([A-Z]+)", r"\1 \2", proper) # strip underscores of vectorial properties new_proper = new_proper.replace("_", " ") @@ -256,7 +256,7 @@ def create_mat_tools_header(): template_data = get_material_template() f = open(headers, "w") for group in template_data: - gg = list(group.keys())[0] # group dict has only one key + gg = list(group)[0] # group dict has only one key # do not write group UserDefined if gg != 'UserDefined': for prop_name in group[gg]: @@ -292,7 +292,7 @@ def create_mat_template_card(write_group_section=True): if write_group_section is False: f.write("\n[FCMat]\n") for group in template_data: - gg = list(group.keys())[0] # group dict has only one key + gg = list(group)[0] # group dict has only one key # do not write groups Meta and UserDefined if (gg != 'Meta') and (gg != 'UserDefined'): # only write group section if write group section parameter is set to True @@ -335,7 +335,7 @@ def get_known_material_quantity_parameter(): template_data = get_material_template() known_quantities = [] for group in template_data: - gname = list(group.keys())[0] # group dict has only one key + gname = list(group)[0] # group dict has only one key for prop_name in group[gname]: prop_type = group[gname][prop_name]['Type'] if prop_type == 'Quantity': @@ -352,7 +352,7 @@ def get_and_output_all_carddata(cards): template_data = get_material_template() # print(template_data) for group in template_data: - gg = list(group.keys())[0] # group dict has only one key + gg = list(group)[0] # group dict has only one key for key in group[gg]: registed_cardkeys.append(key) registed_cardkeys = sorted(registed_cardkeys) @@ -662,7 +662,7 @@ gettemplate()[2]['Mechanical']['FractureToughness'] from materialtools.cardutils import get_material_template as gettemplate template_data=gettemplate() for group in template_data: - gname = list(group.keys())[0] # group dict has only one key + gname = list(group)[0] # group dict has only one key for prop_name in group[gname]: #prop_dict = group[gname][prop_name] #print(prop_dict)