Skip to content

Commit 9149981

Browse files
committed
more linting
1 parent 1358697 commit 9149981

File tree

2 files changed

+64
-64
lines changed

2 files changed

+64
-64
lines changed

adafruit_miniqr.py

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,10 @@ def setup_position_probe_pattern(self, row, col):
150150
for c in range(-1, 8): #pylint: disable=invalid-name
151151
if (col + c <= -1 or self.module_count <= col + c):
152152
continue
153-
#pylint: disable=too-many-boolean-expressions
154-
if ((r >= 0 and r <= 6 and (c == 0 or c == 6))
153+
test = ((r >= 0 and r <= 6 and (c == 0 or c == 6))
155154
or (c >= 0 and c <= 6 and (r == 0 or r == 6))
156-
or (r >= 2 and r <= 4 and c >= 2 and c <= 4)):
157-
self.matrix[row+r, col+c] = True
158-
else:
159-
self.matrix[row+r, col+c] = False
160-
#pylint: enable=too-many-boolean-expressions
155+
or (r >= 2 and r <= 4 and c >= 2 and c <= 4))
156+
self.matrix[row+r, col+c] = test
161157
def setup_timing_pattern(self):
162158
"""Add the timing data pixels to the matrix"""
163159
for r in range(8, self.module_count-8):
@@ -180,11 +176,10 @@ def setup_position_adjust_pattern(self):
180176
continue
181177

182178
for r in range(-2, 3):
183-
for c in range(-2, 3):
184-
if abs(r) == 2 or abs(c) == 2 or (r == 0 and c == 0):
185-
self.matrix[row+r, col+c] = True
186-
else:
187-
self.matrix[row+r, col+c] = False
179+
for c in range(-2, 3): #pylint: disable=invalid-name
180+
test = (abs(r) == 2 or abs(c) == 2 or
181+
(r == 0 and c == 0))
182+
self.matrix[row+r, col+c] = test
188183

189184
def setup_type_number(self, test):
190185
"""Add the type number pixels to the matrix"""
@@ -227,52 +222,53 @@ def setup_type_info(self, test, mask_pattern):
227222
self.matrix[self.module_count - 8, 8] = (not test)
228223

229224
def map_data(self, data, mask_pattern):
225+
"""Map the data onto the QR code"""
230226
inc = -1
231227
row = self.module_count - 1
232-
bitIndex = 7
233-
byteIndex = 0
228+
bit_idx = 7
229+
byte_idx = 0
234230

235231
for col in range(self.module_count - 1, 0, -2):
236232
if col == 6:
237233
col -= 1
238234

239235
while True:
240-
for c in range(2):
241-
if (self.matrix[row, col - c] == None):
236+
for c in range(2): #pylint: disable=invalid-name
237+
if self.matrix[row, col - c] is None:
242238
dark = False
243-
if byteIndex < len(data):
244-
dark = ((data[byteIndex] >> bitIndex) & 1) == 1
239+
if byte_idx < len(data):
240+
dark = ((data[byte_idx] >> bit_idx) & 1) == 1
245241
mask = QRUtil.getMask(mask_pattern, row, col - c)
246242
if mask:
247243
dark = not dark
248244
self.matrix[row, col-c] = dark
249-
bitIndex -= 1
250-
if bitIndex == -1:
251-
byteIndex += 1
252-
bitIndex = 7
245+
bit_idx -= 1
246+
if bit_idx == -1:
247+
byte_idx += 1
248+
bit_idx = 7
253249
row += inc
254250
if row < 0 or self.module_count <= row:
255251
row -= inc
256252
inc = -inc
257253
break
258254

259255
@staticmethod
260-
def create_data(type, ECC, data_list):
261-
rs_blocks = _getRSBlocks(type, ECC)
256+
def create_data(qr_type, ecc, data_list):
257+
"""Check and format data into bit buffer"""
258+
rs_blocks = _getRSBlocks(qr_type, ecc)
262259

263260
buffer = QRBitBuffer()
264261

265-
for i in range(len(data_list)):
266-
data = data_list[i]
262+
for data in data_list:
267263
buffer.put(_MODE_8BIT_BYTE, 4)
268264
buffer.put(len(data), 8)
269-
for i in range(len(data)):
270-
buffer.put(data[i], 8)
265+
for byte in data:
266+
buffer.put(byte, 8)
271267

272268
#// calc num max data.
273269
total_data_count = 0
274-
for i in range(len(rs_blocks)):
275-
total_data_count += rs_blocks[i]['data']
270+
for block in rs_blocks:
271+
total_data_count += block['data']
276272

