PythonUtil: Fix Python 3 next() for SerialNumGen, AlphabetCounter

This commit is contained in:
rdb 2021-03-22 21:48:53 +01:00
parent 8f55d32cb1
commit 3732d3f1e6
2 changed files with 31 additions and 0 deletions

View File

@ -1245,18 +1245,24 @@ class SerialNumGen:
if start is None:
start = 0
self.__counter = start-1
def next(self):
self.__counter += 1
return self.__counter
__next__ = next
class SerialMaskedGen(SerialNumGen):
def __init__(self, mask, start=None):
self._mask = mask
SerialNumGen.__init__(self, start)
def next(self):
v = SerialNumGen.next(self)
return v & self._mask
__next__ = next
_serialGen = SerialNumGen()
def serialNum():
global _serialGen
@ -2517,6 +2523,7 @@ class AlphabetCounter:
# object that produces 'A', 'B', 'C', ... 'AA', 'AB', etc.
def __init__(self):
self._curCounter = ['A']
def next(self):
result = ''.join([c for c in self._curCounter])
index = -1
@ -2540,6 +2547,8 @@ class AlphabetCounter:
break
return result
__next__ = next
if __debug__ and __name__ == '__main__':
def testAlphabetCounter():
tempList = []

View File

@ -157,3 +157,25 @@ def test_weighted_choice():
# When subtracting that number by each weight, it will reach 0
# by the time it hits 'item6' in the iteration.
assert item == items[5]
def test_serial():
gen = PythonUtil.SerialNumGen()
assert gen.next() == 0
assert next(gen) == 1
assert next(gen) == 2
assert gen.next() == 3
def test_alphabet_counter():
counter = PythonUtil.AlphabetCounter()
assert next(counter) == 'A'
assert counter.next() == 'B'
assert counter.next() == 'C'
assert next(counter) == 'D'
for i in range(26 - 4):
next(counter)
assert next(counter) == 'AA'
assert next(counter) == 'AB'