Replace xrange() by range(), for python3

In Pyton 3, range() is implemented like xrange() in Python 2,
and there is no xrange() anymore. The savings from xrange() are
too small for us to really bother, so we choose to live inefficiently
on Python 2.
This commit is contained in:
Sascha Brawer 2015-05-05 15:27:04 +02:00
parent 9bb1ca02ba
commit e4377af3fd
2 changed files with 20 additions and 20 deletions

View File

@ -56,7 +56,7 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
axisTags = [axis.AxisTag for axis in ttFont["fvar"].table.VariationAxis] axisTags = [axis.AxisTag for axis in ttFont["fvar"].table.VariationAxis]
sharedCoords = self.compileSharedCoords_(ttFont, axisTags) sharedCoords = self.compileSharedCoords_(ttFont, axisTags)
sharedCoordIndices = dict([(sharedCoords[i], i) for i in xrange(len(sharedCoords))]) sharedCoordIndices = dict([(sharedCoords[i], i) for i in range(len(sharedCoords))])
sharedCoordSize = sum([len(c) for c in sharedCoords]) sharedCoordSize = sum([len(c) for c in sharedCoords])
compiledGlyphs = self.compileGlyphs_(ttFont, axisTags, sharedCoordIndices) compiledGlyphs = self.compileGlyphs_(ttFont, axisTags, sharedCoordIndices)
@ -145,7 +145,7 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
# (a) each tuple supplies its own private set of points; # (a) each tuple supplies its own private set of points;
# (b) all tuples refer to a shared set of points, which consists of # (b) all tuples refer to a shared set of points, which consists of
# "every control point in the glyph". # "every control point in the glyph".
allPoints = set(xrange(numPointsInGlyph)) allPoints = set(range(numPointsInGlyph))
tuples = [] tuples = []
data = [] data = []
someTuplesSharePoints = False someTuplesSharePoints = False
@ -184,7 +184,7 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
format=(self.flags & 1), glyphCount=self.glyphCount) format=(self.flags & 1), glyphCount=self.glyphCount)
sharedCoords = self.decompileSharedCoords_(axisTags, data) sharedCoords = self.decompileSharedCoords_(axisTags, data)
self.variations = {} self.variations = {}
for i in xrange(self.glyphCount): for i in range(self.glyphCount):
glyphName = glyphs[i] glyphName = glyphs[i]
glyph = ttFont["glyf"][glyphName] glyph = ttFont["glyf"][glyphName]
numPoints = self.getNumPoints_(glyph) numPoints = self.getNumPoints_(glyph)
@ -229,7 +229,7 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
of the 'gvar' header. of the 'gvar' header.
""" """
assert len(offsets) >= 2 assert len(offsets) >= 2
for i in xrange(1, len(offsets)): for i in range(1, len(offsets)):
assert offsets[i - 1] <= offsets[i] assert offsets[i - 1] <= offsets[i]
if max(offsets) <= 0xffff * 2: if max(offsets) <= 0xffff * 2:
packed = array.array(b"H", [n >> 1 for n in offsets]) packed = array.array(b"H", [n >> 1 for n in offsets])
@ -253,7 +253,7 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
sharedPoints, dataPos = GlyphVariation.decompilePoints_(numPoints, data, dataPos) sharedPoints, dataPos = GlyphVariation.decompilePoints_(numPoints, data, dataPos)
else: else:
sharedPoints = [] sharedPoints = []
for i in xrange(flags & TUPLE_COUNT_MASK): for i in range(flags & TUPLE_COUNT_MASK):
dataSize, flags = struct.unpack(b">HH", data[pos:pos+4]) dataSize, flags = struct.unpack(b">HH", data[pos:pos+4])
tupleSize = GlyphVariation.getTupleSize_(flags, numAxes) tupleSize = GlyphVariation.getTupleSize_(flags, numAxes)
tuple = data[pos : pos + tupleSize] tuple = data[pos : pos + tupleSize]
@ -366,7 +366,7 @@ class GlyphVariation:
def getUsedPoints(self): def getUsedPoints(self):
result = set() result = set()
for p in xrange(len(self.coordinates)): for p in range(len(self.coordinates)):
if self.coordinates[p] != (0, 0): if self.coordinates[p] != (0, 0):
result.add(p) result.add(p)
return result return result
@ -398,7 +398,7 @@ class GlyphVariation:
min=minValue, max=maxValue) min=minValue, max=maxValue)
writer.newline() writer.newline()
wrote_any_points = False wrote_any_points = False
for i in xrange(len(self.coordinates)): for i in range(len(self.coordinates)):
x, y = self.coordinates[i] x, y = self.coordinates[i]
if x != 0 or y != 0: if x != 0 or y != 0:
writer.simpletag("delta", pt=i, x=x, y=y) writer.simpletag("delta", pt=i, x=x, y=y)
@ -489,7 +489,7 @@ class GlyphVariation:
def decompileCoords_(axisTags, numCoords, data, offset): def decompileCoords_(axisTags, numCoords, data, offset):
result = [] result = []
pos = offset pos = offset
for i in xrange(numCoords): for i in range(numCoords):
coord, pos = GlyphVariation.decompileCoord_(axisTags, data, pos) coord, pos = GlyphVariation.decompileCoord_(axisTags, data, pos)
result.append(coord) result.append(coord)
return result, pos return result, pos
@ -561,12 +561,12 @@ class GlyphVariation:
numPointsInRun = (runHeader & POINT_RUN_COUNT_MASK) + 1 numPointsInRun = (runHeader & POINT_RUN_COUNT_MASK) + 1
point = 0 point = 0
if (runHeader & POINTS_ARE_WORDS) == 0: if (runHeader & POINTS_ARE_WORDS) == 0:
for i in xrange(numPointsInRun): for i in range(numPointsInRun):
point += ord(data[pos]) point += ord(data[pos])
pos += 1 pos += 1
result.append(point) result.append(point)
else: else:
for i in xrange(numPointsInRun): for i in range(numPointsInRun):
point += struct.unpack(">H", data[pos:pos+2])[0] point += struct.unpack(">H", data[pos:pos+2])[0]
pos += 2 pos += 2
result.append(point) result.append(point)
@ -644,7 +644,7 @@ class GlyphVariation:
runLength += 1 runLength += 1
assert runLength >= 1 and runLength <= 64 assert runLength >= 1 and runLength <= 64
stream.write(bytechr(runLength - 1)) stream.write(bytechr(runLength - 1))
for i in xrange(offset, pos): for i in range(offset, pos):
stream.write(struct.pack('b', deltas[i])) stream.write(struct.pack('b', deltas[i]))
return pos return pos
@ -678,7 +678,7 @@ class GlyphVariation:
runLength += 1 runLength += 1
assert runLength >= 1 and runLength <= 64 assert runLength >= 1 and runLength <= 64
stream.write(bytechr(DELTAS_ARE_WORDS | (runLength - 1))) stream.write(bytechr(DELTAS_ARE_WORDS | (runLength - 1)))
for i in xrange(offset, pos): for i in range(offset, pos):
stream.write(struct.pack('>h', deltas[i])) stream.write(struct.pack('>h', deltas[i]))
return pos return pos
@ -694,11 +694,11 @@ class GlyphVariation:
if (runHeader & DELTAS_ARE_ZERO) != 0: if (runHeader & DELTAS_ARE_ZERO) != 0:
result.extend([0] * numDeltasInRun) result.extend([0] * numDeltasInRun)
elif (runHeader & DELTAS_ARE_WORDS) != 0: elif (runHeader & DELTAS_ARE_WORDS) != 0:
for i in xrange(numDeltasInRun): for i in range(numDeltasInRun):
result.append(struct.unpack(">h", data[pos:pos+2])[0]) result.append(struct.unpack(">h", data[pos:pos+2])[0])
pos += 2 pos += 2
else: else:
for i in xrange(numDeltasInRun): for i in range(numDeltasInRun):
result.append(struct.unpack(">b", data[pos])[0]) result.append(struct.unpack(">b", data[pos])[0])
pos += 1 pos += 1
assert len(result) == numDeltas assert len(result) == numDeltas

