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

Skip to content

Commit 70909c4

Browse files
committed
Cleanup: use sorted() whereever possible.
1 parent 58a8334 commit 70909c4

31 files changed

Lines changed: 149 additions & 278 deletions

doc/sphinxext/math_symbol_table.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,10 @@ def get_n(n, l):
106106

107107
lines = []
108108
for category, columns, syms in symbols:
109-
syms = syms.split()
110-
syms.sort()
109+
syms = sorted(syms.split())
111110
lines.append("**%s**" % category)
112111
lines.append('')
113-
max_width = 0
114-
for sym in syms:
115-
max_width = max(max_width, len(sym))
116-
max_width = max_width * 2 + 16
112+
max_width = max(map(len, syms)) * 2 + 16
117113
header = " " + (('=' * max_width) + ' ') * columns
118114
format = '%%%ds' % max_width
119115
for chunk in get_n(20, get_n(columns, syms)):

doc/utils/pylab_names.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
"""
55
from pylab import *
66
d = locals()
7-
keys = d.keys()
8-
keys.sort()
97

108
modd = dict()
11-
for k in keys:
9+
for k in sorted(d):
1210
o = d[k]
1311
if not callable(o):
1412
continue
@@ -37,10 +35,8 @@
3735
mod, k, doc = mod.strip(), k.strip(), doc.strip()[:80]
3836
modd.setdefault(mod, []).append((k, doc))
3937

40-
mods = modd.keys()
41-
mods.sort()
42-
for mod in mods:
43-
border = '*'*len(mod)
38+
for mod in sorted(modd):
39+
border = '*' * len(mod)
4440
print(mod)
4541
print(border)
4642

examples/pylab_examples/font_table_ttf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
'fonts', 'ttf', 'DejaVuSans.ttf')
3333

3434
font = FT2Font(fontname)
35-
codes = list(font.get_charmap().items())
36-
codes.sort()
35+
codes = sorted(font.get_charmap().items())
3736

3837
# a 16,16 array of character strings
3938
chars = [['' for c in range(16)] for r in range(16)]

examples/style_sheets/style_sheets_reference.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,8 @@ def plot_figure(style_label=""):
135135
# Setup a list of all available styles, in alphabetical order but
136136
# the `default` and `classic` ones, which will be forced resp. in
137137
# first and second position.
138-
style_list = list(plt.style.available) # *new* list: avoids side effects.
139-
style_list.remove('classic') # `classic` is in the list: first remove it.
140-
style_list.sort()
141-
style_list.insert(0, u'default')
142-
style_list.insert(1, u'classic')
138+
style_list = ['default', 'classic'] + sorted(
139+
style for style in plt.style.available if style != 'classic')
143140

144141
# Plot a demonstration figure for every available style sheet.
145142
for style_label in style_list:

lib/matplotlib/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,9 +968,7 @@ def keys(self):
968968
"""
969969
Return sorted list of keys.
970970
"""
971-
k = list(dict.keys(self))
972-
k.sort()
973-
return k
971+
return sorted(self)
974972

975973
def values(self):
976974
"""

lib/matplotlib/axes/_base.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,36 +2365,35 @@ def draw(self, renderer=None, inframe=False):
23652365
artists.remove(self._left_title)
23662366
artists.remove(self._right_title)
23672367

2368-
if self.figure.canvas.is_saving():
2369-
dsu = [(a.zorder, a) for a in artists]
2370-
else:
2371-
dsu = [(a.zorder, a) for a in artists
2372-
if (not a.get_animated() or a in self.images)]
2373-
2374-
dsu.sort(key=itemgetter(0))
2368+
if not self.figure.canvas.is_saving():
2369+
artists = [a for a in artists
2370+
if not a.get_animated() or a in self.images]
2371+
artists = sorted(artists, key=lambda artist: artist.get_zorder())
23752372

23762373
# rasterize artists with negative zorder
23772374
# if the minimum zorder is negative, start rasterization
23782375
rasterization_zorder = self._rasterization_zorder
23792376
if (rasterization_zorder is not None and
2380-
len(dsu) > 0 and dsu[0][0] < rasterization_zorder):
2377+
artists and artists[0].get_zorder() < rasterization_zorder):
23812378
renderer.start_rasterizing()
2382-
dsu_rasterized = [l for l in dsu if l[0] < rasterization_zorder]
2383-
dsu = [l for l in dsu if l[0] >= rasterization_zorder]
2379+
artists_rasterized = [a for a in artists
2380+
if a.get_zorder() < rasterization_zorder]
2381+
artists = [a for a in artists
2382+
if a.get_zorder() >= rasterization_zorder]
23842383
else:
2385-
dsu_rasterized = []
2384+
artists_rasterized = []
23862385

23872386
# the patch draws the background rectangle -- the frame below
23882387
# will draw the edges
23892388
if self.axison and self._frameon:
23902389
self.patch.draw(renderer)
23912390

2392-
if dsu_rasterized:
2393-
for zorder, a in dsu_rasterized:
2391+
if artists_rasterized:
2392+
for a in artists_rasterized:
23942393
a.draw(renderer)
23952394
renderer.stop_rasterizing()
23962395

2397-
mimage._draw_list_compositing_images(renderer, self, dsu)
2396+
mimage._draw_list_compositing_images(renderer, self, artists)
23982397

23992398
renderer.close_group('axes')
24002399
self._cachedRenderer = renderer

lib/matplotlib/axis.py

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -951,45 +951,37 @@ def _update_ticks(self, renderer):
951951
"""
952952

953953
interval = self.get_view_interval()
954-
tick_tups = [t for t in self.iter_ticks()]
954+
tick_tups = list(self.iter_ticks())
955955
if self._smart_bounds:
956956
# handle inverted limits
957-
view_low, view_high = min(*interval), max(*interval)
958-
data_low, data_high = self.get_data_interval()
959-
if data_low > data_high:
960-
data_low, data_high = data_high, data_low
961-
locs = [ti[1] for ti in tick_tups]
962-
locs.sort()
963-
locs = np.array(locs)
964-
if len(locs):
965-
if data_low <= view_low:
966-
# data extends beyond view, take view as limit
967-
ilow = view_low
957+
view_low, view_high = min(interval), max(interval)
958+
data_low, data_high = sorted(self.get_data_interval())
959+
locs = np.sort([ti[1] for ti in tick_tups])
960+
if data_low <= view_low:
961+
# data extends beyond view, take view as limit
962+
ilow = view_low
963+
else:
964+
# data stops within view, take best tick
965+
good_locs = locs[locs <= data_low]
966+
if len(good_locs) > 0:
967+
# last tick prior or equal to first data point
968+
ilow = good_locs[-1]
968969
else:
969-
# data stops within view, take best tick
970-
cond = locs <= data_low
971-
good_locs = locs[cond]
972-
if len(good_locs) > 0:
973-
# last tick prior or equal to first data point
974-
ilow = good_locs[-1]
975-
else:
976-
# No ticks (why not?), take first tick
977-
ilow = locs[0]
978-
if data_high >= view_high:
979-
# data extends beyond view, take view as limit
980-
ihigh = view_high
970+
# No ticks (why not?), take first tick
971+
ilow = locs[0]
972+
if data_high >= view_high:
973+
# data extends beyond view, take view as limit
974+
ihigh = view_high
975+
else:
976+
# data stops within view, take best tick
977+
good_locs = locs[locs >= data_high]
978+
if len(good_locs) > 0:
979+
# first tick after or equal to last data point
980+
ihigh = good_locs[0]
981981
else:
982-
# data stops within view, take best tick
983-
cond = locs >= data_high
984-
good_locs = locs[cond]
985-
if len(good_locs) > 0:
986-
# first tick after or equal to last data point
987-
ihigh = good_locs[0]
988-
else:
989-
# No ticks (why not?), take last tick
990-
ihigh = locs[-1]
991-
tick_tups = [ti for ti in tick_tups
992-
if (ti[1] >= ilow) and (ti[1] <= ihigh)]
982+
# No ticks (why not?), take last tick
983+
ihigh = locs[-1]
984+
tick_tups = [ti for ti in tick_tups if ilow <= ti[1] <= ihigh]
993985

994986
# so that we don't lose ticks on the end, expand out the interval ever
995987
# so slightly. The "ever so slightly" is defined to be the width of a

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,15 +1469,12 @@ def __init__(self, name, canvas, x, y, guiEvent=None):
14691469
else:
14701470
axes_list = [self.canvas.mouse_grabber]
14711471

1472-
if len(axes_list) == 0: # None found
1472+
if axes_list: # Use highest zorder.
1473+
self.inaxes = max(axes_list, key=lambda x: x.zorder)
1474+
else: # None found.
14731475
self.inaxes = None
14741476
self._update_enter_leave()
14751477
return
1476-
elif (len(axes_list) > 1): # Overlap, get the highest zorder
1477-
axes_list.sort(key=lambda x: x.zorder)
1478-
self.inaxes = axes_list[-1] # Use the highest zorder
1479-
else: # Just found one hit
1480-
self.inaxes = axes_list[0]
14811478

14821479
try:
14831480
trans = self.inaxes.transData.inverted()
@@ -1751,8 +1748,7 @@ def onRemove(self, ev):
17511748
canvas.mpl_connect('mouse_press_event',canvas.onRemove)
17521749
"""
17531750
# Find the top artist under the cursor
1754-
under = self.figure.hitlist(ev)
1755-
under.sort(key=lambda x: x.zorder)
1751+
under = sorted(self.figure.hitlist(ev), key=lambda x: x.zorder)
17561752
h = None
17571753
if under:
17581754
h = under[-1]

