1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-09-21 12:30:10 +02:00
python-broadlink/broadlink/helpers.py
Felipe Martins Diel 12fdf01631 Improve code quality (#572)
* Improve typing

* Use better names

* Clean up switch.py

* Remove unused import

* Run black

* Remove unnecessary comments

* Clean up climate.py
2021-04-26 17:57:02 -03:00

44 lines
1.3 KiB
Python

"""Helper functions and classes."""
import typing as t
class CRC16:
"""Helps with CRC-16 calculation.
CRC tables are cached for performance.
"""
_cache: t.Dict[int, t.List[int]] = {}
@classmethod
def get_table(cls, polynomial: int) -> t.List[int]:
"""Return the CRC-16 table for a polynomial."""
try:
crc_table = cls._cache[polynomial]
except KeyError:
crc_table = []
for dividend in range(0, 256):
remainder = dividend
for _ in range(0, 8):
if remainder & 1:
remainder = remainder >> 1 ^ polynomial
else:
remainder = remainder >> 1
crc_table.append(remainder)
cls._cache[polynomial] = crc_table
return crc_table
@classmethod
def calculate(
cls,
sequence: t.Sequence[int],
polynomial: int = 0xA001, # CRC-16-ANSI.
init_value: int = 0xFFFF,
) -> int:
"""Calculate the CRC-16 of a sequence of integers."""
crc_table = cls.get_table(polynomial)
crc = init_value
for item in sequence:
crc = crc >> 8 ^ crc_table[(crc ^ item) & 0xFF]
return crc