Skip to main content

A base class to get well-formatted serialized representations of child classes

Project description

serialclass

A base class to get serialized representations of any Python class

gavanaken

Install

pip install serialclass

Usage

from serialclass import SerialClass


class InnerClass(SerialClass):

    def __init__(self, id_key):
        self.id_key = id_key


class Outerclass(SerialClass):

    def __init__(self):
        self._attribute = 'attribute'
        self._list = ['a', 'list']
        self._dict = {1: InnerClass('a'), 2: InnerClass('b')}


outclass = Outerclass()
outclass.serialize()  # dict

>>>> {'Outerclass': {'_attribute': 'attribute', '_dict': {1: {'InnerClass': {'id_key': 'a'}}, 2: {'InnerClass': {'id_key': 'b'}}}, '_list': ['a', 'list']}}'
outclass.serialize(depth=1)  # do not recurse

>>>> {'Outerclass': {'_attribute': 'attribute', '_dict': {1: <__main__.InnerClass object at 0x000001DC95C800C8>, 2: <__main__.InnerClass object at 0x000001DC95C801C8>}, '_list': ['a', 'list']}}
outclass.stringify()  # json string

>>>> {"Outerclass": {"_attribute": "attribute", "_dict": {"1": {"InnerClass": {"id_key": "a"}}, "2": {"InnerClass": {"id_key": "b"}}}, "_list": ["a", "list"]}}
outclass.pstringify(indent=2)  # json string with indent (default=4)

>>>> {
    "Outerclass": {
        "_attribute": "attribute",
        "_dict": {
            "1": {
                "InnerClass": {
                    "id_key": "a"
                }
            },
            "2": {
                "InnerClass": {
                    "id_key": "b"
                }
            }
        },
        "_list": [
            "a",
            "list"
        ]
    }
}
outclass.pstringify(indent=2, ignore_protected=True))  # ignore _ - prefaced attributes
>>>> {
    "Outerclass": {}
}

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

serialclass-0.0.2.121.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

serialclass-0.0.2.121-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file serialclass-0.0.2.121.tar.gz.

File metadata

  • Download URL: serialclass-0.0.2.121.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for serialclass-0.0.2.121.tar.gz
Algorithm Hash digest
SHA256 5bba19eac5a270c4cfbe48c0087ad6b985cb153927223d7de3f95b94797be47d
MD5 4c85c543317bff220fd1f48bd4e0583b
BLAKE2b-256 11ee5b96f36f50ecb6c6d968fad5e55004849fe565e36b86493ab25793227f59

See more details on using hashes here.

File details

Details for the file serialclass-0.0.2.121-py3-none-any.whl.

File metadata

  • Download URL: serialclass-0.0.2.121-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for serialclass-0.0.2.121-py3-none-any.whl
Algorithm Hash digest
SHA256 d73aff45b01a382cdce4da338f65a809f49b8775f214446ddcd2da87eeded31d
MD5 68962b04009655702f6c4be98708e3f9
BLAKE2b-256 0f1c4fc7e47a5df54548cf009768334eee184ced986c1995e1c9380ae85cbbb7

See more details on using hashes here.

Supported by

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