277273
if buffer.getLengthInBits() > total_data_count * 8:
278274
raise RuntimeError("Code length overflow: %d > %d" %
@@ -295,75 +291,79 @@ def create_data(type, ECC, data_list):
295291
break
296292
buffer.put(_PAD1, 8)
297293

298-
return QRCode.createBytes(buffer, rs_blocks)
294+
return QRCode.create_bytes(buffer, rs_blocks)
299295

296+
#pylint: disable=too-many-locals,too-many-branches
300297
@staticmethod
301-
def createBytes(buffer, rs_blocks):
298+
def create_bytes(buffer, rs_blocks):
299+
"""Perform error calculation math on bit buffer"""
302300
offset = 0
303-
maxDcCount = 0
304-
maxEcCount = 0
301+
max_dc_count = 0
302+
max_ec_count = 0
305303

306304
dcdata = [0] * len(rs_blocks)
307305
ecdata = [0] * len(rs_blocks)
308306

309-
for r in range(len(rs_blocks)):
307+
for r, block in enumerate(rs_blocks):
310308

311-
dcCount = rs_blocks[r]['data']
312-
ecCount = rs_blocks[r]['total'] - dcCount
309+
dc_count = block['data']
310+
ec_count = block['total'] - dc_count
313311

314-
maxDcCount = max(maxDcCount, dcCount)
315-
maxEcCount = max(maxEcCount, ecCount)
312+
max_dc_count = max(max_dc_count, dc_count)
313+
max_ec_count = max(max_ec_count, ec_count)
316314

317-
dcdata[r] = [0 for x in range(dcCount)]
315+
dcdata[r] = [0] * dc_count
318316

319317
for i in range(len(dcdata[r])):
320318
dcdata[r][i] = 0xff & buffer.buffer[i + offset]
321-
offset += dcCount
319+
offset += dc_count
322320

323-
rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount)
324-
modPoly = QRPolynomial(dcdata[r], rsPoly.getLength() - 1)
321+
rs_poly = QRUtil.getErrorCorrectPolynomial(ec_count)
322+
mod_poly = QRPolynomial(dcdata[r], rs_poly.getLength() - 1)
325323

326324
while True:
327-
if modPoly.getLength() - rsPoly.getLength() < 0:
325+
if mod_poly.getLength() - rs_poly.getLength() < 0:
328326
break
329-
ratio = _glog(modPoly.get(0)) - _glog(rsPoly.get(0))
330-
num = [0 for x in range(modPoly.getLength())]
331-
for i in range(modPoly.getLength()):
332-
num[i] = modPoly.get(i)
333-
for i in range(rsPoly.getLength()):
334-
num[i] ^= _gexp(_glog(rsPoly.get(i)) + ratio)
335-
modPoly = QRPolynomial(num, 0)
336-
337-
ecdata[r] = [0 for x in range(rsPoly.getLength()-1)]
327+
ratio = _glog(mod_poly.get(0)) - _glog(rs_poly.get(0))
328+
num = [0 for x in range(mod_poly.getLength())]
329+
for i in range(mod_poly.getLength()):
330+
num[i] = mod_poly.get(i)
331+
for i in range(rs_poly.getLength()):
332+
num[i] ^= _gexp(_glog(rs_poly.get(i)) + ratio)
333+
mod_poly = QRPolynomial(num, 0)
334+
335+
ecdata[r] = [0 for x in range(rs_poly.getLength()-1)]
338336
for i in range(len(ecdata[r])):
339-
modIndex = i + modPoly.getLength() - len(ecdata[r])
340-
if modIndex >= 0:
341-
ecdata[r][i] = modPoly.get(modIndex)
337+
mod_index = i + mod_poly.getLength() - len(ecdata[r])
338+
if mod_index >= 0:
339+
ecdata[r][i] = mod_poly.get(mod_index)
342340
else:
343341
ecdata[r][i] = 0
344342

345-
totalCodeCount = 0
346-
for i in range(len(rs_blocks)):
347-
totalCodeCount += rs_blocks[i]['total']
343+
total_code_count = 0
344+
for block in rs_blocks:
345+
total_code_count += block['total']
348346

349-
data = [None] * totalCodeCount
347+
data = [None] * total_code_count
350348
index = 0
351349

352-
for i in range(maxDcCount):
350+
for i in range(max_dc_count):
353351
for r in range(len(rs_blocks)):
354352
if i < len(dcdata[r]):
355353
data[index] = dcdata[r][i]
356354
index += 1
357355

358-
for i in range(maxEcCount):
356+
for i in range(max_ec_count):
359357
for r in range(len(rs_blocks)):
360358
if i < len(ecdata[r]):
361359
data[index] = ecdata[r][i]
362360
index += 1
363361

364362
return data
363+
#pylint: enable=too-many-locals,too-many-branches
365364

366365
class QRUtil(object):
366+
"""A selection of bit manipulation tools for QR generation"""
367367
PATTERN_POSITION_TABLE = [b'', b'\x06\x12', b'\x06\x16', b'\x06\x1a',
368368
b'\x06\x1e', b'\x06"', b'\x06\x16&',
369369
b'\x06\x18*', b'\x06\x1a.', b'\x06\x1c2']

examples/miniqr_simpletest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def print_QR(matrix):
2828
out.write(WHITE)
2929
print()
3030

31-
qr = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.H)
31+
qr = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L)
3232
qr.add_data(b'https://www.adafruit.com')
3333
qr.make()
3434

@@ -37,7 +37,7 @@ def print_QR(matrix):
3737
print(matrix_s)
3838
hashed = hashlib.md5(matrix_s.encode('utf-8')).hexdigest()
3939
print(hashed)
40-
if hashed != "7b260ec364d4938cc7b7a18af07cfc61":
40+
if hashed != "0b8bf742f2286bc360bf585076aa39ac":
4141
raise Exception("wrong hash")
4242

4343
print_QR(qr.matrix)

0 commit comments

Comments
 (0)