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

Skip to content

Minor cleanups. #10248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,12 +1154,11 @@ def print_figure_impl(fh):
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)

if passed_in_file_object:
if file_requires_unicode(outfile):
with io.open(tmpfile, 'rb') as fh:
outfile.write(fh.read().decode('latin-1'))
else:
with io.open(tmpfile, 'rb') as fh:
outfile.write(fh.read())
fh = (io.open(tmpfile, 'r', encoding='latin-1')
if file_requires_unicode(outfile)
else io.open(tmpfile, 'rb'))
with fh:
shutil.copyfileobj(fh, outfile)
else:
with io.open(outfile, 'w') as fh:
pass
Expand Down Expand Up @@ -1354,12 +1353,10 @@ def write(self, *kl, **kwargs):
rotated=psfrag_rotated)

if is_writable_file_like(outfile):
if file_requires_unicode(outfile):
with io.open(tmpfile, 'rb') as fh:
outfile.write(fh.read().decode('latin-1'))
else:
with io.open(tmpfile, 'rb') as fh:
outfile.write(fh.read())
with (io.open(tmpfile, 'r', encoding='latin-1')
if file_requires_unicode(outfile)
else io.open(tmpfile, 'rb')) as fh:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the risk of bikeshedding... I find this 3-line construct a bit of a trip hazard when reading. How about this:

if file_requires_unicode(outfile):
    kwd = dict(mode='r', encoding='latin-1')
else:
    kwd = dict(mode='rb')
with io.open(tmpfile, **kwd) as fh:

Or if you want to reduce the line count:

kwd = (dict(mode='r', encoding='latin-1') if file_requires_unicode(outfile)
            else dict(mode='rb'))
with io.open(tmpfile, **kwd) as fh:

The latter option is no longer than your original, and to me, much more readable. I haven't checked, but I hope it doesn't fall afoul of the line length limit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I totally agree w/ this but thought maybe I was being an anti-fancy-python-tricks stick-in-the-mud

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new version seems a reasonable compromise?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this actually changed? It's not marked outdated.

Copy link
Contributor Author

@anntzer anntzer Jan 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry. There were two instances of it, only changed one of them. Will make a separate PR.

shutil.copyfileobj(fh, outfile)
else:
with io.open(outfile, 'wb') as fh:
pass
Expand Down
7 changes: 2 additions & 5 deletions lib/matplotlib/fontconfig_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

# This class is defined here because it must be available in:
# - The old-style config framework (:file:`rcsetup.py`)
# - The traits-based config framework (:file:`mpltraits.py`)
# - The font manager (:file:`font_manager.py`)

# It probably logically belongs in :file:`font_manager.py`, but
# placing it in any of these places would have created cyclical
# dependency problems, or an undesired dependency on traits even
# when the traits-based config framework is not used.
# It probably logically belongs in :file:`font_manager.py`, but placing it
# there would have created cyclical dependency problems.

from __future__ import (absolute_import, division, print_function,
unicode_literals)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/type1font.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def italicangle(angle):
.encode('ascii'))

def fontmatrix(array):
array = array.lstrip(b'[').rstrip(b']').strip().split()
array = array.lstrip(b'[').rstrip(b']').split()
array = [float(x) for x in array]
oldmatrix = np.eye(3, 3)
oldmatrix[0:3, 0] = array[::2]
Expand Down