Skip to main content

Automatic (de)serialization of arguments and return values

Project description

datafunctions

Build Status Coverage Status Supports Python versions 3.7+

Automatic (de)serialization of arguments and return values for Python functions.

pip install datafunctions

@datafunction is a decorator which automatically deserializes incoming arguments of the decorated function and serializes the return value. For example:

from datetime import datetime
from datafunctions import datafunction

@datafunction
def next_year(dt: datetime) -> datetime:
    return dt.replace(year=dt.year + 1)

assert next_year("2019-01-02T00:00:00") == "2020-01-02T00:00:00"

@datafunction automatically converts the string argument to a datetime object, and then converts the returned datetime back to a string.

This is useful for calling functions over a remote connection or from a different language - see instant_api and instant_client for example.

More generally, the arguments and return value as seen from the outside the function are basic JSON serializable objects - strings, dicts, etc. They are converted to and from the correct types (as indicated by type annotations) by marshmallow. Common Python types as well as dataclasses (which may be nested) are supported. For example:

from dataclasses import dataclass
from datafunctions import datafunction

@dataclass
class Point:
    x: int
    y: int

@datafunction
def translate(p: Point, dx: int, dy: int) -> Point:
    return Point(p.x + dx, p.y + dy)

assert translate({"x": 1, "y": 2}, 3, 4) == {"x": 4, "y": 6}

To decorate a method, pass is_method=True, e.g:

class MyClass:
    @datafunction(is_method=True)
    def method(self, x: int) -> int:
        ...

All parameters and the return value must have a type annotation, except for the first argument when is_method=True. Variadic parameters (e.g. *args or **kwargs) and positional-only parameters (before /) are not allowed.

If there is an exception deserializing or binding the arguments an ArgumentError will be raised with the underlying exception attached to __cause__. Similarly a ReturnError may be raised when trying to serialize the return value.

For more manual control, use the methods:

  • load_arguments
  • dump_arguments
  • load_result
  • dump_result

Under the hood, the type annotations are gathered into a dataclass which is then converted into a marshmallow schema using marshmallow_dataclass. marshmallow handles the (de)serialization.

Instances of this class have attributes params_schemas and return_schemas, each of which have the following attributes:

  • dataclass
  • schema_class: the marshmallow schema class
  • schema_instance: a no-args instance of schema_class

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

datafunctions-0.0.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file datafunctions-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: datafunctions-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.11.1 setuptools/36.2.7 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.1

File hashes

Hashes for datafunctions-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5320c8ce59e179a453744531d5b78a3276a054732d45ce7dd48adb202530e27e
MD5 ab9104fe73de6c62a277f0bdeb3a0f82
BLAKE2b-256 502245d882723fe25d29257005a29ddd27031125180c04ad11da8f955c71a6bb

See more details on using hashes here.

Supported by

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