Skip to content

Commit 866fe40

Browse files
committed
lent
1 parent 65aaebf commit 866fe40

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

adafruit_miniqr.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def add_data(self, data):
112112
datalen = sum([len(x) for x in self.data_list])
113113
if not self.type:
114114
for qr_type in range(1, 6):
115-
rs_blocks = _getRSBlocks(qr_type, self.ECC)
115+
rs_blocks = _get_rs_blocks(qr_type, self.ECC)
116116
total_data_count = 0
117117
for block in rs_blocks:
118118
total_data_count += block['data']
@@ -256,7 +256,7 @@ def map_data(self, data, mask_pattern):
256256
@staticmethod
257257
def create_data(qr_type, ecc, data_list):
258258
"""Check and format data into bit buffer"""
259-
rs_blocks = _getRSBlocks(qr_type, ecc)
259+
rs_blocks = _get_rs_blocks(qr_type, ecc)
260260

261261
buffer = QRBitBuffer()
262262

@@ -320,22 +320,22 @@ def create_bytes(buffer, rs_blocks):
320320
offset += dc_count
321321

322322
rs_poly = QRUtil.get_error_correct_polynomial(ec_count)
323-
mod_poly = QRPolynomial(dcdata[r], rs_poly.getLength() - 1)
323+
mod_poly = QRPolynomial(dcdata[r], rs_poly.get_length() - 1)
324324

325325
while True:
326-
if mod_poly.getLength() - rs_poly.getLength() < 0:
326+
if mod_poly.get_length() - rs_poly.get_length() < 0:
327327
break
328328
ratio = _glog(mod_poly.get(0)) - _glog(rs_poly.get(0))
329-
num = [0 for x in range(mod_poly.getLength())]
330-
for i in range(mod_poly.getLength()):
329+
num = [0 for x in range(mod_poly.get_length())]
330+
for i in range(mod_poly.get_length()):
331331
num[i] = mod_poly.get(i)
332-
for i in range(rs_poly.getLength()):
332+
for i in range(rs_poly.get_length()):
333333
num[i] ^= _gexp(_glog(rs_poly.get(i)) + ratio)
334334
mod_poly = QRPolynomial(num, 0)
335335

336-
ecdata[r] = [0 for x in range(rs_poly.getLength()-1)]
336+
ecdata[r] = [0 for x in range(rs_poly.get_length()-1)]
337337
for i in range(len(ecdata[r])):
338-
mod_index = i + mod_poly.getLength() - len(ecdata[r])
338+
mod_index = i + mod_poly.get_length() - len(ecdata[r])
339339
if mod_index >= 0:
340340
ecdata[r][i] = mod_poly.get(mod_index)
341341
else:
@@ -364,7 +364,7 @@ def create_bytes(buffer, rs_blocks):
364364
#pylint: enable=too-many-locals,too-many-branches
365365

366366
class QRUtil(object):
367-
"""A selection of bit manipulation tools for QR generation"""
367+
"""A selection of bit manipulation tools for QR generation and BCH encoding"""
368368
PATTERN_POSITION_TABLE = [b'', b'\x06\x12', b'\x06\x16', b'\x06\x1a',
369369
b'\x06\x1e', b'\x06"', b'\x06\x16&',
370370
b'\x06\x18*', b'\x06\x1a.', b'\x06\x1c2']
@@ -375,31 +375,36 @@ class QRUtil(object):
375375
#pylint: disable=invalid-name
376376
@staticmethod
377377
def get_BCH_type_info(data):
378+
"""Encode with G15 BCH mask"""
378379
d = data << 10
379380
while QRUtil.get_BCH_digit(d) - QRUtil.get_BCH_digit(QRUtil.G15) >= 0:
380381
d ^= QRUtil.G15 << (QRUtil.get_BCH_digit(d) - QRUtil.get_BCH_digit(QRUtil.G15))
381382

