py23 Remove uses of __cmp__ and cmp()

This commit is contained in:
Behdad Esfahbod 2013-11-27 18:58:45 -05:00
parent 960280bbd6
commit b7fd2e1913
12 changed files with 79 additions and 121 deletions

View File

@ -225,15 +225,13 @@ class AFM:
lines.append("StartCharMetrics " + repr(len(self._chars)))
items = [(charnum, (charname, width, box)) for charname, (charnum, width, box) in self._chars.items()]
def myCmp(a, b):
"""Custom compare function to make sure unencoded chars (-1)
def myKey(a):
"""Custom key function to make sure unencoded chars (-1)
end up at the end of the list after sorting."""
if a[0] == -1:
a = (0xffff,) + a[1:] # 0xffff is an arbitrary large number
if b[0] == -1:
b = (0xffff,) + b[1:]
return cmp(a, b)
items.sort(myCmp)
return a
items.sort(key=myKey)
for charnum, (charname, width, (l, b, r, t)) in items:
lines.append("C %d ; WX %d ; N %s ; B %d %d %d %d ;" %

View File

@ -280,7 +280,7 @@ class Transform:
"""
return self.__affine[i:j]
def __cmp__(self, other):
def __lt__(self, other):
"""Transform instances are comparable:
>>> t1 = Identity.scale(2, 3).translate(4, 6)
>>> t2 = Identity.translate(8, 18).scale(2, 3)
@ -301,7 +301,31 @@ class Transform:
"""
xx1, xy1, yx1, yy1, dx1, dy1 = self.__affine
xx2, xy2, yx2, yy2, dx2, dy2 = other
return cmp((xx1, xy1, yx1, yy1, dx1, dy1),
return (xx1, xy1, yx1, yy1, dx1, dy1) <
(xx2, xy2, yx2, yy2, dx2, dy2))
def __eq__(self, other):
"""Transform instances are comparable:
>>> t1 = Identity.scale(2, 3).translate(4, 6)
>>> t2 = Identity.translate(8, 18).scale(2, 3)
>>> t1 == t2
1
>>>
But beware of floating point rounding errors:
>>> t1 = Identity.scale(0.2, 0.3).translate(0.4, 0.6)
>>> t2 = Identity.translate(0.08, 0.18).scale(0.2, 0.3)
>>> t1
<Transform [0.2 0.0 0.0 0.3 0.08 0.18]>
>>> t2
<Transform [0.2 0.0 0.0 0.3 0.08 0.18]>
>>> t1 == t2
0
>>>
"""
xx1, xy1, yx1, yy1, dx1, dy1 = self.__affine
xx2, xy2, yx2, yy2, dx2, dy2 = other
return (xx1, xy1, yx1, yy1, dx1, dy1) ==
(xx2, xy2, yx2, yy2, dx2, dy2))
def __hash__(self):

View File

@ -36,9 +36,7 @@ class DefaultTable:
def __repr__(self):
return "<'%s' table at %x>" % (self.tableTag, id(self))
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.__dict__, other.__dict__)
def __eq__(self, other):
if type(self) != type(other):
raise TypeError("unordered types %s() < %s()", type(self), type(other))
return self.__dict__ == other.__dict__

View File

@ -224,16 +224,6 @@ class GlyphRecord:
datum = struct.pack(">L", self.offset)
data = data + datum
return data
def __cmp__(self, other):
"""Compare method, so a list of NameRecords can be sorted
according to the spec by just sorting it..."""
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.glyphID, other.glyphID)
def __repr__(self):
return "GlyphRecord[ glyphID: " + str(self.glyphID) + ", nMetaEntry: " + str(self.nMetaEntry) + ", offset: " + str(self.offset) + " ]"
@ -311,15 +301,6 @@ class StringRecord:
datum = struct.pack(">L", self.offset)
data = data + datum
return data
def __cmp__(self, other):
"""Compare method, so a list of NameRecords can be sorted
according to the spec by just sorting it..."""
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.labelID, other.labelID)
def __repr__(self):
return "StringRecord [ labelID: " + str(self.labelID) + " aka " + getLabelString(self.labelID) \

View File

