Skip to content

Commit

Permalink
Add digit-above-two type to numerify function (#1878)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlwrnc authored Jun 20, 2023
1 parent d193bba commit 44054ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
8 changes: 8 additions & 0 deletions faker/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

_re_hash = re.compile(r"#")
_re_perc = re.compile(r"%")
_re_dol = re.compile(r"\$")
_re_excl = re.compile(r"!")
_re_at = re.compile(r"@")
_re_qm = re.compile(r"\?")
Expand Down Expand Up @@ -325,6 +326,11 @@ def random_digit_not_null(self) -> int:

return self.generator.random.randint(1, 9)

def random_digit_above_two(self) -> int:
"""Generate a random digit above value two (2 to 9)."""

return self.generator.random.randint(2, 9)

def random_digit_or_empty(self) -> Union[int, str]:
"""Generate a random digit (0 to 9) or an empty string.
Expand Down Expand Up @@ -603,6 +609,7 @@ def numerify(self, text: str = "###") -> str:
- Number signs ('#') are replaced with a random digit (0 to 9).
- Percent signs ('%') are replaced with a random non-zero digit (1 to 9).
- Dollar signs ('$') are replaced with a random digit above two (2 to 9).
- Exclamation marks ('!') are replaced with a random digit or an empty string.
- At symbols ('@') are replaced with a random non-zero digit or an empty string.
Expand All @@ -617,6 +624,7 @@ def numerify(self, text: str = "###") -> str:
"""
text = _re_hash.sub(lambda x: str(self.random_digit()), text)
text = _re_perc.sub(lambda x: str(self.random_digit_not_null()), text)
text = _re_dol.sub(lambda x: str(self.random_digit_above_two()), text)
text = _re_excl.sub(lambda x: str(self.random_digit_or_empty()), text)
text = _re_at.sub(lambda x: str(self.random_digit_not_null_or_empty()), text)
return text
Expand Down
56 changes: 28 additions & 28 deletions faker/providers/phone_number/en_US/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,45 @@
class Provider(PhoneNumberProvider):
formats = (
# Standard 10-digit phone number formats
"##########",
"##########",
"###-###-####",
"###-###-####",
"$##$######",
"$##$######",
"$##-$##-####",
"$##-$##-####",
# Optional 10-digit local phone number format
"(###)###-####",
"(###)###-####",
"($##)$##-####",
"($##)$##-####",
# Non-standard 10-digit phone number format
"###.###.####",
"###.###.####",
"$##.$##.####",
"$##.$##.####",
# Standard 10-digit phone number format with extensions
"###-###-####x###",
"###-###-####x####",
"###-###-####x#####",
"$##-$##-####x###",
"$##-$##-####x####",
"$##-$##-####x#####",
# Optional 10-digit local phone number format with extensions
"(###)###-####x###",
"(###)###-####x####",
"(###)###-####x#####",
"($##)$##-####x###",
"($##)$##-####x####",
"($##)$##-####x#####",
# Non-standard 10-digit phone number format with extensions
"###.###.####x###",
"###.###.####x####",
"###.###.####x#####",
"$##.$##.####x###",
"$##.$##.####x####",
"$##.$##.####x#####",
# Standard 11-digit phone number format
"+1-###-###-####",
"001-###-###-####",
"+1-$##-$##-####",
"001-$##-$##-####",
# Standard 11-digit phone number format with extensions
"+1-###-###-####x###",
"+1-###-###-####x####",
"+1-###-###-####x#####",
"001-###-###-####x###",
"001-###-###-####x####",
"001-###-###-####x#####",
"+1-$##-$##-####x###",
"+1-$##-$##-####x####",
"+1-$##-$##-####x#####",
"001-$##-$##-####x###",
"001-$##-$##-####x####",
"001-$##-$##-####x#####",
)

basic_formats = (
# basic 10-digit phone number format with no extensions
"##########",
"###-###-####",
"(###)###-####",
"$##$######",
"$##-$##-####",
"($##)$##-####",
)

def basic_phone_number(self) -> str:
Expand Down
4 changes: 4 additions & 0 deletions tests/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def test_random_digit_not_null(self, faker, num_samples):
samples = [faker.random_digit_not_null() for _ in range(num_samples * 10)]
assert set(samples) == set(range(1, 10))

def test_random_digit_above_two(self, faker, num_samples):
samples = [faker.random_digit_above_two() for _ in range(num_samples * 10)]
assert set(samples) == set(range(2, 10))

def test_random_digit_or_empty(self, faker, num_samples):
expected = set(range(10))
expected.add("")
Expand Down

0 comments on commit 44054ec

Please sign in to comment.