diff --git a/direct/src/showbase/PythonUtil.py b/direct/src/showbase/PythonUtil.py index 98753a7622..7f8830fc85 100644 --- a/direct/src/showbase/PythonUtil.py +++ b/direct/src/showbase/PythonUtil.py @@ -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 = [] diff --git a/tests/showbase/test_PythonUtil.py b/tests/showbase/test_PythonUtil.py index 6b9d50cde0..df174ef76b 100644 --- a/tests/showbase/test_PythonUtil.py +++ b/tests/showbase/test_PythonUtil.py @@ -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'