From 2f76a43c23759d89ded157e705ae2284c7c6a1da Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Mon, 22 Apr 2019 11:07:01 +0100 Subject: [PATCH] instead of using abstract class Set, actually *derive* from frozenset (which was not possible to do in python2): can remove 25% of LFSR2.py in the process. also made max_exponent just a member of LFSR2 instead of being a property (no need for __max_exponent when max_exponent is what is needed and can be assigned then accessed directly) --- TLB/src/LFSR2.py | 32 +++++++------------------------- TLB/src/LFSR2.pyi | 13 +++---------- 2 files changed, 10 insertions(+), 35 deletions(-) diff --git a/TLB/src/LFSR2.py b/TLB/src/LFSR2.py index 91fb14e2..e4d4a5ac 100644 --- a/TLB/src/LFSR2.py +++ b/TLB/src/LFSR2.py @@ -1,12 +1,10 @@ # SPDX-License-Identifier: LGPL-2.1-or-later # See Notices.txt for copyright information from nmigen import Signal, Module, Const -from typing import Iterable, FrozenSet, Optional, Iterator, Any, Union -from collections.abc import Set, Hashable -class LFSRPolynomial(Set): - def __init__(self, exponents): +class LFSRPolynomial(frozenset): + def __init__(self, exponents=()): max_exponent = 0 def elements(): @@ -21,31 +19,15 @@ class LFSRPolynomial(Set): max_exponent = exponent if exponent != 0: yield exponent - self.__exponents = frozenset(elements()) - self.__max_exponent = max_exponent + frozenset.__init__(self, elements()) + self.max_exponent = max_exponent @property def exponents(self): - return self.__exponents - - @property - def max_exponent(self): - return self.__max_exponent - - def __hash__(self): - return hash(self.exponents) - - def __contains__(self, x): - return x in self.exponents - - def __len__(self): - return len(self.exponents) - - def __iter__(self): - return iter(self.exponents) + return self def __str__(self): - exponents = list(self.exponents) + exponents = list(self) exponents.sort(reverse=True) retval = "" separator = "" @@ -61,7 +43,7 @@ class LFSRPolynomial(Set): return retval def __repr__(self): - exponents = list(self.exponents) + exponents = list(self) exponents.sort(reverse=True) return f"LFSRPolynomial({exponents!r})" diff --git a/TLB/src/LFSR2.pyi b/TLB/src/LFSR2.pyi index 44df5aed..c37adc44 100644 --- a/TLB/src/LFSR2.pyi +++ b/TLB/src/LFSR2.pyi @@ -1,23 +1,16 @@ # SPDX-License-Identifier: LGPL-2.1-or-later # See Notices.txt for copyright information from nmigen import Module -from typing import Iterable, FrozenSet, Optional, Iterator, Any, Union +from typing import Iterable, Optional, Iterator, Any, Union from typing_extensions import final -from collections.abc import Set @final -class LFSRPolynomial(Set): +class LFSRPolynomial(frozenset): def __init__(self, exponents: Iterable[int] = ()): def elements() -> Iterable[int]: ... @property - def exponents(self) -> FrozenSet[int]: ... - @property - def max_exponent(self) -> int: ... - def __hash__(self) -> int: ... - def __contains__(self, x: Any) -> bool: ... - def __len__(self) -> int: ... - def __iter__(self) -> Iterator[int]: ... + def exponents(self) -> frozenset[int]: ... def __str__(self) -> str: ... def __repr__(self) -> str: ... -- 2.30.2