Skip to main content

dnode

$ pip install dnode

from dnode import *


print('============== load data ===============')

data = {
    'a': 1,
    'b': {'b1': 3},
    'c': {'c1': 1, 'c2': {'c22': 22}},
    'd': ['d1', 'd2', 'd3'],
    'e': [{'ee': 1}, {'ee': 2}, {'ee': 3}],
    'f': [['f11', 'f12'], ['f21', 'f22']],
    'g': [[{'gg': 11}, {'gg': 12}], [{'gg': 21}, {'gg': 22}]],
}

obj = DNode(data)

print(obj.to_dict())
assert obj.to_dict() == data

print('=========== print object ===============')

print(obj)
obj.pprint()

print('============= print json ===============')

print(obj.json)  
# or print(obj.dumps(indent=4))

print('=========== test getattr ===============')

assert obj.a == 1
assert obj.b.b1 == 3
assert obj.c.c2 == {'c22': 22} == DNode({'c22': 22})
assert obj.d[1] == 'd2'
assert obj.e[1].ee == 2
assert obj.f[0][0] == 'f11'
assert obj.g[0][0].gg == 11
assert obj.h == None

print('=========== test setattr ===============')

obj.a = 'change_a'
obj.b.b1 = 'change_b'
obj.c.c2.c22 = 'change_c'
obj.d[1] = 'change_d'
obj.e[1].ee = 'change_e'
obj.f[0][0] = 'change_f'
obj.g[0][0].gg = 'change_g'

data = obj.to_dict()
assert data['a'] == 'change_a'
assert data['b']['b1'] == 'change_b'
assert data['c']['c2']['c22'] == 'change_c'
assert data['d'][1] == 'change_d'
assert data['e'][1]['ee'] == 'change_e'
assert data['f'][0][0] == 'change_f'
assert data['g'][0][0]['gg'] == 'change_g'

print('======== test set non-json type =========')

obj.a = {1, 2, 3}
data = obj.to_dict()
assert data['a'] == None

print('============== test clear ===============')

obj.clear()
obj.pprint()

print('============== list init ================')

data = '[ {"a": 1}, [{"b": 2}, {"c": 3}] ]'
rs = DNode(data)
print(rs)
assert isinstance(rs, list)
assert rs[0].a == 1
assert rs[1][0].b == 2
assert rs[1][1].c == 3

print('=========================================')

print('\n\n-------------- test SMNode ---------------')

print('========= test STRUCT_FIELDS ============')

obj = SMNode({})
SMNode.STRUCT_FIELDS = set(['aaa', 'bbb'])
obj.aaa = 123
obj.ccc = 456
print(obj.json)  
print('`ccc` should not in the output')

print('=========================================')

Metadata

Release files for dnode 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dnode 0.2.6
File Size Uploaded
dnode-0.2.6.tar.gz 4.4 kB Details

Release files / dnode-0.2.6.tar.gz

Download URL dnode-0.2.6.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2a39da6fc727c189973e51688a1212e3d534458b7d7547c1ed460238ad085d7e
BLAKE2b-256 checksum
How to use checksums
bbbe5c7542f6884d81052ba4081fab8ea8d73734db4237bb9fe1927db6bf3fe1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.2.6 This release

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.1

1 release file

0.2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page