Skip to main content

Generate realistic, context-aware mock data automatically.

Project description

PyMocker

This library is experimental

PyMocker is a powerful Python library that extends Polyfactory to automatically generate realistic, context-aware mock data for your Python data models. Say goodbye to random strings! it's a drop-in solution for creating professional mockups and test data.

Installation

Install via pip:

pip install pymocker

Example:

Add the @mocker.mock() decorator to any Polyfactory class, and it will automatically generate realistic, contextual data.

...
class Person(BaseModel):
    FirstName:str= Field(max_length=8)
    EmailAddress:str= Field(max_length=20)
    CellPhoneNumber:str= Field(min_length=12,max_length=12)

# Polyfactory
class PersonFactory(ModelFactory[Person]):...

# Polyfactory + Mocker
mocker=Mocker()
@mocker.mock()
class MockerPersonFactory(ModelFactory[Person]):...
...
Polyfactory:
Person(FirstName='48a40717', EmailAddress='1a5a1a37', CellPhoneNumber='6185d0d7c109')
Polyfactory + Mocker:
Person(FirstName='Ashley', EmailAddress='tbutler@example.net', CellPhoneNumber='429-860-3379')

See examples for more!

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')] # order affects matches

@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 to 300.
  • coerce_on_fail (bool): If True, attempts to coerce the value to match constraints if Faker generation fails. Defaults to True. When set to False, 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

pymocker-0.1.5.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymocker-0.1.5-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file pymocker-0.1.5.tar.gz.

File metadata

  • Download URL: pymocker-0.1.5.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pymocker-0.1.5.tar.gz
Algorithm Hash digest
SHA256 ea13beb949246d42a66e50cb9a581593cf31caba7c6dccb2b47d6a562187cf31
MD5 5f1581424a5b6a70d863c805000c58c9
BLAKE2b-256 80d1b09e9f04eb9c90cc03d0d3c4ee89c0fdf96ea1bad5eadfa5dd4f35c4987d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymocker-0.1.5.tar.gz:

Publisher: release.yml on eschallack/PyMocker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pymocker-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pymocker-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pymocker-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a2b7d7f92e7612e0a7c290623d046b968b4163a6be636f2d449d4d355c472609
MD5 25b61e6c5e95e4f61b31940809d25111
BLAKE2b-256 fabebb94057e08e4945ea9b177854c56033af0093b31b35de8ba8e751b67db98

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymocker-0.1.5-py3-none-any.whl:

Publisher: release.yml on eschallack/PyMocker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page