@ -52,7 +52,7 @@ class table__c_m_a_p(DefaultTable.DefaultTable):
tables.append(table)
def compile(self, ttFont):
self.tables.sort() # sort according to the spec; see CmapSubtable.__cmp__()
self.tables.sort() # sort according to the spec; see CmapSubtable.__lt__()
numSubTables = len(self.tables)
totalOffset = 4 + 8 * numSubTables
data = struct.pack(">HH", self.tableVersion, numSubTables)
@ -149,10 +149,11 @@ class CmapSubtable:
writer.comment(Unicode[code])
writer.newline()
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
def __lt__(self, other):
if not isinstance(other, CmapSubtable):
raise TypeError("unordered types %s() < %s()", type(self), type(other))
# implemented so that list.sort() sorts according to the cmap spec.
# implemented so that list.sort() sorts according to the spec.
selfTuple = (
getattr(self, "platformID", None),
getattr(self, "platEncID", None),
@ -163,7 +164,7 @@ class CmapSubtable:
getattr(other, "platEncID", None),
getattr(other, "language", None),
other.__dict__)
return cmp(selfTuple, otherTuple)
return selfTuple < otherTuple
class cmap_format_0(CmapSubtable):
@ -1076,21 +1077,7 @@ def cvtFromUVS(val):
threeByteString = struct.pack(">L", val)[:3]
return threeByteString
def cmpUVSListEntry(first, second):
uv1, glyphName1 = first
uv2, glyphName2 = second
if (glyphName1 == None) and (glyphName2 != None):
return -1
elif (glyphName2 == None) and (glyphName1 != None):
return 1
ret = cmp(uv1, uv2)
if ret:
return ret
return cmp(glyphName1, glyphName2)
class cmap_format_14(CmapSubtable):
def decompileHeader(self, data, ttFont):
@ -1163,7 +1150,7 @@ class cmap_format_14(CmapSubtable):
uvsList = sorted(uvsDict.keys())
for uvs in uvsList:
uvList = uvsDict[uvs]
uvList.sort(cmpUVSListEntry)
uvList.sort(key=lambda item: (item[1] != None, item[0], item[1]))
for uv, gname in uvList:
if gname == None:
gname = "None"
@ -1177,7 +1164,7 @@ class cmap_format_14(CmapSubtable):
self.format = safeEval(attrs["format"])
self.length = safeEval(attrs["length"])
self.numVarSelectorRecords = safeEval(attrs["numVarSelectorRecords"])
self.language = 0xFF # provide a value so that CmapSubtable.__cmp__() won't fail
self.language = 0xFF # provide a value so that CmapSubtable.__lt__() won't fail
if not hasattr(self, "cmap"):
self.cmap = {} # so that clients that expect this to exist in a cmap table won't fail.
if not hasattr(self, "uvsDict"):

View File

@ -723,12 +723,11 @@ class Glyph:
data = data + "\0" * nPadBytes
self.data = data
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.__dict__, other.__dict__)
def __eq__(self, other):
if type(self) != type(other):
raise TypeError("unordered types %s() < %s()", type(self), type(other))
return self.__dict__ == other.__dict__
class GlyphComponent:
@ -886,11 +885,10 @@ class GlyphComponent:
self.transform = [[scale, 0], [0, scale]]
self.flags = safeEval(attrs["flags"])
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.__dict__, other.__dict__)
def __eq__(self, other):
if type(self) != type(other):
raise TypeError("unordered types %s() < %s()", type(self), type(other))
return self.__dict__ == other.__dict__
class GlyphCoordinates:
@ -967,6 +965,11 @@ class GlyphCoordinates:
a[2*i ] = int(.5 + x * t[0][0] + y * t[1][0])
a[2*i+1] = int(.5 + x * t[0][1] + y * t[1][1])
def __eq__(self, other):
if type(self) != type(other):
raise TypeError("unordered types %s() < %s()", type(self), type(other))
return self._a == other._a
def reprflag(flag):
bin = ""

View File

@ -86,19 +86,6 @@ class table__h_e_a_d(DefaultTable.DefaultTable):
else:
value = safeEval(value)
setattr(self, name, value)
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
selfdict = self.__dict__.copy()
otherdict = other.__dict__.copy()
# for testing purposes, compare without the modified and checkSumAdjustment
# fields, since they are allowed to be different.
for key in ["modified", "checkSumAdjustment"]:
del selfdict[key]
del otherdict[key]
return cmp(selfdict, otherdict)
def calc_mac_epoch_diff():

View File

