From 58de03afa38a475a8c054a65be8b2ca53c6a142e Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Mon, 22 Apr 2019 11:25:35 +0100 Subject: [PATCH] use set instead of frozenset return sorted list from exponents property join operates on strings (not the list) simplify __repr__ --- TLB/src/LFSR2.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/TLB/src/LFSR2.py b/TLB/src/LFSR2.py index 3adfc359..d52595f5 100644 --- a/TLB/src/LFSR2.py +++ b/TLB/src/LFSR2.py @@ -3,7 +3,7 @@ from nmigen import Signal, Module, Const -class LFSRPolynomial(frozenset): +class LFSRPolynomial(set): def __init__(self, exponents=()): max_exponent = 0 @@ -19,30 +19,28 @@ class LFSRPolynomial(frozenset): max_exponent = exponent if exponent != 0: yield exponent - frozenset.__init__(self, elements()) + set.__init__(self, elements()) self.max_exponent = max_exponent @property def exponents(self): - return self + exponents = list(self) # get elements of set + exponents.sort(reverse=True) + return exponents def __str__(self): - exponents = list(self) - exponents.sort(reverse=True) retval = [] - for i in exponents: + for i in self.exponents: if i == 0: retval.append("1") elif i == 1: retval.append("x") else: retval.append(f"x^{i}") - return retval.join(" + ") + return " + ".join(retval) def __repr__(self): - exponents = list(self) - exponents.sort(reverse=True) - return f"LFSRPolynomial({exponents!r})" + return "LFSRPolynomial(%s)" % self.exponents # list of selected polynomials from https://web.archive.org/web/20190418121923/https://en.wikipedia.org/wiki/Linear-feedback_shift_register#Some_polynomials_for_maximal_LFSRs # noqa -- 2.30.2