Skip to content

Commit 88b50fb

Browse files
committed
add: implement a new function to update the global faker object
1 parent 0276fea commit 88b50fb

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

src/copia/generators/_core.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
import faker
1+
from faker import Faker
2+
from faker.config import AVAILABLE_LOCALES
23

3-
4-
_fake = faker.Faker()
4+
_fake = Faker()
55
"""global faker instance for all generators based on faker"""
66

7-
def get_faker() -> faker.Faker:
7+
def update_global_faker(locale: str = "en_US", use_weighting: bool = True) -> None:
8+
"""replace the global faker object with a new on
9+
10+
Args:
11+
locale (str, optional): the locale to use for the next generations. Defaults to "en_US".
12+
use_weighting (bool, optional): if faker should weight the values to generate.
13+
this will improve the speed of generation, but the datasets will be completly random.
14+
See https://faker.readthedocs.io/en/master/#optimizations.
15+
Defaults to True.
16+
17+
Raises:
18+
ValueError: if the locale isn't supported by faker
19+
"""
20+
if locale not in AVAILABLE_LOCALES:
21+
raise ValueError(f"{locale!r} is not in fakers availables locals")
22+
global _fake
23+
_fake = Faker(locale=locale, use_weighting=use_weighting)
24+
25+
def get_faker() -> Faker:
826
"""return the global faker object"""
927
return _fake
10-
11-
# TODO: implement a set_locale function to update the global faker locale dynamically

0 commit comments

Comments
 (0)