View File

@ -361,7 +361,7 @@ class GlyphVariationTest(unittest.TestCase):
" 7F 00" + (127 * " 01") + # first run, contains 128 points: [0 .. 127] " 7F 00" + (127 * " 01") + # first run, contains 128 points: [0 .. 127]
" 7F 80" + (127 * " 01") + # second run, contains 128 points: [128 .. 511] " 7F 80" + (127 * " 01") + # second run, contains 128 points: [128 .. 511]
" AB 01 00" + (43 * " 00 01"), # third run, contains 44 points: [512 .. 299] " AB 01 00" + (43 * " 00 01"), # third run, contains 44 points: [512 .. 299]
hexencode(compilePoints(set(xrange(300))))) hexencode(compilePoints(set(range(300)))))
def test_decompilePoints(self): def test_decompilePoints(self):
decompilePoints = GlyphVariation.decompilePoints_ decompilePoints = GlyphVariation.decompilePoints_
@ -397,8 +397,8 @@ class GlyphVariationTest(unittest.TestCase):
numPointsInGlyph = 500 # greater than 255, so we also exercise code path for 16-bit encoding numPointsInGlyph = 500 # greater than 255, so we also exercise code path for 16-bit encoding
compile = GlyphVariation.compilePoints compile = GlyphVariation.compilePoints
decompile = lambda data: set(GlyphVariation.decompilePoints_(numPointsInGlyph, data, 0)[0]) decompile = lambda data: set(GlyphVariation.decompilePoints_(numPointsInGlyph, data, 0)[0])
for i in xrange(50): for i in range(50):
points = set(random.sample(xrange(numPointsInGlyph), 30)) points = set(random.sample(range(numPointsInGlyph), 30))
self.assertSetEqual(points, decompile(compile(points)), self.assertSetEqual(points, decompile(compile(points)),
"failed round-trip decompile/compilePoints; points=%s" % points) "failed round-trip decompile/compilePoints; points=%s" % points)
@ -465,9 +465,9 @@ class GlyphVariationTest(unittest.TestCase):
numDeltas = 30 numDeltas = 30
compile = GlyphVariation.compileDeltaValues_ compile = GlyphVariation.compileDeltaValues_
decompile = lambda data: GlyphVariation.decompileDeltas_(numDeltas, data, 0)[0] decompile = lambda data: GlyphVariation.decompileDeltas_(numDeltas, data, 0)[0]
for i in xrange(50): for i in range(50):
deltas = random.sample(xrange(-128, 127), 10) deltas = random.sample(range(-128, 127), 10)
deltas.extend(random.sample(xrange(-32768, 32767), 10)) deltas.extend(random.sample(range(-32768, 32767), 10))
deltas.extend([0] * 10) deltas.extend([0] * 10)
random.shuffle(deltas) random.shuffle(deltas)
self.assertListEqual(deltas, decompile(compile(deltas)), self.assertListEqual(deltas, decompile(compile(deltas)),