Created
March 7, 2026 13:15
-
-
Save mihalt/ad0dbd155292b3a2021612df3b9c33d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_public_methods_of(cls: Type): | |
| return [ | |
| name | |
| for name, attr in vars(cls).items() | |
| if isinstance(attr, FunctionType) and not name.startswith("_") | |
| ] | |
| def get_all_modules_from(package): | |
| return [module.name for module in pkgutil.iter_modules(package.__path__)] | |
| def get_parent_class_from(cls: Type) -> Type | None: | |
| return cls.__bases__[0] if cls.__bases__ else None | |
| def get_class_from_path(module_path: str, class_name: str): | |
| module = import_module(module_path) | |
| return getattr(module, class_name) | |
| def get_all_provider_methods(package, *exclude_modules, exclude_methods=()): | |
| parent_provider_methods = get_public_methods_of(package.Provider) | |
| parent_provider_methods = [ | |
| m for m in parent_provider_methods if m not in exclude_methods | |
| ] | |
| result = [] | |
| for module_name in get_all_modules_from(package): | |
| if module_name in exclude_modules: | |
| continue | |
| module_path = f"{package.__name__}.{module_name}" | |
| child_provider = get_class_from_path(module_path, "Provider") | |
| child_methods = set( | |
| get_public_methods_of(child_provider) + parent_provider_methods | |
| ) | |
| child_methods = [m for m in child_methods if m not in exclude_methods] | |
| all_methods = set(child_methods + parent_provider_methods) | |
| result.append((module_name, all_methods)) | |
| return result | |
| def get_all_provider_method_results(all_provider_methods_list): | |
| results = [] | |
| for locale, methods in all_provider_methods_list: | |
| faker = Faker(locale) | |
| for method_name in methods: | |
| method = getattr(faker, method_name) | |
| sig = inspect.signature(method) | |
| required_args = len( | |
| [ | |
| p | |
| for p in sig.parameters.values() | |
| if p.default == inspect.Parameter.empty | |
| and p.kind == p.POSITIONAL_OR_KEYWORD | |
| ] | |
| ) | |
| if required_args > 0: | |
| continue | |
| results.append((method_name, method())) | |
| return results | |
| from faker.providers import company | |
| COMPANY_SENSITIVE_DATA = get_all_provider_method_results( | |
| get_all_provider_methods(company, exclude_methods=LIST_OF_NON_SENSITIVE_METHODS) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment