Cleanup: sorted, dict iteration, array.{ndim,size}, ...#7549
Cleanup: sorted, dict iteration, array.{ndim,size}, ...#7549QuLogic merged 9 commits intomatplotlib:masterfrom
Conversation
d163a75 to
bec79f4
Compare
|
Do Unicode entities work in Python 2? I'm not seeing it in the Unicode howto. |
bec79f4 to
179b3ed
Compare
|
Yes, see table at https://docs.python.org/2.7/reference/lexical_analysis.html#string-literals (or try it yourself :-)). |
9d0a612 to
5044c22
Compare
Current coverage is 61.90% (diff: 60.69%)@@ master #7549 diff @@
==========================================
Files 173 173
Lines 56103 55917 -186
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
- Hits 34731 34614 -117
+ Misses 21372 21303 -69
Partials 0 0
|
d5fe5e4 to
d17d6b5
Compare
twmr
left a comment
There was a problem hiding this comment.
I didn't review the whole PR
lib/matplotlib/afm.py
Outdated
| break | ||
| for line in fh: | ||
| line = line.rstrip() | ||
| if len(line) == 0: |
There was a problem hiding this comment.
here you could have also used 'if not line'
| # the `default` and `classic` ones, which will be forced resp. in | ||
| # first and second position. | ||
| style_list = list(plt.style.available) # *new* list: avoids side effects. | ||
| style_list = sorted(plt.style.available) # *new* list: avoids side effects. |
3883fa0 to
b22d353
Compare
QuLogic
left a comment
There was a problem hiding this comment.
It's a behemoth, but I think I managed to get through it all.
lib/matplotlib/__init__.py
Outdated
| Return values in order of sorted keys. | ||
| """ | ||
| return [self[k] for k in self.keys()] | ||
| return [self[k] for k in self] |
There was a problem hiding this comment.
Does dropping .keys() and iterating over self correctly preserve the sorted order that the keys method from this subclass produces?
There was a problem hiding this comment.
No. But that also means that iteration on the dict itself (for k in rcparams) happens in a different order right now...
| pos = fh.tell() | ||
| try: | ||
| line = fh.readline() | ||
| line = next(fh) |
There was a problem hiding this comment.
The problem is that the outer loop may be using for line in fh first before passing the handle to the function at some point, and Py2 (not Py3) doesn't allow (raises an exception on) mixing for line in fh and fh.readline due to the fact that the former uses a readahead buffer (https://docs.python.org/2/library/stdtypes.html#file.next).
There was a problem hiding this comment.
But this is called before the iteration, and the next line is a seek, which should flush the readahead buffer anyway.
There was a problem hiding this comment.
I think the gain in legibility with .readline() doesn't compensate the brittleness of only being able to pass in a file with an empty readahead buffer.
| """ | ||
|
|
||
| line = fh.readline() | ||
| line = next(fh) |
There was a problem hiding this comment.
This one makes sense though; will need to remain next.
| locs = [ti[1] for ti in tick_tups] | ||
| locs.sort() | ||
| locs = np.array(locs) | ||
| if len(locs): |
There was a problem hiding this comment.
I understand this condition is probably not necessary, but I guess it could also be anded onto line 955?
There was a problem hiding this comment.
In fact it's probably needed, otherwise we may end up trying to get the first or last item of an empty array...
| IMAGE_FORMAT = ['eps', 'jpg', 'png', 'ps', 'svg'] + ['bmp'] # , 'raw', 'rgb'] | ||
| IMAGE_FORMAT.sort() | ||
| IMAGE_FORMAT_DEFAULT = 'png' | ||
| IMAGE_FORMAT = sorted(['eps', 'jpg', 'png', 'ps', 'svg'] + ['bmp']) # , 'raw', 'rgb'] |
There was a problem hiding this comment.
Sure, but I'll keep the sorted which makes the intent clear and should be cheap.
|
|
||
| epsfile = tmpfile + '.eps' | ||
| with io.open(epsfile, 'wb') as epsh: | ||
| with io.open(epsfile, 'wb') as epsh, io.open(tmpfile, 'rb') as tmph: |
There was a problem hiding this comment.
Do we even need io. when it's binary?
There was a problem hiding this comment.
What's the difference between io.open and open in Py2?
There was a problem hiding this comment.
I don't think there is one in binary mode, is there?
There was a problem hiding this comment.
I guess...
On the other hand there seems to be an awful lot use of temporary files in backend_ps, most of which could probably just get rewritten using pipes to communicate via the subprocesses' standard streams.
Another great cleanup project :-)
lib/matplotlib/table.py
Outdated
| @@ -331,8 +328,7 @@ def _get_grid_bbox(self, renderer): | |||
|
|
|||
| Only include those in the range (0,0) to (maxRow, maxCol)""" | |||
| boxes = [self._cells[pos].get_window_extent(renderer) | |||
There was a problem hiding this comment.
Since the value is actually used here, it might make sense to use items() below.
| if kwargs: | ||
| raise ValueError("Following keys are not processed: %s" % \ | ||
| ", ".join([str(k) for k in kwargs.keys()])) | ||
| raise ValueError("Following keys are not processed: %s" |
| p0, p1 = edges[edgei] | ||
| p0, p1 = min(self.tunit_edges(), | ||
| key=lambda edge: proj3d.line2d_seg_dist( | ||
| edge[0], edge[1], (xd, yd))) |
There was a problem hiding this comment.
I prefer to break after edge[1], but that's minor.
lib/mpl_toolkits/mplot3d/proj3d.py
Outdated
| tis = (vecw[0] >= 0) * (vecw[0] <= 1) * (vecw[1] >= 0) * (vecw[1] <= 1) | ||
| if np.sometrue(tis): | ||
| tis = vecw[1] < 1 | ||
| tis = (vecw[0] >= 0) & (vecw[0] <= 1) & (vecw[1] >= 0) & (vecw[1] <= 1) |
There was a problem hiding this comment.
Since you're editing the line already, can you reorder it so that it looks like an implied and?
examples/misc/multiprocess.py
Outdated
|
|
||
| def call_back(): | ||
| while 1: | ||
| while True: |
There was a problem hiding this comment.
Cannot this be while self.pipe.poll(): (and remove if not self.pipe.poll(): break below)?
lib/matplotlib/axes/_base.py
Outdated
| while 1: | ||
|
|
||
| if len(remaining) == 0: | ||
| while True: |
lib/matplotlib/axes/_base.py
Outdated
| if not self.figure.canvas.is_saving(): | ||
| artists = [a for a in artists | ||
| if not a.get_animated() or a in self.images] | ||
| artists = sorted(artists, key=lambda artist: artist.get_zorder()) |
There was a problem hiding this comment.
Is not attrgetter faster than lambda?
There was a problem hiding this comment.
I doubt it matters but sure.
lib/matplotlib/cm.py
Outdated
| spec = datad[cmapname] | ||
| spec_reversed = _reverse_cmap_spec(spec) | ||
| datad[cmapname + '_r'] = spec_reversed | ||
| for cmapname, spec in list(six.iteritems(datad)): |
There was a problem hiding this comment.
No because we're modifying the dict at the same time; sidestepped the issue using update.
3b5197c to
e809aaf
Compare
lib/matplotlib/table.py
Outdated
| boxes = [self._cells[pos].get_window_extent(renderer) | ||
| for pos in six.iterkeys(self._cells) | ||
| if pos[0] >= 0 and pos[1] >= 0] | ||
| for pos in self._cells if pos[0] >= 0 and pos[1] >= 0] |
There was a problem hiding this comment.
I knew I should have commented the first time, but here's another one that could use items.
e809aaf to
32df815
Compare
32df815 to
84baecd
Compare
|
Can you rebase this PR? |
| style_list.sort() | ||
| style_list.insert(0, u'default') | ||
| style_list.insert(1, u'classic') | ||
| style_list = ['default', 'classic'] + sorted( |
| line = fh.readline() | ||
| if not line: | ||
| break | ||
| for line in fh: |
|
After Christmas break, probably. |
84baecd to
14b47ba
Compare
|
Actually it wasn't that bad. |
sortedwhereever it improves readbility overlist.sort.iterkeys.N{...}unicode entities.