instead of using abstract class Set, actually *derive* from frozenset
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 22 Apr 2019 10:07:01 +0000 (11:07 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 22 Apr 2019 10:07:01 +0000 (11:07 +0100)
(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
TLB/src/LFSR2.pyi

index 91fb14e293a6881ed5d2f2bbbcb1c5f260bf4f5f..e4d4a5ac2455bdf9a0bbda521aeb01092a5dbc18 100644 (file)
@@ -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})"
 
index 44df5aedbf50906e35412ec6fd2beded690d35ba..c37adc44fa8891dbff24d9e8e802964a5abc53d3 100644 (file)
@@ -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: ...