Exemplo n.º 1
0
class BaseProvider(ValidateEnumMixin, StrMixin):
    """
    This is a base class for all data providers.
    """
    def __init__(self,
                 locale: Optional[str] = None,
                 seed: Optional[int] = None) -> None:
        """Base constructor for all providers.

        :param str locale: Current locale. Default is 'en'.
        :param int seed: Seed to all the random functions. Default is 'None'.
        """
        self.seed = seed
        self.random = Random()
        self.locale = setup_locale(locale)

        if seed is not None:
            self.random.seed(self.seed)

    def get_current_locale(self) -> str:
        """Current locale of provider.

        ..Note: Default for all providers is locale ``en``.
        """
        return self.locale
Exemplo n.º 2
0
class BaseProvider(object):
    """This is a base class for all providers."""

    def __init__(self, locale=None, seed=None):
        """Base constructor for all providers.

        :param locale: Current locale. Default is 'en'.
        :param seed: Seed to all the random functions. Default is 'None'.
        """
        if not locale:
            self.locale = DEFAULT_LOCALE
        else:
            self.locale = locale

        self.seed = seed
        self.random = Random()

        if seed is not None:
            self.random.seed(self.seed)

    def __str__(self):
        return '{}:{}:{}'.format(
            self.__class__.__name__,
            self.locale,
            locale_info(self.locale),
        )
Exemplo n.º 3
0
class BaseProvider(object):
    """This is a base class for all providers."""
    def __init__(self, seed: Optional[Seed] = None) -> None:
        """Initialize attributes.

        :param seed: Seed for random.
        """
        self.seed = seed
        self.random = Random()

        if seed is not None:
            self.random.seed(self.seed)

    def _validate_enum(self, item: Any, enum: Any) -> Any:
        """Validate enum parameter of method in subclasses of BaseProvider.

        :param item: Item of enum object.
        :param enum: Enum object.
        :return: Value of item.
        :raises NonEnumerableError: if ``item`` not in ``enum``.
        """
        if item is None:
            result = get_random_item(enum, self.random)
        elif item and isinstance(item, enum):
            result = item
        else:
            raise NonEnumerableError(enum)

        return result.value
Exemplo n.º 4
0
    def __init__(self, seed: Optional[Seed] = None) -> None:
        """Initialize attributes.

        :param seed: Seed for random.
        """
        self.seed = seed
        self.random = Random()

        if seed is not None:
            self.random.seed(self.seed)
Exemplo n.º 5
0
    def __init__(self, locale: str = '',
                 seed: Optional[int] = None) -> None:
        """Base constructor for all providers.

        :param str locale: Current locale. Default is 'en'.
        :param int seed: Seed to all the random functions. Default is 'None'.
        """
        self.seed = seed
        self.random = Random()
        self.locale = setup_locale(locale)

        if seed is not None:
            self.random.seed(self.seed)
Exemplo n.º 6
0
class BaseProvider(object):
    """This is a base class for all providers."""
    def __init__(self, seed: Optional[Seed] = None) -> None:
        """Initialize attributes.

        :param seed: Seed for random.
            When set to `None` the current system time is used.
        """
        self.seed = seed
        self.random = random

        if seed is not None:
            self.reseed(seed)

    def reseed(self, seed: Optional[Seed] = None) -> None:
        """Reseed the internal random generator.

        In case we use the default seed, we need to create a per instance
        random generator, in this case two providers with the same seed
        will always return the same values.

        :param seed: Seed for random.
            When set to `None` the current system time is used.
        """
        if self.random is random:
            self.random = Random()

        self.seed = seed
        self.random.seed(self.seed)

    def _validate_enum(self, item: Any, enum: Any) -> Any:
        """Validate enum parameter of method in subclasses of BaseProvider.

        :param item: Item of enum object.
        :param enum: Enum object.
        :return: Value of item.
        :raises NonEnumerableError: if ``item`` not in ``enum``.
        """
        if item is None:
            result = get_random_item(enum, self.random)
        elif item and isinstance(item, enum):
            result = item
        else:
            raise NonEnumerableError(enum)

        return result.value

    def __str__(self) -> str:
        """Human-readable representation of locale."""
        return self.__class__.__name__
Exemplo n.º 7
0
    def reseed(self, seed: Optional[Seed] = None) -> None:
        """Reseed the internal random generator.

        In case we use the default seed, we need to create a per instance
        random generator, in this case two providers with the same seed
        will always return the same values.

        :param seed: Seed for random.
            When set to `None` the current system time is used.
        """
        if self.random is random:
            self.random = Random()

        self.seed = seed
        self.random.seed(self.seed)
Exemplo n.º 8
0
    def __init__(self, locale=None, seed=None):
        """Base constructor for all providers.

        :param locale: Current locale. Default is 'en'.
        :param seed: Seed to all the random functions. Default is 'None'.
        """
        if not locale:
            self.locale = DEFAULT_LOCALE
        else:
            self.locale = locale

        self.seed = seed
        self.random = Random()

        if seed is not None:
            self.random.seed(self.seed)
Exemplo n.º 9
0
class BaseProvider(object):
    """This is a base class for all providers."""

    def __init__(self, locale: str = '',
                 seed: Optional[int] = None) -> None:
        """Base constructor for all providers.

        :param str locale: Current locale. Default is 'en'.
        :param int seed: Seed to all the random functions. Default is 'None'.
        """
        self.seed = seed
        self.random = Random()
        self.locale = setup_locale(locale)

        if seed is not None:
            self.random.seed(self.seed)

    def __str__(self) -> str:
        return '{}:{}:{}'.format(
            self.__class__.__name__,
            self.locale,
            locale_info(self.locale),
        )
Exemplo n.º 10
0
def random():
    return Random()