Skip to main content

Just an extended Python dict with attribute-accessibility, extra methods, and metadata

Project description

lxdx is an “extended” Python dict with attribute-like accessibility. Other than the usual dict operations, functions and methods, some useful functions are incorporated as well.

Only supports Python 3.9+, due to the usage of the union operator for dict.

Why this project?

  • Hobbies and curiosities. Just for the fun of programming.

  • dataclass is not cut for modelling hierarchical data.

  • Brackets when accessing multi-layer data is too many. Dot notation may be a cleaner way.

  • Introduce utility functions like get_from(path), inspired from JsonPath, for programmability.

  • Ability to add metadata to the items.

Installation

lxdx is available in PyPI, and installable via pip:

pip install lxdx

Examples

from lxdx import Dixt

assert Dixt() == {}
assert Dixt({1: 1, 'alpha': 'α'}) == {1: 1, 'alpha': 'α'}
assert Dixt(alpha='α', beta='β') == {'alpha': 'α', 'beta': 'β'}
assert Dixt(alpha='α', beta='β').is_supermap_of({'beta': 'β'})

# data can be deeply nested
data = {'Accept-Encoding': 'gzip', 'metadata': {'Content-Type': 'application/json'}}
dx = Dixt(**data)

# update dx using the union operator
dx |= {'other': 'dict or Dixt obj'}

# 'Normalise' the keys to use it as attributes additionally.
assert dx['Accept-Encoding'] == dx.accept_encoding
del dx.accept_encoding
print(dx.metadata.CONTENT_TYPE)

# Instead of
dx['a-list'][1]['obj-attr'] = 'value'

# Is way cleaner
dx.a_list[1].obj_attr = 'value'

# Programmatically get values
assert dx.a_list[1].obj_attr == dx.get_from('$.a_list[1].obj_attr')

json_str = '{"a": "JSON string"}'
assert Dixt.from_json(json_str).json() == json_str

Documentation

Full documentation is at https://hardistones.github.io/lxdx.

Future

lxdx is supposed to be a library of “extended” list and dict. For now there’s no use case for the list extension.

To be supported

  • User-defined meta specification.

License

This project and all its files are licensed under the 3-Clause BSD License.

Copyright (c) 2021, @github.com/hardistones All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

lxdx-0.6.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

lxdx-0.6.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file lxdx-0.6.0.tar.gz.

File metadata

  • Download URL: lxdx-0.6.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for lxdx-0.6.0.tar.gz
Algorithm Hash digest
SHA256 38e73365e0dafe70802d01ddb18be5b352f01b54e732e8c923fe2096f5082ab5
MD5 a9aac79212d831484b243f661d510dd8
BLAKE2b-256 906498c33775bb1f4eed0a06bdea86de1b27bdc7655c5bdb6890e633764b9bd0

See more details on using hashes here.

File details

Details for the file lxdx-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: lxdx-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for lxdx-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b4d95cf39fe17e9eb55adf056e172309c9c7725230e05a4ecff11f3ebcc28de
MD5 e8c56c2f966a3e4dcfbfd37404b1d7c9
BLAKE2b-256 46550263d5a31f931b5b49f0be2fccdfa74b3fe79ce8f647d3f25a3521dc23bc

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