-
Notifications
You must be signed in to change notification settings - Fork 288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom providers to Faker #308
Comments
Hi @akarpf thanks for opening this issue. It looks like this could be achieved by writing your faker provider in a module and registering it globally. For example, I can create a provider inside a
Such that I should be able to import and run it from python: In [1]: from my_providers.dummy import Provider
...: from faker import Faker
...: faker = Faker()
...: faker.add_provider(Provider)
...: faker.dummy()
Out[1]: 'foo' Then, when I want to use it within SDV I just need to register the provider globally by appending its full module path to the from faker.config import PROVIDERS
PROVIDERS.append('my_providers.dummy') And afterwards use SDV normally just declaring |
Let's also keep this issue open to add support for custom providers directly within SDV without having to follow the workaround explained above. I think that it would be interesting to add the option to pass provider functions or full provider paths as the anonymization settings, so SDV would take care of adding the provider to the Faker instance on the fly without having to configure this outside of SDV beforehand. |
Hi @csala! Thank you very much! Your solution works perfectly and is also very convenient and clear. I can define multiple providers as methods inside that class and then call them from within SDV. In my opinion, it doesn't need any further integration. It would just be great if you could add this example to the documentation. While exploring your solution I also found out how I can change the locale in a similar fashion: import faker
#change locale
faker.proxy.DEFAULT_LOCALE = 'de_AT'
#add custom provider
faker.config.PROVIDERS.append('custom_provider.random_number') Although it would be great, if I couldn't only change the DEFAULT_LOCALE but add a list of potential locales as it's possible with |
Since I also stumbled upon the problem of needing multiple locales for an anonymized field, I would like to make a suggestion for the new feature and could also contribute a PR to it. Suggestion:Would it be possible to introduce a new metadata field like In the This should then result in faked values being generated from the specified Since the The |
* Adds multi localisation to pii marked fields * Refectors multi locale faker usage - pull Faker creation outside of loop - can be reused inside - get faker fn by category from Faker created outside of loop * - Mocks faker method - safer testing as not relying on random.choice for localisation selection - Renames test methods - Uses anonymization function directly not calling fit * Renames constant for n values created * adds docstring for _get_faker function * Adds docstring to test methods * creates test for passing arguments to the function creating fake values * adds documentation for localized data anonymization * adds version specific test logic - prior to faker version 3.0.0 only single localization can be specified - after 3.0.0 multiple localizations are possible * adds warning to documentation for faker versions prior to 3.0.0 - when using older versions only one localization can be specified * adds tests for different versions of Faker - skipping multiple localization if Faker version is too low * - renames get_faker_fn to get_faker_method - adds new function to generate fake values * removes return value of _make_anonymization_mappings - use the private attribute _ANONYMIZATION_MAPPINGS for tests * change minimal faker version to 3.0.0 - remove skipping test when lower version is used * rename test name and docstrings * add tests for _get_faker * sorts imports * incorporates review: - moves getting concrete faker attribute to closure - moves functions in order to always define before using * asserts isinstance faker to get_faker tests * removes faker import - only import Faker object from the library * removes metadata variable - when only used once use static method directly in Run section * moves mock fn outside of test setup * fix typo strig-> string * change double quotes to single quotes * remove specific version warning from doc - version is not supported anymore * add blank line after double intendation * make faker methods static - _get_faker - _get_faker_method - _get_fake_values - add doc string
Problem Description
I created a custom Faker provider which I would like add to the Faker instance called by SDV.
Expected behavior
The faker methods defined in the custom provider are callable via "anonymize_fields".
Additional context
I asked the question on Slack and csala suggested me to add my provider to Faker globally right before going into the SDV model. I tried to do so:
I however got the following error message when I tried to fit the model:
(If such a workaround is possible, I would be very grateful, if somebody could indicate me how!)
The text was updated successfully, but these errors were encountered: