Skip to main content

Python data serializer with type checking

Project description

DSerial

PyPI version

0. Introduction

DSerial is dictionary serializer with type checking methodology by declared type hinting

1. Requirements

python 3.7 +

2. Quick start

2.0. Installation

pip3 install dserial

2.1. Default type

{
  "int_variable": 1234,
  "string_variable": "string_value"
}

For serialize above dictionary, data class can be below code

class DefaultData(TypeCheckingDictSerializer):
    int_variable: int
    string_variable: str

and usage:

>>> dictionary = {
  'int_variable': 1234,
  'string_variable': 'string_value'
}
>>> data = DefaultData(dictionary)
>>> data.int_variable
1234
>>> data.string_variable
string_value

2.2. Nested type

{
  "int_variable": 1234,
  "string_variable": "string_value",
  "nested": {
    "int_variable": 4321
  }
}

For serialize above dictionary, data class can be below code

class NestedData(TypeCheckingDictSerializer):
    int_variable: int

class DefaultData(TypeCheckingDictSerializer):
    int_variable: int
    string_variable: str
    nested: NestedData

and usage:

>>> dictionary = {
    'int_variable': 1234,
    'string_variable': 'string_value',
    'nested': {
        'int_variable': 4321
    }
}
>>> data = DefaultData(dictionary)
>>> data.int_variable
1234
>>> data.string_variable
string_value
>>> data.nested.int_variable
4321

2.3. Repeated type

{
  "int_variable": 1234,
  "string_variable": "string_value",
  "nested": {
    "int_variable": 4321
  },
  "repeated": [1, 2, 3]
}

For serialize above dictionary, data class can be below code

class RepeatedData(TypeAnnotatedList):
    @staticmethod
    def get_type():
        return int

class NestedData(TypeCheckingDictSerializer):
    int_variable: int

class DefaultData(TypeCheckingDictSerializer):
    int_variable: int
    string_variable: str
    nested: NestedData
    repeated: RepeatedData

and usage:

>>> dictionary = {
    'int_variable': 1234,
    'string_variable': 'string_value',
    'nested': {
        'int_variable': 4321
    },
    'repeated': [1, 2, 3]
}
>>> data = DefaultData(dictionary)
>>> data.int_variable
1234
>>> data.string_variable
string_value
>>> data.nested.int_variable
4321
>>> len(data.repeated)
3
>>> data.repeated[0]
1

2.4. More complex

{
  "int_variable": 1234,
  "string_variable": "string_value",
  "repeated_nested": [{
    "int_variable": 3133
  }, {
    "int_variable": 1337
  }]
}

For serialize above dictionary, data class can be below code

class NestedData(TypeCheckingDictSerializer):
    int_variable: int

class RepeatedData(TypeAnnotatedList):
    @staticmethod
    def get_type():
        return NestedData

class DefaultData(TypeCheckingDictSerializer):
    int_variable: int
    string_variable: str
    repeated_nested: RepeatedData

and usage:

>>> dictionary = {
    'int_variable': 1234,
    'string_variable': 'string_value',
    'repeated_nested': [{
        'int_variable': 3133
    }, {
        'int_variable': 1337
    }]
}
>>> data = DefaultData(dictionary)
>>> data.int_variable
1234
>>> data.string_variable
string_value
>>> len(data.repeated_nested)
3
>>> data.repeated[0].int_variable
3133

3. Test

> dserial$ python3 -m tests

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

dserial-0.0.2.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file dserial-0.0.2.tar.gz.

File metadata

  • Download URL: dserial-0.0.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.25.0 CPython/3.7.3

File hashes

Hashes for dserial-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c1830d1e4af8984f97d55ee40221dd393b90ee7885a71f128345b6e2fcc76f1e
MD5 bca032f1da91d9aa3e16def09f0d212a
BLAKE2b-256 24fdc24a0ac52a8ca8df76c0fbf73badc943572b4726f3b099f71d952a65977a

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