Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 68b42d9

Browse files
committed
Clarify the implementation of prepare_data.
1 parent 44ce103 commit 68b42d9

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,27 @@ def figure_edit(axes, parent=None):
8181

8282
def prepare_data(d, init):
8383
"""Prepare entry for FormLayout.
84+
85+
`d` is a mapping of shorthands to style names (a single style may
86+
have multiple shorthands, in particular the shorthands `None`,
87+
`"None"`, `"none"` and `""` are synonyms); `init` is one shorthand
88+
of the initial style.
89+
90+
This function returns an list suitable for initializing a
91+
FormLayout combobox, namely `[initial_name, (shorthand,
92+
style_name), (shorthand, style_name), ...]`.
8493
"""
85-
# List items in dict, dropping duplicate values, sorting by values.
86-
kvs = [(k, v) for v, k in
87-
sorted({v: k for k, v in d.items()}.items())]
88-
# Find the unique kept key with the same value as the init value.
89-
canonical_init, = ({k for k, v in d.items() if v == d[init]}.
90-
intersection(k for k, v in kvs))
91-
return [canonical_init] + kvs
94+
# Drop duplicate shorthands from dict (by overwriting them during
95+
# the dict comprehension).
96+
name2short = {name: short for short, name in d.items()}
97+
# Convert back to {shorthand: name}.
98+
short2name = {short: name for name, short in name2short.items()}
99+
# Find the kept shorthand for the style specified by init.
100+
canonical_init = name2short[d[init]]
101+
# Sort by representation and prepend the initial value.
102+
return ([canonical_init] +
103+
sorted(short2name.items(),
104+
key=lambda short_and_name: short_and_name[1]))
92105

93106
curvelabels = sorted(linedict.keys())
94107
for label in curvelabels:

0 commit comments

Comments
 (0)