Skip to main content

A decorator to serialize a string into a Pydantic model with error handling

Project description

Pydantic serializer

this is a simple decorator that can be used on functions that have a return type of str, To serialize it into a given Pydantic Model.

Usage:

For a function returning a string:

def func_returning_str() -> str:
    return '{"sample": "json"}'

Let's say there's a Pydantic model like so:

class SampleJson(BaseModel):
    sample: str

We can try and parse it with the decorator:

from pydantic_serializer import serialize

@serialize(return_type=SampleJson)
def func_returning_str() -> str:
    return '{"sample": "json"}'

The function will return a None and log the error using loguru if there is an error. If no error occurs, then you should get a return type of SampleJson in this case.

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

pydantic_serializer-0.1.0.tar.gz (1.5 kB view details)

Uploaded Source

Built Distribution

pydantic_serializer-0.1.0-py3-none-any.whl (2.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_serializer-0.1.0.tar.gz.

File metadata

  • Download URL: pydantic_serializer-0.1.0.tar.gz
  • Upload date:
  • Size: 1.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for pydantic_serializer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 222ecbce3148d10167da351713b706590fe49f6132b0b2bce7909ce0cd414ea0
MD5 e8828c8ccadd985cef09de6a57b8fb11
BLAKE2b-256 7233f4d25ac8be5705ffb7002ee6c3b46116963927d9961978bdb7d9efeda0c7

See more details on using hashes here.

File details

Details for the file pydantic_serializer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_serializer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40a3a7dbbd5dfc4391d843b5df29cd618e10c471fccf0e0cec4ba3db617656b4
MD5 475c0fe2729b6258b6868aee2c6c1644
BLAKE2b-256 6f86aba7ac34dfc33843d7870c365b4cb92539c54ec2fcd5b1007f9cb9a1f7a9

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