Skip to main content

Mutations

pypi-version

Compose your business logic into commands that sanitize and validate input.

Install

$ pip install mutations

How it Works:

  1. Subclass mutations.Mutation
  2. Define your inputs.
  3. Define an execute method in your command.
  4. Run it, like this: SimpleMutation.run(foo='bar')

To learn more, see this blog post.

Example

import mutations

class UserSignup(mutations.Mutation):
    """Define the inputs to your mutation here. """
    email = mutations.fields.CharField(required=True)
    full_name = mutations.fields.CharField(required=True)
    send_welcome_email = mutations.fields.Boolean(required=False, default=True)

    def validate_email_address(self):
        """Custom validation for a field.

        If you encounter any validation errors and want to raise, you should
        raise mutation.ValidationError or some sublcass thereof. Otherwise, it
        assumes there were no problems.

        Any function beginning with `validate_` is assumed to be a validator
        function and will be run before the mutation can execute.
        """
        if not self.email.is_valid():
            raise mutations.ValidationError("email_not_valid", "Email is not valid.")

    def execute(self):
        """Executes the mutation.

        This method does the heavy lifting. You can call it by calling .run() on
        your mutation class.
        """
        user = User.objects.create(email=self.email, name=self.full_name)
        if self.send_welcome_email:
            EmailServer.deliver(recipient = self.email)
        return user

Calling Commands

>>> result = UserSignup.run(email=email, full_name="Bob Boblob")
>>> result.success
True
>>> result.return_value
<User id=...>
>>> result.errors

result = ...
>>> result = UserSignup.run(email=None)
>>> result.success
False
>>> result.errors
mutations.ErrorDict({
  'email': ['email_not_valid']
})
>>> result.value
None

Only Run Validations

>>> result = UserSignup.validate(email=email, full_name="Bob Boblob")
>>> result.is_valid
True

Testing

$ make tests

When you're ready to do a release, please make sure tests pass across both 2.7 and 3.6 by running tox:

$ tox

Versioning

This project uses Semantic Versioning.

Thanks

Thanks to Cypriss for the excellent Ruby Mutations Gem. I created this library because I was looking for something similar for Python.

Metadata

Release files for mutations 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mutations 0.4.0
File Size Uploaded Provenance
mutations-0.4.0.tar.gz 5.5 kB Without provenance Details

Release files / mutations-0.4.0.tar.gz

Download URL mutations-0.4.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
813d57b48adaeb643d0d8c66f4b0bb7f74a26452cdc4ceaca312daca92572a00
BLAKE2b-256 checksum
How to use checksums
e92cdb7144437418ccd7137cb5075f808e10df9068773265bbf6544dfc93e66c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

Security for mutations 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page