Fix #1
This commit is contained in:
parent
2621f934e2
commit
c974c52bad
|
@ -1,8 +1,8 @@
|
|||
import re
|
||||
import math
|
||||
|
||||
import six
|
||||
from six.moves import xrange
|
||||
# ~ import six
|
||||
# ~ from six.moves import xrange
|
||||
|
||||
from qrcode import base, exceptions, LUT
|
||||
|
||||
|
@ -32,8 +32,10 @@ MODE_SIZE_LARGE = {
|
|||
MODE_KANJI: 12,
|
||||
}
|
||||
|
||||
ALPHA_NUM = six.b('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:')
|
||||
RE_ALPHA_NUM = re.compile(six.b('^[') + re.escape(ALPHA_NUM) + six.b(r']*\Z'))
|
||||
|
||||
ALPHA_NUM = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'.encode("latin-1")
|
||||
RE_ALPHA_NUM = re.compile('^['.encode("latin-1") + re.escape(ALPHA_NUM) + r']*\Z'.encode("latin-1"))
|
||||
|
||||
|
||||
# The number of bits for numeric delimited data lengths.
|
||||
NUMBER_LENGTH = {3: 10, 2: 7, 1: 4}
|
||||
|
@ -96,8 +98,8 @@ PAD1 = 0x11
|
|||
_data_count = lambda block: block.data_count
|
||||
BIT_LIMIT_TABLE = [
|
||||
[0] + [8*sum(map(_data_count, base.rs_blocks(version, error_correction)))
|
||||
for version in xrange(1, 41)]
|
||||
for error_correction in xrange(4)
|
||||
for version in range(1, 41)]
|
||||
for error_correction in range(4)
|
||||
]
|
||||
|
||||
|
||||
|
@ -192,7 +194,7 @@ def lost_point(modules):
|
|||
def _lost_point_level1(modules, modules_count):
|
||||
lost_point = 0
|
||||
|
||||
modules_range = xrange(modules_count)
|
||||
modules_range = range(modules_count)
|
||||
container = [0] * (modules_count + 1)
|
||||
|
||||
for row in modules_range:
|
||||
|
@ -225,7 +227,7 @@ def _lost_point_level1(modules, modules_count):
|
|||
container[length] += 1
|
||||
|
||||
lost_point += sum(container[each_length] * (each_length - 2)
|
||||
for each_length in xrange(5, modules_count + 1))
|
||||
for each_length in range(5, modules_count + 1))
|
||||
|
||||
return lost_point
|
||||
|
||||
|
@ -233,7 +235,7 @@ def _lost_point_level1(modules, modules_count):
|
|||
def _lost_point_level2(modules, modules_count):
|
||||
lost_point = 0
|
||||
|
||||
modules_range = xrange(modules_count - 1)
|
||||
modules_range = range(modules_count - 1)
|
||||
for row in modules_range:
|
||||
this_row = modules[row]
|
||||
next_row = modules[row + 1]
|
||||
|
@ -262,8 +264,8 @@ def _lost_point_level3(modules, modules_count):
|
|||
# row/column, preceded or followed by light area 4 modules wide. From ISOIEC.
|
||||
# pattern1: 10111010000
|
||||
# pattern2: 00001011101
|
||||
modules_range = xrange(modules_count)
|
||||
modules_range_short = xrange(modules_count-10)
|
||||
modules_range = range(modules_count)
|
||||
modules_range_short = range(modules_count-10)
|
||||
lost_point = 0
|
||||
|
||||
for row in modules_range:
|
||||
|
@ -348,14 +350,14 @@ def optimal_data_chunks(data, minimum=4):
|
|||
:param minimum: The minimum number of bytes in a row to split as a chunk.
|
||||
"""
|
||||
data = to_bytestring(data)
|
||||
num_pattern = six.b(r'\d')
|
||||
alpha_pattern = six.b('[') + re.escape(ALPHA_NUM) + six.b(']')
|
||||
num_pattern = r'\d'.encode("latin-1")
|
||||
alpha_pattern = '[').encode("latin-1") + re.escape(ALPHA_NUM) + ']'.encode("latin-1")
|
||||
if len(data) <= minimum:
|
||||
num_pattern = re.compile(six.b('^') + num_pattern + six.b('+$'))
|
||||
alpha_pattern = re.compile(six.b('^') + alpha_pattern + six.b('+$'))
|
||||
num_pattern = re.compile('^'.encode("latin-1") + num_pattern + '+$'.encode("latin-1"))
|
||||
alpha_pattern = re.compile('^'.encode("latin-1") + alpha_pattern + '+$'.encode("latin-1"))
|
||||
else:
|
||||
re_repeat = (
|
||||
six.b('{') + six.text_type(minimum).encode('ascii') + six.b(',}'))
|
||||
'{'.encode("latin-1") + str(minimum).encode('ascii') + ',}'.encode("latin-1"))
|
||||
num_pattern = re.compile(num_pattern + re_repeat)
|
||||
alpha_pattern = re.compile(alpha_pattern + re_repeat)
|
||||
num_bits = _optimal_split(data, num_pattern)
|
||||
|
@ -390,8 +392,8 @@ def to_bytestring(data):
|
|||
Convert data to a (utf-8 encoded) byte-string if it isn't a byte-string
|
||||
already.
|
||||
"""
|
||||
if not isinstance(data, six.binary_type):
|
||||
data = six.text_type(data).encode('utf-8')
|
||||
if not isinstance(data, bytes):
|
||||
data = str(data).encode('utf-8')
|
||||
return data
|
||||
|
||||
|
||||
|
@ -439,12 +441,12 @@ class QRData(object):
|
|||
|
||||
def write(self, buffer):
|
||||
if self.mode == MODE_NUMBER:
|
||||
for i in xrange(0, len(self.data), 3):
|
||||
for i in range(0, len(self.data), 3):
|
||||
chars = self.data[i:i + 3]
|
||||
bit_length = NUMBER_LENGTH[len(chars)]
|
||||
buffer.put(int(chars), bit_length)
|
||||
elif self.mode == MODE_ALPHA_NUM:
|
||||
for i in xrange(0, len(self.data), 2):
|
||||
for i in range(0, len(self.data), 2):
|
||||
chars = self.data[i:i + 2]
|
||||
if len(chars) > 1:
|
||||
buffer.put(
|
||||
|
@ -453,12 +455,13 @@ class QRData(object):
|
|||
else:
|
||||
buffer.put(ALPHA_NUM.find(chars), 6)
|
||||
else:
|
||||
if six.PY3:
|
||||
# ~ if six.PY3:
|
||||
# Iterating a bytestring in Python 3 returns an integer,
|
||||
# no need to ord().
|
||||
data = self.data
|
||||
else:
|
||||
data = [ord(c) for c in self.data]
|
||||
# ~ data = self.data
|
||||
# ~ else:
|
||||
# ~ data = [ord(c) for c in self.data]
|
||||
data = self.data
|
||||
for c in data:
|
||||
buffer.put(c, 8)
|
||||
|
||||
|
|
Loading…
Reference in New Issue