@@ -112,7 +112,7 @@ def add_data(self, data):
112
112
datalen = sum ([len (x ) for x in self .data_list ])
113
113
if not self .type :
114
114
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 )
116
116
total_data_count = 0
117
117
for block in rs_blocks :
118
118
total_data_count += block ['data' ]
@@ -256,7 +256,7 @@ def map_data(self, data, mask_pattern):
256
256
@staticmethod
257
257
def create_data (qr_type , ecc , data_list ):
258
258
"""Check and format data into bit buffer"""
259
- rs_blocks = _getRSBlocks (qr_type , ecc )
259
+ rs_blocks = _get_rs_blocks (qr_type , ecc )
260
260
261
261
buffer = QRBitBuffer ()
262
262
@@ -320,22 +320,22 @@ def create_bytes(buffer, rs_blocks):
320
320
offset += dc_count
321
321
322
322
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 )
324
324
325
325
while True :
326
- if mod_poly .getLength () - rs_poly .getLength () < 0 :
326
+ if mod_poly .get_length () - rs_poly .get_length () < 0 :
327
327
break
328
328
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 ()):
331
331
num [i ] = mod_poly .get (i )
332
- for i in range (rs_poly .getLength ()):
332
+ for i in range (rs_poly .get_length ()):
333
333
num [i ] ^= _gexp (_glog (rs_poly .get (i )) + ratio )
334
334
mod_poly = QRPolynomial (num , 0 )
335
335
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 )]
337
337
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 ])
339
339
if mod_index >= 0 :
340
340
ecdata [r ][i ] = mod_poly .get (mod_index )
341
341
else :
@@ -364,7 +364,7 @@ def create_bytes(buffer, rs_blocks):
364
364
#pylint: enable=too-many-locals,too-many-branches
365
365
366
366
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 """
368
368
PATTERN_POSITION_TABLE = [b'' , b'\x06 \x12 ' , b'\x06 \x16 ' , b'\x06 \x1a ' ,
369
369
b'\x06 \x1e ' , b'\x06 "' , b'\x06 \x16 &' ,
370
370
b'\x06 \x18 *' , b'\x06 \x1a .' , b'\x06 \x1c 2' ]
@@ -375,31 +375,36 @@ class QRUtil(object):
375
375
#pylint: disable=invalid-name
376
376
@staticmethod
377
377
def get_BCH_type_info (data ):
378
+ """Encode with G15 BCH mask"""
378
379
d = data << 10
379
380
while QRUtil .get_BCH_digit (d ) - QRUtil .get_BCH_digit (QRUtil .G15 ) >= 0 :
380
381
d ^= QRUtil .G15 << (QRUtil .get_BCH_digit (d ) - QRUtil .get_BCH_digit (QRUtil .G15 ))
381
382
382
383
return ((data << 10 ) | d ) ^ QRUtil .G15_MASK
383
384
@staticmethod
384
385
def get_BCH_type_number (data ):
386
+ """Encode with G18 BCH mask"""
385
387
d = data << 12
386
388
while QRUtil .get_BCH_digit (d ) - QRUtil .get_BCH_digit (QRUtil .G18 ) >= 0 :
387
389
d ^= QRUtil .G18 << (QRUtil .get_BCH_digit (d ) - QRUtil .get_BCH_digit (QRUtil .G18 ))
388
390
return (data << 12 ) | d
389
- #pylint: enable=invalid-name
390
391
@staticmethod
391
392
def get_BCH_digit (data ):
393
+ """Count digits in data"""
392
394
digit = 0
393
395
while data != 0 :
394
396
digit += 1
395
397
data >>= 1
396
398
return digit
399
+ #pylint: enable=invalid-name
397
400
@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 ]
400
404
@staticmethod
401
405
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
403
408
if mask == 0 : return (i + j ) % 2 == 0
404
409
if mask == 1 : return i % 2 == 0
405
410
if mask == 2 : return j % 3 == 0
@@ -409,17 +414,20 @@ def get_mask(mask, i, j):
409
414
if mask == 6 : return ((i * j ) % 2 + (i * j ) % 3 ) % 2 == 0
410
415
if mask == 7 : return ((i * j ) % 3 + (i + j ) % 2 ) % 2 == 0
411
416
raise ValueError ("Bad mask pattern:" + mask )
412
- #pylint: enable=multiple-statements
417
+ #pylint: enable=multiple-statements, too-many-return-statements
413
418
@staticmethod
414
419
def get_error_correct_polynomial (ecc_length ):
415
- a = QRPolynomial ([1 ], 0 )
420
+ """ Generate a ecc polynomial"""
421
+ poly = QRPolynomial ([1 ], 0 )
416
422
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
419
425
420
426
class QRPolynomial :
427
+ """Structure for creating and manipulating error code polynomials"""
421
428
def __init__ (self , num , shift ):
422
- if len (num ) == 0 :
429
+ """Create a QR polynomial"""
430
+ if not num :
423
431
raise Exception (num .length + "/" + shift )
424
432
offset = 0
425
433
while offset < len (num ) and num [offset ] == 0 :
@@ -429,22 +437,25 @@ def __init__(self, num, shift):
429
437
self .num [i ] = num [i + offset ]
430
438
431
439
def get (self , index ):
440
+ """The exponent at the index location"""
432
441
return self .num [index ]
433
- def getLength (self ):
442
+ def get_length (self ):
443
+ """Length of the poly"""
434
444
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 )]
437
448
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 ()):
440
451
num [i + j ] ^= _gexp (_glog (self .get (i )) + _glog (e .get (j )))
441
452
442
453
return QRPolynomial (num , 0 )
443
454
444
455
_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'\x01 F,' , b'\x01 F7' , b'\x02 #\r ' , b'\x02 #\x11 ' , b'\x02 2 ' , b'\x01 dP' , b'\x04 \x19 \t ' , b'\x02 2\x18 ' , b'\x02 C+' , b'\x01 \x86 l' , b'\x02 !\x0b \x02 "\x0c ' , b'\x02 !\x0f \x02 "\x10 ' , b'\x04 +\x1b ' , b'\x02 VD' , b'\x04 +\x0f ' , b'\x04 +\x13 ' , b'\x04 1\x1f ' , b'\x02 bN' , b"\x04 '\r \x01 (\x0e " , b'\x02 \x0e \x04 !\x0f ' , b"\x02 <&\x02 ='" , b'\x02 ya' , b'\x04 (\x0e \x02 )\x0f ' , b'\x04 (\x12 \x02 )\x13 ' , b'\x03 :$\x02 ;%' , b'\x02 \x92 t' , b'\x04 $\x0c \x04 %\r ' , b'\x04 $\x10 \x04 %\x11 ' ) #pylint: disable=line-too-long
445
456
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 ]
448
459
449
460
length = len (rs_block ) // 3
450
461
blocks = []
0 commit comments