More elegant solution for diffing two sequences
Ulrich Eckhardt
doomster at knuut.de
Thu Dec 3 16:28:09 EST 2009
Hi!
I'm trying to write some code to diff two fonts. What I have is every
character (glyph) of the two fonts in a list. I know that the list is sorted
by the codepoints of the characters. What I'd like to ask is whether there
is a more elegant solution to the loop below or whether there are any rough
corners in my code (see below). Note that I'm targeting Python 2, not 3 yet.
Thank you!
Uli
def diff_font(font1, font2):
it1 = iter(font1.glyphs)
it2 = iter(font2.glyphs)
try:
glyph1 = it1.next()
glyph2 = it2.next()
while True:
if glyph1.codepoint < glyph2.codepoint:
# glyph only in first font
diff_glyph(glyph1, None)
glyph1 = it1.next()
elif glyph2.codepoint < glyph2.codepoint:
# glyph only in second font
diff_glyph(None, glyph2)
glyph2 = it2.next()
else:
# glyph in both fonts
diff_glyph(glyph1, glyph2)
glyph1 = it1.next()
glyph2 = it2.next()
except StopIteration, msg:
# remaining glyphs in either font are exclusive to that font
for glyph in it1:
diff_glyph(glyph, None)
for glyph in it1:
diff_glyph(None, glyph)
More information about the Python-list
mailing list