Skip to main content

to_dict

Project description

py_todict

Usage

from enum import Enum
import json

from to_dict import Dictable

class E(Enum):
    test = 'test_val'

class D:
    def __init__(self):
        self.val = 'str_val'

class C:
    def __init__(self):
        self.d1 = D()
        self.d2 = D()

class B(Dictable):
    def __init__(self, vals: list):
        self.vals = vals

class A(Dictable):
    def __init__(self):
        self.a = 5
        self.b = 'b'
        self.c = E.test
        self.d = C()
        self.e = [B([996, 997]), B([998, 999])]
        self.f = {'key_1': B([996, 997]), 'key_2': B([998, 999])}

print(A().dict)
# Outputs: {'a': 5, 'b': 'b', 'c': <E.test: 'test_val'>, 'd': <__main__.C object at 0x102afa1d0>, 'e': [{'vals': [996, 997]}, {'vals': [998, 999]}], 'f': {'key_1': {'vals': [996, 997]}, 'key_2': {'vals': [998, 999]}}}

print(json.dumps(A().json, indent=4))
# Outputs: 
# {
#     "a": 5,
#     "b": "b",
#     "c": "test_val",
#     "d": {
#         "d1": {
#             "val": "str_val"
#         },
#         "d2": {
#             "val": "str_val"
#         }
#     },
#     "e": [
#         {
#             "vals": [
#                 996,
#                 997
#             ]
#         },
#         {
#             "vals": [
#                 998,
#                 999
#             ]
#         }
#     ],
#     "f": {
#         "key_1": {
#             "vals": [
#                 996,
#                 997
#             ]
#         },
#         "key_2": {
#             "vals": [
#                 998,
#                 999
#             ]
#         }
#     }
# }

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

to_dict-0.0.3.tar.gz (2.3 kB view hashes)

Uploaded Source

Built Distribution

to_dict-0.0.3-py3-none-any.whl (3.4 kB view hashes)

Uploaded Python 3

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