From 4a00acd12cbbbeb88c5f9867e564174488d8407b Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Tue, 9 Oct 2012 22:10:55 +0200 Subject: [PATCH 1/2] PEP8 fixes on type1font.py --- lib/matplotlib/type1font.py | 77 +++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/lib/matplotlib/type1font.py b/lib/matplotlib/type1font.py index 6c5d676d8df1..906100473fda 100644 --- a/lib/matplotlib/type1font.py +++ b/lib/matplotlib/type1font.py @@ -35,6 +35,7 @@ def ord(x): return x + class Type1Font(object): """ A class representing a Type-1 font, for use by backends. @@ -75,13 +76,13 @@ def _read(self, file): data = b'' while len(rawdata) > 0: if not rawdata.startswith(b'\x80'): - raise RuntimeError('Broken pfb file (expected byte 128, got %d)' % \ - ord(rawdata[0])) + raise RuntimeError('Broken pfb file (expected byte 128, ' + 'got %d)' % ord(rawdata[0])) type = ord(rawdata[1]) - if type in (1,2): + if type in (1, 2): length, = struct.unpack('{}/%[]+') _comment = re.compile(br'%[^\r\n\v]*') _instring = re.compile(br'[()\\]') + @classmethod def _tokens(cls, text): """ @@ -146,7 +149,8 @@ def _tokens(cls, text): """ pos = 0 while pos < len(text): - match = cls._comment.match(text[pos:]) or cls._whitespace.match(text[pos:]) + match = cls._comment.match(text[pos:]) or \ + cls._whitespace.match(text[pos:]) if match: yield ('whitespace', match.group()) pos += match.end() @@ -156,17 +160,18 @@ def _tokens(cls, text): depth = 1 while depth: match = cls._instring.search(text[pos:]) - if match is None: return + if match is None: + return pos += match.end() if match.group() == '(': depth += 1 elif match.group() == ')': depth -= 1 - else: # a backslash - skip the next character + else: # a backslash - skip the next character pos += 1 yield ('string', text[start:pos]) - elif text[pos:pos+2] in ('<<', '>>'): - yield ('delimiter', text[pos:pos+2]) + elif text[pos:pos + 2] in ('<<', '>>'): + yield ('delimiter', text[pos:pos + 2]) pos += 2 elif text[pos] == '<': start = pos @@ -192,8 +197,8 @@ def _parse(self): Compatibility" of the Type-1 spec. """ # Start with reasonable defaults - prop = { 'weight': 'Regular', 'ItalicAngle': 0.0, 'isFixedPitch': False, - 'UnderlinePosition': -100, 'UnderlineThickness': 50 } + prop = {'weight': 'Regular', 'ItalicAngle': 0.0, 'isFixedPitch': False, + 'UnderlinePosition': -100, 'UnderlineThickness': 50} tokenizer = self._tokens(self.parts[0]) filtered = itertools.ifilter(lambda x: x[0] != 'whitespace', tokenizer) for token, value in filtered: @@ -208,16 +213,20 @@ def _parse(self): elif token == b'string': value = value.lstrip(b'(').rstrip(b')') elif token == b'number': - if b'.' in value: value = float(value) - else: value = int(value) - else: # more complicated value such as an array + if b'.' in value: + value = float(value) + else: + value = int(value) + else: # more complicated value such as an array value = None if key != b'FontInfo' and value is not None: prop[key] = value # Fill in the various *Name properties if 'FontName' not in prop: - prop['FontName'] = prop.get('FullName') or prop.get('FamilyName') or 'Unknown' + prop['FontName'] = (prop.get('FullName') or + prop.get('FamilyName') or + 'Unknown') if 'FullName' not in prop: prop['FullName'] = prop['FontName'] if 'FamilyName' not in prop: @@ -230,25 +239,27 @@ def _parse(self): def _transformer(cls, tokens, slant, extend): def fontname(name): result = name - if slant: result += '_Slant_' + str(int(1000*slant)) - if extend != 1.0: result += '_Extend_' + str(int(1000*extend)) + if slant: + result += '_Slant_' + str(int(1000 * slant)) + if extend != 1.0: + result += '_Extend_' + str(int(1000 * extend)) return result def italicangle(angle): - return str(float(angle) - np.arctan(slant)/np.pi*180) + return str(float(angle) - np.arctan(slant) / np.pi * 180) def fontmatrix(array): array = array.lstrip('[').rstrip(']').strip().split() - array = [ float(x) for x in array ] - oldmatrix = np.eye(3,3) - oldmatrix[0:3,0] = array[::2] - oldmatrix[0:3,1] = array[1::2] + array = [float(x) for x in array] + oldmatrix = np.eye(3, 3) + oldmatrix[0:3, 0] = array[::2] + oldmatrix[0:3, 1] = array[1::2] modifier = np.array([[extend, 0, 0], [slant, 1, 0], [0, 0, 1]]) newmatrix = np.dot(modifier, oldmatrix) - array[::2] = newmatrix[0:3,0] - array[1::2] = newmatrix[0:3,1] + array[::2] = newmatrix[0:3, 0] + array[1::2] = newmatrix[0:3, 1] return '[' + ' '.join(str(x) for x in array) + ']' def replace(fun): @@ -275,15 +286,16 @@ def suppress(tokens): pass yield '' - table = { '/FontName': replace(fontname), - '/ItalicAngle': replace(italicangle), - '/FontMatrix': replace(fontmatrix), - '/UniqueID': suppress } + table = {'/FontName': replace(fontname), + '/ItalicAngle': replace(italicangle), + '/FontMatrix': replace(fontmatrix), + '/UniqueID': suppress} while True: token, value = next(tokens) if token == 'name' and value in table: - for value in table[value](itertools.chain([(token, value)], tokens)): + for value in table[value](itertools.chain([(token, value)], + tokens)): yield value else: yield value @@ -311,4 +323,3 @@ def transform(self, effects): buffer.close() return Type1Font((result, self.parts[1], self.parts[2])) - From 857b53d47c5340ce0a4e90157942453ee10e7134 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Tue, 9 Oct 2012 22:48:53 +0200 Subject: [PATCH 2/2] PEP8 fixes - small backslashes and breaks fixes --- lib/matplotlib/type1font.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/type1font.py b/lib/matplotlib/type1font.py index 906100473fda..522cd1fd5324 100644 --- a/lib/matplotlib/type1font.py +++ b/lib/matplotlib/type1font.py @@ -149,8 +149,8 @@ def _tokens(cls, text): """ pos = 0 while pos < len(text): - match = cls._comment.match(text[pos:]) or \ - cls._whitespace.match(text[pos:]) + match = (cls._comment.match(text[pos:]) or + cls._whitespace.match(text[pos:])) if match: yield ('whitespace', match.group()) pos += match.end()