Skip to main content

Python library to work with complicated nested dict

Project description

DictPath

Extended dict for Python. Make easy to extract complicated nested dict.

repo-size license

Installation

pip3 install dict-path

Getting and setting value with path

No more boilerplate code to extract or inject nested dict

What you do previously
test_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
foo1 = test_dict['foo1']
if foo1:
   foo2 = foo1['foo2']
   if foo2:
       foo3 = foo2['foo3']
       if foo3:
           foo4 = foo3['foo4'] #finally, get the result: bar1
What you can do NOW
test_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
data = DictPath(test_dict)
data.get('foo1/foo2/foo3/foo4') #result: bar
Set value with easy
data.set('foo1/foo2/foo3/foo5', 'bar1')
data.get('foo1/foo2/foo3/foo5') #result: bar1

Do not want to use new Object? no worries, we have a method that you can call directly

from dict_path import extract_dict, inject_dict

test_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
extract_dict(test_dict, 'foo1/foo2/foo3/foo4') #result: bar

inject_dict(test_dict, 'foo1/foo2/foo3/foo5', 'bar1')}
extract_dict(test_dict, 'foo1/foo2/foo3/foo5') #result: bar1

Concept

DictPath is basically a normal python dict, nothing different

# A DictPath keeps a reference to the original initializing dict:

normal_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
dic_path = DictPath(normal_dict)
> normal_dict == dic_path
---> True
> dic_path.dict is normal_dict
---> True

You can also get a deep copy:

joe = DictPath(user, deepcopy=True)
> joe == user
---> True
> joe.dict is user
---> False

Invalid path will return None

from dict_path import DictPath

test_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
data = DictPath(test_dict)
data.get('foo1/foo2/foo3/foo4/foo6')
#result: None

Set up unknown path will create an actual dict

from dict_path import DictPath

test_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
data = DictPath(test_dict)
data.set('foo1/foo2/foo3/foo5/foo6/foo7/foo8/', 'bar1')
data.get('foo1/foo2/foo3/foo5/foo6/foo7/foo8/')
#result: bar1

Its also working with array

Note: For now its only working with get or extract_dict.

from dict_path import extract_dict, DictPath

test_dict = {'foo1':{'foo2':[{'foo3':'bar'},{'foo4':[[{'foo5': 'bar'}]]}]}}
data = DictPath(test_dict)
data.get('foo1/foo2/1/foo4/0/0/foo5/')
#result: bar

extract_dict(test_dict, 'foo1/foo2/1/foo4/0/0/foo5/')

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

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

dict-path-1.1.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

dict_path-1.1.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file dict-path-1.1.1.tar.gz.

File metadata

  • Download URL: dict-path-1.1.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for dict-path-1.1.1.tar.gz
Algorithm Hash digest
SHA256 7e928aa59b12ce8f2786356a865f745c2762ab9359aae02ac46426729f1a9baf
MD5 ee745ef3d51404f687ca259d45239449
BLAKE2b-256 c08d769956f98b1a0b6324cf3dd74ebb96a74815fca8c71e804cd85ed8fde5d7

See more details on using hashes here.

File details

Details for the file dict_path-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: dict_path-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for dict_path-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77defee9208d0c47f94072d02e9b56c3e710da2c90154cffc5df900948467427
MD5 62a6b9b3e0f42eb778207785026fe344
BLAKE2b-256 08d428fd43a71b8703cb8e5a6f066114d39b12e8caa40c9687707f5e58092317

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