Skip to content

Commit 6f54861

Browse files
committed
use implicit dict to .keys() conversion
slightly more readable
1 parent 239f495 commit 6f54861

File tree

11 files changed

+28
-36
lines changed

11 files changed

+28
-36
lines changed

crystal_toolkit/components/bandstructure.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def get_bandstructure_traces(bs, path_convention, energy_window=(-6.0, 10.0)):
443443
}
444444

445445
for entry_num in range(len(bs_data["ticks"]["label"])):
446-
for key in str_replace.keys():
446+
for key in str_replace:
447447
if key in bs_data["ticks"]["label"][entry_num]:
448448
bs_data["ticks"]["label"][entry_num] = bs_data["ticks"]["label"][
449449
entry_num
@@ -519,7 +519,7 @@ def get_dos_traces(dos, dos_select, energy_window=(-6.0, 10.0)):
519519
dos_min = np.abs(dos.energies - dos.efermi - energy_window[0]).argmin()
520520

521521
# TODO: pymatgen should have a property here
522-
spin_polarized = len(dos.densities.keys()) == 2
522+
spin_polarized = len(dos.densities) == 2
523523

524524
if spin_polarized:
525525
# Add second spin data if available
@@ -580,7 +580,7 @@ def get_dos_traces(dos, dos_select, energy_window=(-6.0, 10.0)):
580580
"#e377c2", # raspberry yogurt pink
581581
]
582582

583-
for label in proj_data.keys():
583+
for label in proj_data:
584584