lib/matplotlib/backends/backend_gdk.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3636
_debug = False
3737

3838
# Image formats that this backend supports - for FileChooser and print_figure()
39-
IMAGE_FORMAT = ['eps', 'jpg', 'png', 'ps', 'svg'] + ['bmp'] # , 'raw', 'rgb']
40-
IMAGE_FORMAT.sort()
41-
IMAGE_FORMAT_DEFAULT = 'png'
39+
IMAGE_FORMAT = sorted(['eps', 'jpg', 'png', 'ps', 'svg'] + ['bmp']) # , 'raw', 'rgb']
40+
IMAGE_FORMAT_DEFAULT = 'png'
4241

4342

4443
class RendererGDK(RendererBase):

lib/matplotlib/backends/backend_gtk.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -829,33 +829,32 @@ def __init__ (self,
829829
filetypes = [],
830830
default_filetype = None
831831
):
832-
super(FileChooserDialog, self).__init__ (title, parent, action,
833-
buttons)
832+
super(FileChooserDialog, self).__init__(title, parent, action, buttons)
834833
super(FileChooserDialog, self).set_do_overwrite_confirmation(True)
835-
self.set_default_response (gtk.RESPONSE_OK)
834+
self.set_default_response(gtk.RESPONSE_OK)
836835

837-
if not path: path = os.getcwd() + os.sep
836+
if not path:
837+
path = os.getcwd() + os.sep
838838

839839
# create an extra widget to list supported image formats
840840
self.set_current_folder (path)
841841
self.set_current_name ('image.' + default_filetype)
842842

843-
hbox = gtk.HBox (spacing=10)
844-
hbox.pack_start (gtk.Label ("File Format:"), expand=False)
843+
hbox = gtk.HBox(spacing=10)
844+
hbox.pack_start(gtk.Label ("File Format:"), expand=False)
845845

846846
liststore = gtk.ListStore(gobject.TYPE_STRING)
847847
cbox = gtk.ComboBox(liststore)
848848
cell = gtk.CellRendererText()
849849
cbox.pack_start(cell, True)
850850
cbox.add_attribute(cell, 'text', 0)
851-
hbox.pack_start (cbox)
851+
hbox.pack_start(cbox)
852852

853853
self.filetypes = filetypes
854-
self.sorted_filetypes = list(six.iteritems(filetypes))
855-
self.sorted_filetypes.sort()
854+
self.sorted_filetypes = sorted(six.iteritems(filetypes))
856855
default = 0
857856
for i, (ext, name) in enumerate(self.sorted_filetypes):
858-
cbox.append_text ("%s (*.%s)" % (name, ext))
857+
cbox.append_text("%s (*.%s)" % (name, ext))
859858
if ext == default_filetype:
860859
default = i
861860
cbox.set_active(default)
@@ -874,8 +873,8 @@ def cb_cbox_changed (cbox, data=None):
874873
elif ext == '':
875874
filename = filename.rstrip('.') + '.' + new_ext
876875

877-
self.set_current_name (filename)
878-
cbox.connect ("changed", cb_cbox_changed)
876+
self.set_current_name(filename)
877+
cbox.connect("changed", cb_cbox_changed)
879878

880879
hbox.show_all()
881880
self.set_extra_widget(hbox)

0 commit comments

Comments
 (0)