Skip to content

Commit 2652633

Browse files
committed
dict.keys() is nearly always useless.
1 parent cbbffc8 commit 2652633

35 files changed

+66
-115
lines changed

doc/sphinxext/gen_gallery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def gen_gallery(app, doctree):
154154
fh.write(content)
155155

156156
for key in app.builder.status_iterator(
157-
iter(thumbnails.keys()), "generating thumbnails... ",
157+
iter(thumbnails), "generating thumbnails... ",
158158
length=len(thumbnails)):
159159
if out_of_date(key, thumbnails[key]):
160160
image.thumbnail(key, thumbnails[key], 0.3)

doc/sphinxext/gen_rst.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def generate_example_rst(app):
5151
relpath = os.path.split(root)[-1]
5252
datad.setdefault(relpath, []).append((fullpath, fname, contents))
5353

54-
subdirs = list(datad.keys())
55-
subdirs.sort()
54+
subdirs = sorted(datad)
5655

5756
fhindex = open(os.path.join(exampledir, 'index.rst'), 'w')
5857
fhindex.write("""\

examples/pylab_examples/fancybox_demo2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
fig1 = plt.figure(1, (4/1.5, figheight/1.5))
99
fontsize = 0.3 * 72
1010

11-
for i, stylename in enumerate(sorted(styles.keys())):
12-
fig1.text(0.5, (spacing * (float(len(styles)) - i) - 0.5)/figheight, stylename,
11+
for i, stylename in enumerate(sorted(styles)):
12+
fig1.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight, stylename,
1313
ha="center",
1414
size=fontsize,
1515
transform=fig1.transFigure,
1616
bbox=dict(boxstyle=stylename, fc="w", ec="k"))
17-
plt.draw()
17+
1818
plt.show()

examples/statistics/bxp_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
stats[n]['med'] = np.median(data)
3333
stats[n]['mean'] *= 2
3434

35-
print(stats[0].keys())
35+
print(list(stats[0]))
3636

3737
fs = 10 # fontsize
3838

examples/units/basic_units.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def __get__(self, obj, objtype=None):
1717
return self.proxy_type(self.fn_name, obj)
1818

1919

20-
class TaggedValueMeta (type):
20+
class TaggedValueMeta(type):
2121
def __init__(cls, name, bases, dict):
22-
for fn_name in cls._proxies.keys():
22+
for fn_name in cls._proxies:
2323
try:
2424
dummy = getattr(cls, fn_name)
2525
except AttributeError:

examples/user_interfaces/toolmanager.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,16 @@ class ListTools(ToolBase):
2424

2525
def trigger(self, *args, **kwargs):
2626
print('_' * 80)
27-
print("{0:12} {1:45} {2}".format('Name (id)',
28-
'Tool description',
29-
'Keymap'))
27+
print("{0:12} {1:45} {2}".format(
28+
'Name (id)', 'Tool description', 'Keymap'))
3029
print('-' * 80)
3130
tools = self.toolmanager.tools
32-
for name in sorted(tools.keys()):
31+
for name in sorted(tools):
3332
if not tools[name].description:
3433
continue
3534
keys = ', '.join(sorted(self.toolmanager.get_tool_keymap(name)))
36-
print("{0:12} {1:45} {2}".format(name,
37-
tools[name].description,
38-
keys))
35+
print("{0:12} {1:45} {2}".format(
36+
name, tools[name].description, keys))
3937
print('_' * 80)
4038
print("Active Toggle tools")
4139
print("{0:12} {1:45}".format("Group", "Active"))

lib/matplotlib/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,9 @@ def __setitem__(self, key, val):
916916
raise ValueError("Key %s: %s" % (key, str(ve)))
917917
dict.__setitem__(self, key, cval)
918918
except KeyError:
919-
raise KeyError('%s is not a valid rc parameter.\
920-
See rcParams.keys() for a list of valid parameters.' % (key,))
919+
raise KeyError(
920+
'%s is not a valid rc parameter. See rcParams.keys() for a '
921+
'list of valid parameters.' % (key,))
921922

922923
def __getitem__(self, key):
923924
inverse_alt = None
@@ -974,7 +975,7 @@ def values(self):
974975
"""
975976
Return values in order of sorted keys.
976977
"""
977-
return [self[k] for k in self.keys()]
978+
return [self[k] for k in self]
978979

979980
def find_all(self, pattern):
980981
"""

lib/matplotlib/afm.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,9 @@ def _parse_char_metrics(fh):
199199
if line.startswith('EndCharMetrics'):
200200
return ascii_d, name_d
201201
# Split the metric line into a dictonary, keyed by metric identifiers
202-
vals = filter(lambda s: len(s) > 0, line.split(';'))
203-
vals = dict(map(lambda s: tuple(s.strip().split(' ', 1)), vals))
202+
vals = dict(s.strip().split(' ', 1) for s in line.split(';') if s)
204203
# There may be other metrics present, but only these are needed
205-
if any([id not in vals.keys() for id in ('C', 'WX', 'N', 'B')]):
204+
if not {'C', 'WX', 'N', 'B'}.issubset(vals):
206205
raise RuntimeError('Bad char metrics line: %s' % line)
207206
num = _to_int(vals['C'])
208207
wx = _to_float(vals['WX'])

lib/matplotlib/animation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def reset_available_writers(self):
107107
def list(self):
108108
''' Get a list of available MovieWriters.'''
109109
self.ensure_not_dirty()
110-
return list(self.avail.keys())
110+
return list(self.avail)
111111

112112
def is_available(self, name):
113113
self.ensure_not_dirty()

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,7 +2953,7 @@ def set_xscale(self, value, **kwargs):
29532953
"""
29542954
# If the scale is being set to log, clip nonposx to prevent headaches
29552955
# around zero
2956-
if value.lower() == 'log' and 'nonposx' not in kwargs.keys():
2956+
if value.lower() == 'log' and 'nonposx' not in kwargs:
29572957
kwargs['nonposx'] = 'clip'
29582958

29592959
g = self.get_shared_x_axes()
@@ -3233,7 +3233,7 @@ def set_yscale(self, value, **kwargs):
32333233
"""
32343234
# If the scale is being set to log, clip nonposy to prevent headaches
32353235
# around zero
3236-
if value.lower() == 'log' and 'nonposy' not in kwargs.keys():
3236+
if value.lower() == 'log' and 'nonposy' not in kwargs:
32373237
kwargs['nonposy'] = 'clip'
32383238

32393239
g = self.get_shared_y_axes()

0 commit comments

Comments
 (0)