382383
return ((data << 10) | d) ^ QRUtil.G15_MASK
383384
@staticmethod
384385
def get_BCH_type_number(data):
386+
"""Encode with G18 BCH mask"""
385387
d = data << 12
386388
while QRUtil.get_BCH_digit(d) - QRUtil.get_BCH_digit(QRUtil.G18) >= 0:
387389
d ^= QRUtil.G18 << (QRUtil.get_BCH_digit(d) - QRUtil.get_BCH_digit(QRUtil.G18))
388390
return (data << 12) | d
389-
#pylint: enable=invalid-name
390391
@staticmethod
391392
def get_BCH_digit(data):
393+
"""Count digits in data"""
392394
digit = 0
393395
while data != 0:
394396
digit += 1
395397
data >>= 1
396398
return digit
399+
#pylint: enable=invalid-name
397400
@staticmethod
398-
def get_pattern_position(type):
399-
return QRUtil.PATTERN_POSITION_TABLE[type - 1]
401+
def get_pattern_position(qr_type):
402+
"""The mask pattern position array for this QR type"""
403+
return QRUtil.PATTERN_POSITION_TABLE[qr_type - 1]
400404
@staticmethod
401405
def get_mask(mask, i, j):
402-
#pylint: disable=multiple-statements
406+
"""Perform matching calculation on two vals for given pattern mask"""
407+
#pylint: disable=multiple-statements, too-many-return-statements
403408
if mask == 0: return (i + j) % 2 == 0
404409
if mask == 1: return i % 2 == 0
405410
if mask == 2: return j % 3 == 0
@@ -409,17 +414,20 @@ def get_mask(mask, i, j):
409414
if mask == 6: return ((i * j) % 2 + (i * j) % 3) % 2 == 0
410415
if mask == 7: return ((i * j) % 3 + (i + j) % 2) % 2 == 0
411416
raise ValueError("Bad mask pattern:" + mask)
412-
#pylint: enable=multiple-statements
417+
#pylint: enable=multiple-statements, too-many-return-statements
413418
@staticmethod
414419
def get_error_correct_polynomial(ecc_length):
415-
a = QRPolynomial([1], 0)
420+
""" Generate a ecc polynomial"""
421+
poly = QRPolynomial([1], 0)
416422
for i in range(ecc_length):
417-
a = a.multiply(QRPolynomial([1, _gexp(i)], 0))
418-
return a
423+
poly = poly.multiply(QRPolynomial([1, _gexp(i)], 0))
424+
return poly
419425

420426
class QRPolynomial:
427+
"""Structure for creating and manipulating error code polynomials"""
421428
def __init__(self, num, shift):
422-
if len(num) == 0:
429+
"""Create a QR polynomial"""
430+
if not num:
423431
raise Exception(num.length + "/" + shift)
424432
offset = 0
425433
while offset < len(num) and num[offset] == 0:
@@ -429,22 +437,25 @@ def __init__(self, num, shift):
429437
self.num[i] = num[i + offset]
430438

431439
def get(self, index):
440+
"""The exponent at the index location"""
432441
return self.num[index]
433-
def getLength(self):
442+
def get_length(self):
443+
"""Length of the poly"""
434444
return len(self.num)
435-
def multiply(self, e):
436-
num = [0 for x in range(self.getLength() + e.getLength() - 1)]
445+
def multiply(self, e): #pylint: disable=invalid-name
446+
"""Multiply two polynomials, returns a new one"""
447+
num = [0 for x in range(self.get_length() + e.get_length() - 1)]
437448

438-
for i in range(self.getLength()):
439-
for j in range(e.getLength()):
449+
for i in range(self.get_length()):
450+
for j in range(e.get_length()):
440451
num[i + j] ^= _gexp(_glog(self.get(i)) + _glog(e.get(j)))
441452

442453
return QRPolynomial(num, 0)
443454

444455
_QRRS_BLOCK_TABLE = (b'\x01\x1a\x10', b'\x01\x1a\x13', b'\x01\x1a\t', b'\x01\x1a\r', b'\x01,\x1c', b'\x01,"', b'\x01,\x10', b'\x01,\x16', b'\x01F,', b'\x01F7', b'\x02#\r', b'\x02#\x11', b'\x022 ', b'\x01dP', b'\x04\x19\t', b'\x022\x18', b'\x02C+', b'\x01\x86l', b'\x02!\x0b\x02"\x0c', b'\x02!\x0f\x02"\x10', b'\x04+\x1b', b'\x02VD', b'\x04+\x0f', b'\x04+\x13', b'\x041\x1f', b'\x02bN', b"\x04'\r\x01(\x0e", b'\x02 \x0e\x04!\x0f', b"\x02<&\x02='", b'\x02ya', b'\x04(\x0e\x02)\x0f', b'\x04(\x12\x02)\x13', b'\x03:$\x02;%', b'\x02\x92t', b'\x04$\x0c\x04%\r', b'\x04$\x10\x04%\x11') #pylint: disable=line-too-long
445456

446-
def _getRSBlocks(qr_type, ECC):
447-
rs_block = _QRRS_BLOCK_TABLE[(qr_type - 1) * 4 + ECC]
457+
def _get_rs_blocks(qr_type, ecc):
458+
rs_block = _QRRS_BLOCK_TABLE[(qr_type - 1) * 4 + ecc]
448459

449460
length = len(rs_block) // 3
450461
blocks = []

examples/miniqr_simpletest.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
import hashlib
32
import adafruit_miniqr
43

54
# For drawing filled rectangles to the console:
@@ -31,13 +30,4 @@ def print_QR(matrix):
3130
qr = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L)
3231
qr.add_data(b'https://www.adafruit.com')
3332
qr.make()
34-
35-
matrix = qr.matrix
36-
matrix_s = str(matrix)
37-
print(matrix_s)
38-
hashed = hashlib.md5(matrix_s.encode('utf-8')).hexdigest()
39-
print(hashed)
40-
if hashed != "0b8bf742f2286bc360bf585076aa39ac":
41-
raise Exception("wrong hash")
42-
4333
print_QR(qr.matrix)

0 commit comments

Comments
 (0)