Skip to main content

Utilities for working with json-style datastructures.

Project description

DICTionary Operations

Is a library that makes applying functions to and performing operations on nested dicts easy.

This repository attempts to unify numerous utilities in one package.

Python >=3.7 is required as retained dict order is wanted.

Python library that inspired this project:

TODO: links

https://github.com/glowdigitalmedia/dict-utils/tree/master/dict_utils

Features

Scalpl seems to be the most polished library. Dicty differs with some slight changes in the api and additional set of functionality for operating on json like data structure:

  • apply an operation to multiple dictoos with same shape
  • setting the type for the dictoos values

Setup

pip install dictoo

Usage

import dictoo.Dicty as D

# you can configure dictoo to fit your needs
# dictoo only imports and uses what is enabled
dictoo.CONFIG.update({
	'delimiter': '.'
})

# you can create dictoo dicts from many things
d = D(dict(a=1, b=[3, dict(c=4)]))
d = D.from_file('dict.yaml')
d = D.from_file('dict.json')
d = D("{a:1, b[0]:3, b[1].c=4}")  //not sure whether this is a good idea

assert d.b[1].c == d['b.[1].c']

d.update(dict(b=[5, dict(c=9)]))

d.flat()

dictoo.apply(lambda x, y: x + y, d1, d2, kwargs)

dictoo.reduce(fn, [d1, d2, d3], kwargs)

dictoo.remap({'old_key_name': 'new_key_name'})

d = D([{'a': 1, 'b': 2}, {'a':4, 'b':5})
isinstance(d, list) == True
isinstance(d, dict) == False
assert d != [D({'a': 1, 'b': 2}), D({'a':4, 'b':5})]  # try not to leave lists of Ds as you would also not do with numpy arrays
assert d == D([D({'a': 1, 'b': 2}), D({'a':4, 'b':5})])
d[:, 'a'] = 8  # D([{'a': 8, 'b': 2}, {'a':8, 'b':5}])  # this is hard to get used to, maybe be more strict here such that the code is more readable. Should be configurable
d[:1, 'a'] = 8  # only modifies a slice

d.batch()['a'][:] = 8

d: Dict[str, Union[np.ndarray, dict]] = D(dict(...), dtype=np.ndarray)

d.batch()  # D({'a': [1, 4], 'b': [2, 5]}), batch goes from lowest level to top level and unbatch inreverse

# more features:
# cli for json and yaml files
# regex keys

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

dictoo-1.3.1.tar.gz (9.1 kB view details)

Uploaded Source

File details

Details for the file dictoo-1.3.1.tar.gz.

File metadata

  • Download URL: dictoo-1.3.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for dictoo-1.3.1.tar.gz
Algorithm Hash digest
SHA256 515800abe145e839f6d78a48e34c0493205fdc0a909cda642554b291e9e69530
MD5 cfee1071db4a2cfdfc2e61cebf00df50
BLAKE2b-256 a7dfa8d43bb288aa24da6850eca27716b3a0fa276d39e69b7a511af6a14b6616

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