Generate realistic, context-aware mock data automatically.
Project description
PyMocker
this library works, but is in an experimental phase. Feedback is encouraged PyMocker is a powerful and flexible Python library designed to generate realistic, context-aware mock data automatically. Built on top of polyfactory, PyMocker extends its capabilities with intelligent field matching.
Example:
class Person(BaseModel):
FirstName:str= Field(max_length=8)
EmailAddress:str= Field(max_length=20)
CellPhoneNumber:str= Field(min_length=12,max_length=12)
# without mocker
class PersonFactory(ModelFactory[Person]):...
person = PersonFactory.build()
print(f"Polyfactory:")
pprint(person)
# with mocker
mocker=Mocker()
mocker.Config.confidence_threshold = .5
@mocker.mock()
class MockerPersonFactory(ModelFactory[Person]):...
mocker_person = MockerPersonFactory.build()
print("Polyfactory + Mocker:")
pprint(mocker_person)
Polyfactory:
Person(FirstName='48a40717', EmailAddress='1a5a1a37', CellPhoneNumber='6185d0d7c109')
Polyfactory + Mocker:
Person(FirstName='Ashley', EmailAddress='tbutler@example.net', CellPhoneNumber='429-860-3379')
Installation
PyPi package coming soon! In the meantime, install via git:
gh repo clone eschallack/PyMocker
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Intelligent Field Matching
PyMocker's internal ranking and similarity algorithms use a number of techniques to match fields to methods, including cosine similarity. You can adjust the confidence threshold for this behavior:
...
mocker=Mocker()
Mocker.confidence_threshold = 0.75 #.5 by default. higher means the model must be more confident to match
@mocker.mock()
...
Adding and customizing Providers
By default, PyMocker will use a Faker instance as its sole method provider. Configure your faker instance and add custom classes by adding it directly to Mocker's provider_instances.
...
class SuperHeroProvider:
@staticmethod
def super_hero_name():
return 'MockerMan'
class Hero(BaseModel):
HeroName:str=Field(max_length=9)
custom_faker_mocker=Mocker()
custom_faker_mocker.Config.provider_instances = [SuperHeroProvider(), Faker(locale='en_us')]
@custom_faker_mocker.mock()
...
Hero(HeroName='MockerMan')
Intelligent Field Matching
PyMocker uses a number of matching rules to match methods to fields, including cosine similarity. Configure this behavior like so:
#Control the Confidence threshold of similarity matching, .5 by default
mocker.confidence_threshold = 0.75
Note: Cosine Similarity is not perfect, and at times, may produce undesired results. You can disable this behavior entirely by setting match_field_generation_on_cosine_similarity to False
mocker.match_field_generation_on_cosine_similarity = False
# a confidence threshold of 0 also disables the behavior
mocker.confidence_threshold = 0
When disabled, PyMocker still uses word segmentation to discover matches for you. If no method is found, PyMocker defaults to PolyFactory's behavior
Other configurable attributes:
max_retries(int): The number of times a method will attempt to generate a constraint-fulfilling value. Higher values can impact performance. Defaults to300.coerce_on_fail(bool): IfTrue, attempts to coerce the value to match constraints if Faker generation fails. Defaults toTrue. When set toFalse, PyMocker will default to a PolyFactory generated value
Supported Model Types
PyMocker seamlessly integrates with all PolyFactory Factories, except for SQLAlchemy - there's currently an issue with pk/fk relationships, so your milage may vary
Contributing
I'm just one guy, so I'd love some help improving this library. This is very early stages, so any suggestions or changes are welcome.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pymocker-0.1.1.tar.gz.
File metadata
- Download URL: pymocker-0.1.1.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.13.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df95930c0d013942e59e5fd4f2f63cefd52a639b99a84d08010512267ccb98f7
|
|
| MD5 |
229c85d1f2d643a0a1926b5146cb06cf
|
|
| BLAKE2b-256 |
0d4eba9333bf552d96391542ad6b7219b07bd135a86a4652abb4a22d49b7c524
|
File details
Details for the file pymocker-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pymocker-0.1.1-py3-none-any.whl
- Upload date:
- Size: 17.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.13.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63f8c80cf1646e86b09040c466976e7d9bf3b586a14fac27b85da669bdd18f12
|
|
| MD5 |
7e140f6ee3144e2dd500fba5ff385ed8
|
|
| BLAKE2b-256 |
ab105fb85eab28b87964db8b77052ddfc6bd4a1e63ef47822595ab9e8771151a
|