585585
if spin_polarized:
586586
trace = {
@@ -691,7 +691,7 @@ def get_figure(
691691
]
692692

693693
# check the max of the second dos trace only if spin polarized
694-
spin_polarized = len(dos.densities.keys()) == 2
694+
spin_polarized = len(dos.densities) == 2
695695
if spin_polarized:
696696
list_max.extend(
697697
[

crystal_toolkit/components/localenv.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def _get_local_order_parameters(structure_graph, n):
7575
# code from @nisse3000, moved here from graphs to avoid circular
7676
# import, also makes sense to have this as a general NN method
7777
cn = structure_graph.get_coordination_of_site(n)
78-
if cn in [int(k_cn) for k_cn in cn_opt_params.keys()]:
79-
names = [k for k in cn_opt_params[cn].keys()]
78+
if cn in [int(k_cn) for k_cn in cn_opt_params]:
79+
names = [k for k in cn_opt_params[cn]]
8080
types = []
8181
params = []
8282
for name in names:
@@ -645,7 +645,7 @@ def update_soap_similarities(struct, all_kwargs):
645645
if mpid != "input"
646646
}
647647

648-
sorted_mpids = sorted(similarities.keys(), key=lambda x: -similarities[x])
648+
sorted_mpids = sorted(similarities, key=lambda x: -similarities[x])
649649

650650
print("Generating similarity graphs")
651651
# TODO: was much slower using px.imshow (see prev commit)

crystal_toolkit/components/search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def update_dropdown_options(results):
197197
def update_dropdown_value(results):
198198
if not results or "error" in results:
199199
raise PreventUpdate
200-
return list(results.keys())[0]
200+
return list(results)[0]
201201

202202
@app.callback(
203203
Output(self.id("dropdown-container"), "style"),

crystal_toolkit/components/structure.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def _make_title(self, legend):
651651
for part in formula_parts
652652
]
653653
except Exception:
654-
formula_components = list(map(str, composition.keys()))
654+
formula_components = list(map(str, composition))
655655

656656
return H2(
657657
formula_components, id=self.id("title"), style={"display": "inline-block"}
@@ -666,9 +666,7 @@ def _make_bonding_algorithm_custom_cuffoff_data(graph):
666666
species = set(
667667
map(
668668
str,
669-
chain.from_iterable(
670-
[list(c.keys()) for c in struct_or_mol.species_and_occu]
671-
),
669+
chain.from_iterable([list(c) for c in struct_or_mol.species_and_occu]),
672670
)
673671
)
674672
rows = [
@@ -879,7 +877,7 @@ def _sub_layouts(self):
879877
data=self.initial_data["scene"],
880878
settings=self.initial_scene_settings,
881879
sceneSize="100%",
882-
fileOptions=list(self.download_options["Structure"].keys()),
880+
fileOptions=list(self.download_options["Structure"]),
883881
showControls=self.show_controls,
884882
showExpandButton=self.show_expand_button,
885883
showImageButton=self.show_image_button,
@@ -964,7 +962,7 @@ def _preprocess_input_to_graph(
964962
else:
965963
if (
966964
bonding_strategy
967-
not in StructureMoleculeComponent.available_bonding_strategies.keys()
965+
not in StructureMoleculeComponent.available_bonding_strategies
968966
):
969967
valid_subclasses = ", ".join(
970968
StructureMoleculeComponent.available_bonding_strategies

crystal_toolkit/components/transformations/grainboundary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def _get_sigmas_options_and_ratio(structure, rotation_axis):
220220
8: "₈",
221221
9: "₉",
222222
}
223-
for sigma in sorted(sigmas.keys()):
223+
for sigma in sorted(sigmas):
224224
sigma_label = f"Σ{sigma}"
225225
for k, v in subscript_unicode_map.items():
226226
sigma_label = sigma_label.replace(str(k), v)

crystal_toolkit/core/legend.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,7 @@ def generate_accessible_color_scheme_on_the_fly(
153153
color_scheme = {}
154154

155155
all_species = set(
156-
chain.from_iterable(
157-
comp.keys() for comp in site_collection.species_and_occu
158-
)
156+
chain.from_iterable(comp for comp in site_collection.species_and_occu)
159157
)
160158
all_elements = sorted(sp.as_dict()["element"] for sp in all_species)
161159

@@ -194,7 +192,7 @@ def generate_accessible_color_scheme_on_the_fly(
194192
)
195193

196194
preferred_elements_present = [
197-
el for el in all_elements if el in preferred_colors.keys()
195+
el for el in all_elements if el in preferred_colors
198196
]
199197

200198
colors_assigned = []
@@ -203,9 +201,7 @@ def generate_accessible_color_scheme_on_the_fly(
203201
color_scheme[el] = palette[preferred_colors[el]]
204202
colors_assigned.append(preferred_colors[el])
205203

206-
remaining_elements = [
207-
el for el in all_elements if el not in color_scheme.keys()
208-
]
204+
remaining_elements = [el for el in all_elements if el not in color_scheme]
209205
remaining_palette = [
210206
c for idx, c in enumerate(palette) if idx not in colors_assigned
211207
]

crystal_toolkit/core/mpcomponent.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def all_stores(self) -> list[str]:
348348
"""
349349
:return: List of all store ids generated by this component
350350
"""
351-
return list(self._stores.keys())
351+
return list(self._stores)
352352

353353
@property
354354
def all_ids(self) -> list[str]:
@@ -369,9 +369,7 @@ def __str__(self):
369369
[f"* {component_id} " for component_id in sorted(self.all_ids)]
370370
)
371371
stores = "\n".join([f"* {store} " for store in sorted(self.all_stores)])
372-
layouts = "\n".join(
373-
[f"* {layout} " for layout in sorted(self._sub_layouts.keys())]
374-
)
372+
layouts = "\n".join([f"* {layout} " for layout in sorted(self._sub_layouts)])
375373

376374
return f"""{self.id()}<{type(self).__name__}> \n
377375
IDs: \n{ids} \n
@@ -498,9 +496,9 @@ def matrix_element(idx, value=0):
498496
# arrange the input boxes in two dimensions (rows, columns)
499497
matrix_div_contents = []
500498
print("matrix_contents", matrix_contents)
501-
for column_idx in sorted(matrix_contents.keys()):
499+
for column_idx in sorted(matrix_contents):
502500
row = []
503-
for row_idx in sorted(matrix_contents[column_idx].keys()):
501+
for row_idx in sorted(matrix_contents[column_idx]):
504502
row.append(matrix_contents[column_idx][row_idx])
505503
matrix_div_contents.append(html.Div(row))
506504

crystal_toolkit/core/tests/test_legend.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ def setup_method(self, method):
1818
)
1919

2020
self.site0 = self.struct[0]
21-
self.sp0 = list(self.site0.species.keys())[0]
21+
self.sp0 = list(self.site0.species)[0]
2222

2323
self.site1 = self.struct[1]
24-
self.sp1 = list(self.site1.species.keys())[0]
24+
self.sp1 = list(self.site1.species)[0]
2525

2626
self.site2 = self.struct[2]
27-
self.sp2 = list(self.site2.species.keys())[0]
27+
self.sp2 = list(self.site2.species)[0]
2828

2929
self.struct_disordered = Structure(
3030
Lattice.cubic(5),
@@ -34,8 +34,8 @@ def setup_method(self, method):
3434
)
3535

3636
self.site_d = self.struct_disordered[2]
37-
self.site_d_sp0 = list(self.site_d.species.keys())[0]
38-
self.site_d_sp1 = list(self.site_d.species.keys())[1]
37+
self.site_d_sp0 = list(self.site_d.species)[0]
38+
self.site_d_sp1 = list(self.site_d.species)[1]
3939

4040
self.struct_manual = Structure(
4141
Lattice.cubic(5),

crystal_toolkit/helpers/asymptote_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def filter_data(scene_data, fstream):
350350
"""
351351
Recursively traverse the scene_data dictionary to find objects to draw
352352
"""
353-
if "type" in scene_data.keys():
353+
if "type" in scene_data:
354354
asy_write_data(scene_data, fstream)
355355
else:
356356
for itr in scene_data["contents"]:

crystal_toolkit/helpers/povray_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def filter_data(scene_data, fstream):
196196
"""
197197
Recursively traverse the scene_data dictionary to find objects to draw
198198
"""
199-
if "type" in scene_data.keys():
199+
if "type" in scene_data:
200200
pov_write_data(scene_data, fstream)
201201
else:
202202
for itr in scene_data["contents"]:

0 commit comments

Comments
 (0)