Coder Social home page Coder Social logo

dserial's Introduction

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

dserial's People

Contributors

pjongy avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.