Skip to content

Commit

Permalink
Add weigthing to dynamic providers (#1894)
Browse files Browse the repository at this point in the history
* Add weigthing to dynamic providers

* Fixing C419 error for Flake8 in test_phone_number
  • Loading branch information
pauldechorgnat authored Aug 7, 2023
1 parent fcdb0e3 commit b05eec7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
8 changes: 6 additions & 2 deletions faker/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,12 @@ def add_element(self, element: str) -> None:
"""Add new element."""
self.elements.append(element)

def get_random_value(self) -> Any:
def get_random_value(self, use_weighting: bool = True) -> Any:
"""Returns a random value for this provider.
:param use_weighting: boolean option to use weighting. Defaults to True
"""
if not self.elements or len(self.elements) == 0:
raise ValueError("Elements should be a list of values the provider samples from")

return self.random_element(self.elements)
return self.random_elements(self.elements, length=1, use_weighting=use_weighting)[0]
50 changes: 50 additions & 0 deletions tests/providers/test_dynamic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import OrderedDict

import pytest

from faker import Faker
Expand Down Expand Up @@ -70,3 +72,51 @@ def test_dynamic_add_element(self):
provider.add_element("two")

assert faker.my_provider() in ("one", "two")

def test_weighted_dynamic_with_use_weighting(self):
elements = OrderedDict(
[
("A", 0.75),
("B", 0.25),
("C", 0.)
]
)
provider_name = "my_provider"
provider = DynamicProvider(
provider_name=provider_name,
elements=elements,
)
faker = Faker()
faker.add_provider(provider)

fake_data = [
faker.my_provider(use_weighting=True)
for _ in range(10_000)
]

for i in fake_data:
assert i in {"A", "B"}

def test_weighted_dynamic_without_use_weighting(self):
elements = OrderedDict(
[
("A", 0.75),
("B", 0.25),
("C", 0.)
]
)
provider_name = "my_provider"
provider = DynamicProvider(
provider_name=provider_name,
elements=elements,
)
faker = Faker()
faker.add_provider(provider)

fake_data = [
faker.my_provider(use_weighting=False)
for _ in range(10_000)
]

for i in fake_data:
assert i in {"A", "B", "C"}
17 changes: 15 additions & 2 deletions tests/providers/test_phone_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,14 @@ def test_phone_number(self, faker, num_samples):
]
for _ in range(num_samples):
phone_number = faker.phone_number()
assert any([re.match(pattern, phone_number) for pattern in patterns])

pattern_is_found = False

for pattern in patterns:
if re.match(pattern, phone_number):
pattern_is_found = True
break
assert pattern_is_found


class TestEnUs:
Expand All @@ -378,4 +385,10 @@ def test_basic_phone_number(self, faker, num_samples):
patterns = [pattern_no_whitespaces, pattern_dashes, pattern_parens]
for _ in range(num_samples):
phone_number = faker.basic_phone_number()
assert any([re.match(pattern, phone_number) for pattern in patterns])

pattern_is_found = False
for pattern in patterns:
if re.match(pattern, phone_number):
pattern_is_found = True
break
assert pattern_is_found

0 comments on commit b05eec7

Please sign in to comment.