Skip to content

Commit

Permalink
Some pythonic optimization and more typing work
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileyChris committed Mar 11, 2022
1 parent f63dc2b commit f2fee0a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
8 changes: 4 additions & 4 deletions qrcode/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import NamedTuple
from qrcode import constants

EXP_TABLE = list(range(256))
Expand Down Expand Up @@ -288,10 +289,9 @@ def __mod__(self, other):
return Polynomial(num, 0) % other


class RSBlock:
def __init__(self, total_count, data_count):
self.total_count = total_count
self.data_count = data_count
class RSBlock(NamedTuple):
total_count: int
data_count: int


def rs_blocks(version, error_correction):
Expand Down
4 changes: 2 additions & 2 deletions qrcode/image/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def get_default_eye_drawer(self) -> QRModuleDrawer:
def __init__(
self,
*args,
module_drawer: Union[QRModuleDrawer, str] = None,
eye_drawer: Union[QRModuleDrawer, str] = None,
module_drawer: Union[QRModuleDrawer, str, None] = None,
eye_drawer: Union[QRModuleDrawer, str, None] = None,
**kwargs,
):
self.module_drawer = self.get_drawer(module_drawer) or self.get_default_module_drawer()
Expand Down
55 changes: 26 additions & 29 deletions qrcode/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import math
import re
from typing import List

from qrcode import LUT, base, exceptions
from qrcode.base import RSBlock

# QR encoding modes.
MODE_NUMBER = 1 << 0
Expand Down Expand Up @@ -468,7 +470,7 @@ def __repr__(self):

class BitBuffer:
def __init__(self):
self.buffer = []
self.buffer: List[int] = []
self.length = 0

def __repr__(self):
Expand All @@ -494,27 +496,23 @@ def put_bit(self, bit):
self.length += 1


def create_bytes(buffer, rs_blocks):
def create_bytes(buffer: BitBuffer, rs_blocks: List[RSBlock]):
offset = 0

maxDcCount = 0
maxEcCount = 0

dcdata = [0] * len(rs_blocks)
ecdata = [0] * len(rs_blocks)
dcdata: List[List[int]] = []
ecdata: List[List[int]] = []

for r in range(len(rs_blocks)):

dcCount = rs_blocks[r].data_count
ecCount = rs_blocks[r].total_count - dcCount
for rs_block in rs_blocks:
dcCount = rs_block.data_count
ecCount = rs_block.total_count - dcCount

maxDcCount = max(maxDcCount, dcCount)
maxEcCount = max(maxEcCount, ecCount)

dcdata[r] = [0] * dcCount

for i in range(len(dcdata[r])):
dcdata[r][i] = 0xFF & buffer.buffer[i + offset]
current_dc = [0xFF & buffer.buffer[i + offset] for i in range(dcCount)]
offset += dcCount

# Get error correction polynomial.
Expand All @@ -525,28 +523,27 @@ def create_bytes(buffer, rs_blocks):
for i in range(ecCount):
rsPoly = rsPoly * base.Polynomial([1, base.gexp(i)], 0)

rawPoly = base.Polynomial(dcdata[r], len(rsPoly) - 1)
rawPoly = base.Polynomial(current_dc, len(rsPoly) - 1)

modPoly = rawPoly % rsPoly
ecdata[r] = [0] * (len(rsPoly) - 1)
for i in range(len(ecdata[r])):
modIndex = i + len(modPoly) - len(ecdata[r])
ecdata[r][i] = modPoly[modIndex] if (modIndex >= 0) else 0
totalCodeCount = sum(rs_block.total_count for rs_block in rs_blocks)
data = [None] * totalCodeCount
index = 0
current_ec = []
mod_offset = len(modPoly) - dcCount
for i in range(dcCount):
modIndex = i + mod_offset
current_ec.append(modPoly[modIndex] if (modIndex >= 0) else 0)

for i in range(maxDcCount):
for r in range(len(rs_blocks)):
if i < len(dcdata[r]):
data[index] = dcdata[r][i]
index += 1
dcdata.append(current_dc)
ecdata.append(current_ec)

data = []
for i in range(maxDcCount):
for dc in dcdata:
if i < len(dc):
data.append(dc[i])
for i in range(maxEcCount):
for r in range(len(rs_blocks)):
if i < len(ecdata[r]):
data[index] = ecdata[r][i]
index += 1
for ec in ecdata:
if i < len(ec):
data.append(ec[i])

return data

Expand Down

0 comments on commit f2fee0a

Please sign in to comment.