mirror of
https://github.com/panda3d/panda3d.git
synced 2025-09-29 00:06:44 -04:00
PythonUtil: Fix Python 3 next() for SerialNumGen, AlphabetCounter
This commit is contained in:
parent
8f55d32cb1
commit
3732d3f1e6
@ -1245,18 +1245,24 @@ class SerialNumGen:
|
|||||||
if start is None:
|
if start is None:
|
||||||
start = 0
|
start = 0
|
||||||
self.__counter = start-1
|
self.__counter = start-1
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
self.__counter += 1
|
self.__counter += 1
|
||||||
return self.__counter
|
return self.__counter
|
||||||
|
|
||||||
|
__next__ = next
|
||||||
|
|
||||||
class SerialMaskedGen(SerialNumGen):
|
class SerialMaskedGen(SerialNumGen):
|
||||||
def __init__(self, mask, start=None):
|
def __init__(self, mask, start=None):
|
||||||
self._mask = mask
|
self._mask = mask
|
||||||
SerialNumGen.__init__(self, start)
|
SerialNumGen.__init__(self, start)
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
v = SerialNumGen.next(self)
|
v = SerialNumGen.next(self)
|
||||||
return v & self._mask
|
return v & self._mask
|
||||||
|
|
||||||
|
__next__ = next
|
||||||
|
|
||||||
_serialGen = SerialNumGen()
|
_serialGen = SerialNumGen()
|
||||||
def serialNum():
|
def serialNum():
|
||||||
global _serialGen
|
global _serialGen
|
||||||
@ -2517,6 +2523,7 @@ class AlphabetCounter:
|
|||||||
# object that produces 'A', 'B', 'C', ... 'AA', 'AB', etc.
|
# object that produces 'A', 'B', 'C', ... 'AA', 'AB', etc.
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._curCounter = ['A']
|
self._curCounter = ['A']
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
result = ''.join([c for c in self._curCounter])
|
result = ''.join([c for c in self._curCounter])
|
||||||
index = -1
|
index = -1
|
||||||
@ -2540,6 +2547,8 @@ class AlphabetCounter:
|
|||||||
break
|
break
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
__next__ = next
|
||||||
|
|
||||||
if __debug__ and __name__ == '__main__':
|
if __debug__ and __name__ == '__main__':
|
||||||
def testAlphabetCounter():
|
def testAlphabetCounter():
|
||||||
tempList = []
|
tempList = []
|
||||||
|
@ -157,3 +157,25 @@ def test_weighted_choice():
|
|||||||
# When subtracting that number by each weight, it will reach 0
|
# When subtracting that number by each weight, it will reach 0
|
||||||
# by the time it hits 'item6' in the iteration.
|
# by the time it hits 'item6' in the iteration.
|
||||||
assert item == items[5]
|
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'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user