Tweaked Python code to allow static type checking, without changing behavior.

This commit is contained in:
Project Nayuki 2020-04-24 18:06:22 +00:00
parent 979de6d335
commit 49f9ea0890
2 changed files with 35 additions and 14 deletions

View File

@ -25,6 +25,7 @@
# #
import itertools, random, subprocess, sys, time import itertools, random, subprocess, sys, time
from typing import Optional, TypeVar
CHILD_PROGRAMS = [ CHILD_PROGRAMS = [
@ -56,7 +57,7 @@ def main():
for proc in subprocs: for proc in subprocs:
if proc.poll() is None: if proc.poll() is None:
print(-1, file=proc.stdin) print(-1, file=proc.stdin)
proc.stdin.flush() not_none(proc.stdin).flush()
sys.exit("Error: One or more workers failed to start") sys.exit("Error: One or more workers failed to start")
# Do tests # Do tests
@ -120,15 +121,22 @@ def write_all(val):
def flush_all(): def flush_all():
for proc in subprocs: for proc in subprocs:
proc.stdin.flush() not_none(proc.stdin).flush()
def read_verify(): def read_verify():
val = subprocs[0].stdout.readline().rstrip("\r\n") val = not_none(subprocs[0].stdout).readline().rstrip("\r\n")
for proc in subprocs[1 : ]: for proc in subprocs[1 : ]:
if proc.stdout.readline().rstrip("\r\n") != val: if not_none(proc.stdout).readline().rstrip("\r\n") != val:
raise ValueError("Mismatch") raise ValueError("Mismatch")
return int(val) return int(val)
T = TypeVar("T")
def not_none(obj: Optional[T]) -> T:
if obj is None:
raise TypeError()
return obj
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -650,19 +650,19 @@ class QrCode:
_ECC_CODEWORDS_PER_BLOCK = ( _ECC_CODEWORDS_PER_BLOCK = (
# Version: (note that index 0 is for padding, and is set to an illegal value) # Version: (note that index 0 is for padding, and is set to an illegal value)
# 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
(None, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), # Low (-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), # Low
(None, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28), # Medium (-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28), # Medium
(None, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), # Quartile (-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), # Quartile
(None, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30)) # High (-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30)) # High
_NUM_ERROR_CORRECTION_BLOCKS = ( _NUM_ERROR_CORRECTION_BLOCKS = (
# Version: (note that index 0 is for padding, and is set to an illegal value) # Version: (note that index 0 is for padding, and is set to an illegal value)
# 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
(None, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25), # Low (-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25), # Low
(None, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49), # Medium (-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49), # Medium
(None, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68), # Quartile (-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68), # Quartile
(None, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81)) # High (-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81)) # High
_MASK_PATTERNS = ( _MASK_PATTERNS = (
(lambda x, y: (x + y) % 2 ), (lambda x, y: (x + y) % 2 ),
@ -684,6 +684,12 @@ class QrCode:
def __init__(self, i, fb): def __init__(self, i, fb):
self.ordinal = i # (Public) In the range 0 to 3 (unsigned 2-bit integer) self.ordinal = i # (Public) In the range 0 to 3 (unsigned 2-bit integer)
self.formatbits = fb # (Package-private) In the range 0 to 3 (unsigned 2-bit integer) self.formatbits = fb # (Package-private) In the range 0 to 3 (unsigned 2-bit integer)
# Placeholders
LOW : QrCode.Ecc
MEDIUM : QrCode.Ecc
QUARTILE: QrCode.Ecc
HIGH : QrCode.Ecc
# Public constants. Create them outside the class. # Public constants. Create them outside the class.
Ecc.LOW = Ecc(0, 1) # The QR Code can tolerate about 7% erroneous codewords Ecc.LOW = Ecc(0, 1) # The QR Code can tolerate about 7% erroneous codewords
@ -882,6 +888,13 @@ class QrSegment:
"""Returns the bit width of the character count field for a segment in this mode """Returns the bit width of the character count field for a segment in this mode
in a QR Code at the given version number. The result is in the range [0, 16].""" in a QR Code at the given version number. The result is in the range [0, 16]."""
return self._charcounts[(ver + 7) // 17] return self._charcounts[(ver + 7) // 17]
# Placeholders
NUMERIC : QrSegment.Mode
ALPHANUMERIC: QrSegment.Mode
BYTE : QrSegment.Mode
KANJI : QrSegment.Mode
ECI : QrSegment.Mode
# Public constants. Create them outside the class. # Public constants. Create them outside the class.
Mode.NUMERIC = Mode(0x1, (10, 12, 14)) Mode.NUMERIC = Mode(0x1, (10, 12, 14))