Added more type annotations to Python tester programs.

pull/118/head
Project Nayuki 3 years ago
parent 27dd722961
commit aa32fe1235

@ -68,7 +68,9 @@ def main() -> None:
def do_trial() -> None: def do_trial() -> None:
mode = random.randrange(4) mode: int = random.randrange(4)
length: int
data: List[int]
if mode == 0: # Numeric if mode == 0: # Numeric
length = round((2 * 7089) ** random.random()) length = round((2 * 7089) ** random.random())
data = random.choices(b"0123456789", k=length) data = random.choices(b"0123456789", k=length)
@ -88,12 +90,12 @@ def do_trial() -> None:
for b in data: for b in data:
write_all(b) write_all(b)
errcorlvl = random.randrange(4) errcorlvl: int = random.randrange(4)
minversion = random.randint(1, 40) minversion: int = random.randint(1, 40)
maxversion = random.randint(1, 40) maxversion: int = random.randint(1, 40)
if minversion > maxversion: if minversion > maxversion:
minversion, maxversion = maxversion, minversion minversion, maxversion = maxversion, minversion
mask = -1 mask: int = -1
if random.random() < 0.5: if random.random() < 0.5:
mask = random.randrange(8) mask = random.randrange(8)
boostecl = int(random.random() < 0.2) boostecl = int(random.random() < 0.2)
@ -106,11 +108,11 @@ def do_trial() -> None:
write_all(boostecl) write_all(boostecl)
flush_all() flush_all()
version = read_verify() version: int = read_verify()
print(" version={}".format(version), end="") print(" version={}".format(version), end="")
if version == -1: if version == -1:
return return
size = version * 4 + 17 size: int = version * 4 + 17
for _ in range(size**2): for _ in range(size**2):
read_verify() read_verify()
@ -124,7 +126,7 @@ def flush_all() -> None:
not_none(proc.stdin).flush() not_none(proc.stdin).flush()
def read_verify() -> int: def read_verify() -> int:
val = not_none(subprocs[0].stdout).readline().rstrip("\r\n") val: str = not_none(subprocs[0].stdout).readline().rstrip("\r\n")
for proc in subprocs[1 : ]: for proc in subprocs[1 : ]:
if not_none(proc.stdout).readline().rstrip("\r\n") != val: if not_none(proc.stdout).readline().rstrip("\r\n") != val:
raise ValueError("Mismatch") raise ValueError("Mismatch")

@ -27,6 +27,7 @@
# #
import sys import sys
from typing import List, Sequence
import qrcodegen import qrcodegen
@ -38,26 +39,26 @@ def main() -> None:
while True: while True:
# Read data or exit # Read data or exit
length = read_int() length: int = read_int()
if length == -1: if length == -1:
break break
data = bytearray(read_int() for _ in range(length)) data = bytearray(read_int() for _ in range(length))
# Read encoding parameters # Read encoding parameters
errcorlvl = read_int() errcorlvl : int = read_int()
minversion = read_int() minversion: int = read_int()
maxversion = read_int() maxversion: int = read_int()
mask = read_int() mask : int = read_int()
boostecl = read_int() boostecl : int = read_int()
# Make segments for encoding # Make segments for encoding
if all((b < 128) for b in data): # Is ASCII if all((b < 128) for b in data): # Is ASCII
segs = qrcodegen.QrSegment.make_segments(data.decode("ASCII")) segs: List[qrcodegen.QrSegment] = qrcodegen.QrSegment.make_segments(data.decode("ASCII"))
else: else:
segs = [qrcodegen.QrSegment.make_bytes(data)] segs = [qrcodegen.QrSegment.make_bytes(data)]
try: # Try to make QR Code symbol try: # Try to make QR Code symbol
qr = qrcodegen.QrCode.encode_segments(segs, ECC_LEVELS[errcorlvl], minversion, maxversion, mask, boostecl != 0) qr: qrcodegen.QrCode = qrcodegen.QrCode.encode_segments(segs, ECC_LEVELS[errcorlvl], minversion, maxversion, mask, boostecl != 0)
# Print grid of modules # Print grid of modules
print(qr.get_version()) print(qr.get_version())
for y in range(qr.get_size()): for y in range(qr.get_size()):
@ -69,7 +70,7 @@ def main() -> None:
sys.stdout.flush() sys.stdout.flush()
ECC_LEVELS = ( ECC_LEVELS: Sequence[qrcodegen.QrCode.Ecc] = (
qrcodegen.QrCode.Ecc.LOW, qrcodegen.QrCode.Ecc.LOW,
qrcodegen.QrCode.Ecc.MEDIUM, qrcodegen.QrCode.Ecc.MEDIUM,
qrcodegen.QrCode.Ecc.QUARTILE, qrcodegen.QrCode.Ecc.QUARTILE,

Loading…
Cancel
Save