Simplified Python code, without changing behavior.

This commit is contained in:
Project Nayuki 2016-10-24 07:27:48 +00:00
parent 18834e548b
commit 941dd14cc7
1 changed files with 5 additions and 11 deletions

View File

@ -468,8 +468,7 @@ class QrCode(object):
# 2*2 blocks of modules having same color # 2*2 blocks of modules having same color
for y in range(size - 1): for y in range(size - 1):
for x in range(size - 1): for x in range(size - 1):
color = modules[y][x] if modules[y][x] == modules[y][x + 1] == modules[y + 1][x] == modules[y + 1][x + 1]:
if color == modules[y][x + 1] == modules[y + 1][x] == modules[y + 1][x + 1]:
result += QrCode._PENALTY_N2 result += QrCode._PENALTY_N2
# Finder-like pattern in rows # Finder-like pattern in rows
@ -488,11 +487,7 @@ class QrCode(object):
result += QrCode._PENALTY_N3 result += QrCode._PENALTY_N3
# Balance of black and white modules # Balance of black and white modules
black = 0 black = sum(1 for x in range(size) for y in range(size) if modules[y][x])
for row in modules:
for color in row:
if color:
black += 1
total = size**2 total = size**2
# Find smallest k such that (45-5k)% <= dark/total <= (55+5k)% # Find smallest k such that (45-5k)% <= dark/total <= (55+5k)%
for k in itertools.count(): for k in itertools.count():
@ -622,10 +617,9 @@ class QrSegment(object):
def make_bytes(data): def make_bytes(data):
"""Returns a segment representing the given binary data encoded in byte mode.""" """Returns a segment representing the given binary data encoded in byte mode."""
bb = _BitBuffer() bb = _BitBuffer()
py3 = sys.version_info.major >= 3
for b in data: for b in data:
if sys.version_info[0] < 3: bb.append_bits((b if py3 else ord(b)), 8)
b = ord(b)
bb.append_bits(b, 8)
return QrSegment(QrSegment.Mode.BYTE, len(data), bb.get_bits()) return QrSegment(QrSegment.Mode.BYTE, len(data), bb.get_bits())
@ -663,7 +657,7 @@ class QrSegment(object):
def make_segments(text): def make_segments(text):
"""Returns a new mutable list of zero or more segments to represent the given Unicode text string. """Returns a new mutable list of zero or more segments to represent the given Unicode text string.
The result may use various segment modes and switch modes to optimize the length of the bit stream.""" The result may use various segment modes and switch modes to optimize the length of the bit stream."""
if not isinstance(text, str) and (sys.version_info[0] >= 3 or not isinstance(text, unicode)): if not (isinstance(text, str) or (sys.version_info.major < 3 and isinstance(text, unicode))):
raise TypeError("Text string expected") raise TypeError("Text string expected")
# Select the most efficient segment encoding automatically # Select the most efficient segment encoding automatically