Ejemplo n.º 1
0
    def full_name(self,
                  gender: Optional[Gender] = None,
                  reverse: bool = False) -> str:
        """Generate a random full name.

        :param reverse: Return reversed full name.
        :param gender: Gender's enum object.
        :return: Full name.

        :Example:
            Johann Wolfgang.
        """
        if gender is None:
            gender = get_random_item(Gender, rnd=self.random)

        if gender and isinstance(gender, Gender):
            gender = gender
        else:
            raise NonEnumerableError(Gender)

        fmt = '{1} {0}' if reverse else '{0} {1}'
        return fmt.format(
            self.name(gender),
            self.surname(gender),
        )
Ejemplo n.º 2
0
    def port(self, port_range: PortRange = PortRange.ALL) -> int:
        """Generate random port.

        :param port_range: PortRange enum object.
        :return: Port number.
        :raises NonEnumerableError: if port_range is not in PortRange.

        :Example:
            8080
        """
        if isinstance(port_range, PortRange):
            return self.random.randint(*port_range.value)

        raise NonEnumerableError(PortRange)
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    def country_iso_code(self, fmt: Optional[CountryCode] = None) -> str:
        """Get a random ISO code of country.

        :param fmt: Enum object CountryCode.
        :return: ISO Code.
        :raises KeyError: if fmt is not supported.

        :Example:
            DE
        """
        if fmt is None:
            fmt = CountryCode.get_random_item()

        if fmt and fmt in CountryCode:
            codes = COUNTRIES_ISO[fmt.value]
            return self.random.choice(codes)
        else:
            raise NonEnumerableError(CountryCode)
Ejemplo n.º 5
0
    def credit_card_number(self, card_type: t.Optional[CardType] = None) -> str:
        """Generate a random credit card number.

        :param card_type: Issuing Network. Default is Visa.
        :return: Credit card number.
        :raises NotImplementedError: if card_type not supported.

        :Example:
            4455 5299 1152 2450
        """
        length = 16
        regex = re.compile(r"(\d{4})(\d{4})(\d{4})(\d{4})")

        if card_type is None:
            card_type = get_random_item(CardType, rnd=self.random)

        if card_type == CardType.VISA:
            number = self.random.randint(4000, 4999)
        elif card_type == CardType.MASTER_CARD:
            number = self.random.choice(
                [
                    self.random.randint(2221, 2720),
                    self.random.randint(5100, 5599),
                ]
            )
        elif card_type == CardType.AMERICAN_EXPRESS:
            number = self.random.choice([34, 37])
            length = 15
            regex = re.compile(r"(\d{4})(\d{6})(\d{5})")
        else:
            raise NonEnumerableError(CardType)

        str_num = str(number)
        while len(str_num) < length - 1:
            str_num += self.random.choice(string.digits)

        groups = regex.search(  # type: ignore
            str_num + luhn_checksum(str_num),
        ).groups()
        card = " ".join(groups)
        return card
Ejemplo n.º 6
0
    def full_name(self,
                  gender: t.Optional[Gender] = None,
                  reverse: bool = False) -> str:
        """Generate a random full name.

        :param reverse: Return reversed full name.
        :param gender: Gender's enum object.
        :return: Full name.

        :Example:
            Johann Wolfgang.
        """
        if gender is None:
            gender = get_random_item(Gender, rnd=self.random)

        if gender and isinstance(gender, Gender):
            gender = gender
        else:
            raise NonEnumerableError(Gender)

        name = self.name(gender)
        surname = self.surname(gender)
        return f"{surname} {name}" if reverse else f"{name} {surname}"