@ -160,12 +160,6 @@ class KernTable_format_0:
def __delitem__(self, pair):
del self.kernTable[pair]
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.__dict__, other.__dict__)
class KernTable_format_2:

View File

@ -58,10 +58,4 @@ class table__l_o_c_a(DefaultTable.DefaultTable):
def __len__(self):
return len(self.locations)
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.locations, other.locations)

View File

@ -48,7 +48,7 @@ class table__n_a_m_e(DefaultTable.DefaultTable):
# only happens when there are NO name table entries read
# from the TTX file
self.names = []
self.names.sort() # sort according to the spec; see NameRecord.__cmp__()
self.names.sort() # sort according to the spec; see NameRecord.__lt__()
stringData = ""
format = 0
n = len(self.names)
@ -87,12 +87,6 @@ class table__n_a_m_e(DefaultTable.DefaultTable):
return namerecord
return None # not found
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.names, other.names)
class NameRecord:
@ -128,27 +122,26 @@ class NameRecord:
else:
self.string = s.encode("latin1")
def __cmp__(self, other):
"""Compare method, so a list of NameRecords can be sorted
according to the spec by just sorting it..."""
def __lt__(self, other):
if type(self) != type(other):
raise TypeError("unordered types %s() < %s()", type(self), type(other))
if not isinstance(self, type(other)): return cmp(type(self), type(other))
selftuple = (
# implemented so that list.sort() sorts according to the spec.
selfTuple = (
getattr(self, "platformID", None),
getattr(self, "platEncID", None),
getattr(self, "langID", None),
getattr(self, "nameID", None),
getattr(self, "string", None),
)
othertuple = (
otherTuple = (
getattr(other, "platformID", None),
getattr(other, "platEncID", None),
getattr(other, "langID", None),
getattr(other, "nameID", None),
getattr(other, "string", None),
)
return cmp(selftuple, othertuple)
return selfTuple < otherTuple
def __repr__(self):
return "<NameRecord NameID=%d; PlatformID=%d; LanguageID=%d>" % (

View File

@ -287,11 +287,10 @@ class OTTableWriter(object):
# only works after self._doneWriting() has been called
return hash(self.items)
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.items, other.items)
def __eq__(self, other):
if type(self) != type(other):
raise TypeError("unordered types %s() < %s()", type(self), type(other))
return self.items == other.items
def _doneWriting(self, internedTables=None):
# Convert CountData references to data string items
@ -674,13 +673,14 @@ class BaseTable(object):
else:
setattr(self, conv.name, value)
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
def __eq__(self, other):
if type(self) != type(other):
raise TypeError("unordered types %s() < %s()", type(self), type(other))
self.ensureDecompiled()
other.ensureDecompiled()
return cmp(self.__dict__, other.__dict__)
return self.__dict__ == other.__dict__
class FormatSwitchingBaseTable(BaseTable):
@ -840,8 +840,7 @@ class ValueRecord:
value.fromXML(name2, attrs2, content2, font)
setattr(self, name, value)
def __cmp__(self, other):
if not isinstance(self, type(other)): return cmp(type(self), type(other))
if self.__class__ != other.__class__: return cmp(self.__class__, other.__class__)
return cmp(self.__dict__, other.__dict__)
def __eq__(self, other):
if type(self) != type(other):
raise TypeError("unordered types %s() < %s()", type(self), type(other))
return self.__dict__ == other.__dict__

View File

@ -43,7 +43,7 @@ class Coverage(FormatSwitchingBaseTable):
# Some SIL fonts have coverage entries that don't have sorted
# StartCoverageIndex. If it is so, fixup and warn. We undo
# this when writing font out.
sorted_ranges = sorted(ranges, cmp=lambda a,b: cmp(a.StartCoverageIndex,b.StartCoverageIndex))
sorted_ranges = sorted(ranges, key=lambda a: a.StartCoverageIndex)
if ranges != sorted_ranges:
warnings.warn("GSUB/GPOS Coverage is not sorted by glyph ids.")
ranges = sorted_ranges
@ -106,7 +106,7 @@ class Coverage(FormatSwitchingBaseTable):
index = index + end - start + 1
if brokenOrder:
warnings.warn("GSUB/GPOS Coverage is not sorted by glyph ids.")
ranges.sort(cmp=lambda a,b: cmp(a.StartID,b.StartID))
ranges.sort(key=lambda a: a.StartID)
for r in ranges:
del r.StartID
format = 2