Source code for eventsourcing.infrastructure.integersequencegenerators.base

from abc import abstractmethod
from threading import Lock


[docs]class AbstractIntegerSequenceGenerator(object): """ Abstract base class for generating a sequence of integers. """ def __iter__(self) -> "AbstractIntegerSequenceGenerator": return self
[docs] @abstractmethod def __next__(self) -> int: """ Returns the next item in the container. """
[docs]class SimpleIntegerSequenceGenerator(AbstractIntegerSequenceGenerator): """ Generates a sequence of integers, by simply incrementing a Python int. """
[docs] def __init__(self, i: int = 0): self.i = i self.lock = Lock()
[docs] def __next__(self) -> int: """ Returns the next item in the container. """ self.lock.acquire() i = self.i self.i += 1 self.lock.release() return i