remove typing, move to .pyi file (increases code clarity)
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 22 Apr 2019 09:58:28 +0000 (10:58 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 22 Apr 2019 09:58:28 +0000 (10:58 +0100)
TLB/src/LFSR2.py
TLB/src/LFSR2.pyi [new file with mode: 0644]
TLB/test/test_LFSR2.py

index 8cdb2cc560ca9a28e88d7e96e81213da2c44bf3d..91fb14e293a6881ed5d2f2bbbcb1c5f260bf4f5f 100644 (file)
@@ -1,17 +1,15 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # See Notices.txt for copyright information
-from nmigen import Signal, Module, Elaboratable, Const
+from nmigen import Signal, Module, Const
 from typing import Iterable, FrozenSet, Optional, Iterator, Any, Union
-from typing_extensions import final
 from collections.abc import Set, Hashable
 
 
-@final
 class LFSRPolynomial(Set):
-    def __init__(self, exponents: Iterable[int] = ()):
+    def __init__(self, exponents):
         max_exponent = 0
 
-        def elements() -> Iterable[int]:
+        def elements():
             nonlocal max_exponent
             yield 0  # 0 is always required
             for exponent in exponents:
@@ -27,26 +25,26 @@ class LFSRPolynomial(Set):
         self.__max_exponent = max_exponent
 
     @property
-    def exponents(self) -> FrozenSet[int]:
+    def exponents(self):
         return self.__exponents
 
     @property
-    def max_exponent(self) -> int:
+    def max_exponent(self):
         return self.__max_exponent
 
-    def __hash__(self) -> int:
+    def __hash__(self):
         return hash(self.exponents)
 
-    def __contains__(self, x: Any) -> bool:
+    def __contains__(self, x):
         return x in self.exponents
 
-    def __len__(self) -> int:
+    def __len__(self):
         return len(self.exponents)
 
-    def __iter__(self) -> Iterator[int]:
+    def __iter__(self):
         return iter(self.exponents)
 
-    def __str__(self) -> str:
+    def __str__(self):
         exponents = list(self.exponents)
         exponents.sort(reverse=True)
         retval = ""
@@ -62,7 +60,7 @@ class LFSRPolynomial(Set):
                 retval += f"x^{i}"
         return retval
 
-    def __repr__(self) -> str:
+    def __repr__(self):
         exponents = list(self.exponents)
         exponents.sort(reverse=True)
         return f"LFSRPolynomial({exponents!r})"
@@ -94,22 +92,21 @@ LFSR_POLY_23 = LFSRPolynomial([23, 18, 0])
 LFSR_POLY_24 = LFSRPolynomial([24, 23, 22, 17, 0])
 
 
-@final
-class LFSR(Elaboratable):
-    def __init__(self, polynomial: Union[Iterable[int], LFSRPolynomial]):
+class LFSR:
+    def __init__(self, polynomial):
         self.__polynomial = LFSRPolynomial(polynomial)
         self.state = Signal(self.width, reset=1)
         self.enable = Signal(1, reset=1)
 
     @property
-    def polynomial(self) -> LFSRPolynomial:
+    def polynomial(self):
         return self.__polynomial
 
     @property
-    def width(self) -> int:
+    def width(self):
         return self.polynomial.max_exponent
 
-    def elaborate(self, platform: Any) -> Module:
+    def elaborate(self, platform):
         m = Module()
         feedback: Value = Const(0)
         for exponent in self.polynomial:
@@ -121,3 +118,4 @@ class LFSR(Elaboratable):
                     self.state[0:self.width - 1])
                 m.d.sync += self.state[0].eq(feedback)
         return m
+
diff --git a/TLB/src/LFSR2.pyi b/TLB/src/LFSR2.pyi
new file mode 100644 (file)
index 0000000..44df5ae
--- /dev/null
@@ -0,0 +1,32 @@
+# 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_extensions import final
+from collections.abc import Set
+
+
+@final
+class LFSRPolynomial(Set):
+    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 __str__(self) -> str: ...
+    def __repr__(self) -> str: ...
+
+
+@final
+class LFSR:
+    def __init__(self, polynomial: Union[Iterable[int], LFSRPolynomial]): ...
+    @property
+    def polynomial(self) -> LFSRPolynomial: ...
+    @property
+    def width(self) -> int: ...
+    def elaborate(self, platform: Any) -> Module: ...
index f7eabc1944d16c45b5c87967ee8daefbcdd79e25..640f41ff3b7883f69e5133779c1b8d39bf72b4dc 100644 (file)
@@ -1,6 +1,10 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # See Notices.txt for copyright information
-from ..src.LFSR2 import LFSR, LFSRPolynomial, LFSR_POLY_3
+import sys
+sys.path.append("../src")
+sys.path.append("../../TestUtil")
+from LFSR2 import LFSR, LFSRPolynomial, LFSR_POLY_3
+
 from nmigen.back.pysim import Simulator, Delay, Tick
 import unittest