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

Skip to content

Commit 94587b1

Browse files
committed
Small changes in response to code review
Improve a docstring, remove unneeded parens from an assert, open a file as binary instead of encoding each line read from it, don't call six.b on variable strings, simplify string handling, improve the formatting of a matplotlib.verbose.report call.
1 parent 8fa303f commit 94587b1

2 files changed

Lines changed: 25 additions & 31 deletions

File tree

lib/matplotlib/dviread.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -747,14 +747,10 @@ class Tfm(object):
747747
Used for verifying against the dvi file.
748748
design_size : int
749749
Design size of the font (unknown units)
750-
width : dict
751-
Width of each character, needs to be scaled by the factor
752-
specified in the dvi file. This is a dict because indexing may
750+
width, height, depth : dict
751+
Dimensions of each character, need to be scaled by the factor
752+
specified in the dvi file. These are dicts because indexing may
753753
not start from 0.
754-
height : dict
755-
Height of each character.
756-
depth : dict
757-
Depth of each character.
758754
"""
759755
__slots__ = ('checksum', 'design_size', 'width', 'height', 'depth')
760756

@@ -844,25 +840,25 @@ def __init__(self, filename):
844840
self._filename = filename
845841
if six.PY3 and isinstance(filename, bytes):
846842
self._filename = filename.decode('ascii', errors='replace')
847-
with open(filename, 'rt') as file:
843+
with open(filename, 'rb') as file:
848844
self._parse(file)
849845

850846
def __getitem__(self, texname):
851-
assert(isinstance(texname, bytes))
847+
assert isinstance(texname, bytes)
852848
try:
853849
result = self._font[texname]
854850
except KeyError:
855-
matplotlib.verbose.report(textwrap.fill
856-
('A PostScript file for the font whose TeX name is "%s" '
857-
'could not be found in the file "%s". The dviread module '
858-
'can only handle fonts that have an associated PostScript '
859-
'font file. '
860-
'This problem can often be solved by installing '
861-
'a suitable PostScript font package in your (TeX) '
862-
'package manager.' % (texname.decode('ascii'),
863-
self._filename),
864-
break_on_hyphens=False, break_long_words=False),
865-
'helpful')
851+
fmt = ('A PostScript file for the font whose TeX name is "{0}" '
852+
'could not be found in the file "{1}". The dviread module '
853+
'can only handle fonts that have an associated PostScript '
854+
'font file. '
855+
'This problem can often be solved by installing '
856+
'a suitable PostScript font package in your (TeX) '
857+
'package manager.')
858+
msg = fmt.format(texname.decode('ascii'), self._filename)
859+
msg = textwrap.fill(msg, break_on_hyphens=False,
860+
break_long_words=False)
861+
matplotlib.verbose.report(msg, 'helpful')
866862
raise
867863
fn, enc = result.filename, result.encoding
868864
if fn is not None and not fn.startswith(b'/'):
@@ -873,7 +869,6 @@ def __getitem__(self, texname):
873869

874870
def _parse(self, file):
875871
for line in file:
876-
line = six.b(line)
877872
line = line.strip()
878873
if line == b'' or line.startswith(b'%'):
879874
continue
@@ -979,21 +974,20 @@ def __iter__(self):
979974
def _parse(self, file):
980975
result = []
981976

982-
lines = (line[:line.find(b'%')] if b'%' in line else line.strip()
983-
for line in file)
977+
lines = (line.split(b'%', 1)[0].strip() for line in file)
984978
data = b''.join(lines)
985-
match = re.search(six.b(r'\['), data)
986-
if not match:
979+
beginning = data.find(b'[')
980+
if beginning < 0:
987981
raise ValueError("Cannot locate beginning of encoding in {}"
988982
.format(file))
989-
data = data[match.span()[1]:]
990-
match = re.search(six.b(r'\]'), data)
991-
if not match:
983+
data = data[beginning:]
984+
end = data.find(b']')
985+
if end < 0:
992986
raise ValueError("Cannot locate end of encoding in {}"
993987
.format(file))
994-
data = data[:match.span()[0]]
988+
data = data[:end]
995989

996-
return re.findall(six.b(r'/([^][{}<>\s]+)'), data)
990+
return re.findall(br'/([^][{}<>\s]+)', data)
997991

998992

999993
def find_tex_file(filename, format=None):

lib/matplotlib/tests/test_dviread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_dviread():
6161
with open(os.path.join(dir, 'test.json')) as f:
6262
correct = json.load(f)
6363
for entry in correct:
64-
entry['text'] = [[a, b, c, six.b(d), e]
64+
entry['text'] = [[a, b, c, d.encode('ascii'), e]
6565
for [a, b, c, d, e] in entry['text']]
6666
with dr.Dvi(os.path.join(dir, 'test.dvi'), None) as dvi:
6767
data = [{'text': [[t.x, t.y,

0 commit comments

Comments
 (0)