Skip to main content

Utility/Helper functions build around dictionary data type in Python

Project description

NestDict: Advanced Nested Dictionary Library for Python

PyPI Python Versions

Table of Contents

Overview

NestDict is a powerful Python library that extends the standard dictionary functionality to handle nested dictionaries, providing advanced features such as validation and support for frozen dictionaries. This library simplifies the manipulation of complex data structures, making it an ideal choice for applications that require dynamic data management.

Features

  • Nested Dictionary Handling: Seamlessly access and manipulate deeply nested dictionaries.
  • Validation: Validate data types based on predefined mappings.
  • Frozen Dictionaries: Create immutable nested dictionaries to protect critical data.
  • List Support: Manage lists within nested structures effectively.

Installation

You can install NestDict using pip:

pip install nestdict

Usage

Here’s a quick example of how to use NestDict:

from nestdict import NestDict

# Create a nested dictionary
data = {
    "user": {
        "name": "John Doe",
        "age": 30,
        "address": {
            "city": "New York",
            "zip": "10001"
        }
    }
}

# Initialize NestDict
nested_dict = NestDict(data)

# Access nested data
print(nested_dict.get("user.name"))  # Output: John Doe

# Set new values
nested_dict["user.age"] =  31

# print out dict
print(nested_dict)

# save final dict object
final_dict = nested_dict.to_dict()

# Validate data
validation_rules = {
    "user.age": int,
    "user.name": str
}
nested_dict_with_validation = NestDict(data, validation=validation_rules)

API Reference

  • get(key_path, default=None) Retrieves the value at the specified key path in the nested dictionary. If the key path does not exist, it returns the specified default value (or None if not provided).

  • __getitem__(key_path) Allows access to the value at the specified key path using bracket notation (e.g., nested_dict[key_path]). Raises a KeyError if the key path is not found.

  • __setitem__(key_path, value) Sets the value at the specified key path using bracket notation (e.g., nested_dict[key_path] = value). It validates the value's type according to the validation rules if provided during initialization.

  • delete(key_path) Deletes the value at the specified key path. If the key path does not exist, it raises a KeyError.

  • to_dict() Returns the nested structure as a standard dictionary, representing the current state of the data.

Data Parameter

The data parameter is the initial nested dictionary structure that you want to manage using the NestDict class. It can be any valid Python dictionary (or list of dictionaries) that you need to work with.

Key Points:

  • Type: Accepts a dict or list.
  • Nested Structure: You can create deeply nested dictionaries. For example, {"a": {"b": {"c": 1}}} is a valid input.
  • Mutable: The data is mutable, meaning you can modify it using the available methods like set, delete, or through direct item access.

Validation Parameter

The validation parameter is an optional dictionary used to enforce type checking on the values in your nested dictionary. It allows you to define expected data types for specific keys.

Key Points:

  • Type: Accepts a dict where:
    • Keys: Are the key paths (in dot notation) that you want to validate. For example, "user.age" for a nested dictionary structure.
    • Values: Are the expected data types (e.g., int, str, list, dict) for those keys.
  • Validation Check: When you set a value for a key specified in the validation dictionary, the library checks if the value is of the expected type. If it’s not, a ValueError is raised.
  • Initialization Validation: The validation is performed both during the initialization of the NestDict instance and when using the set method.

Contributing Guidelines

Contributions to NestDict are welcome! To maintain a high standard for our project, please follow these guidelines when contributing:

  1. Fork the Repository: Start by forking the repository to your account.

  2. Create a New Branch: Create a new branch for your feature or bug fix:

    git checkout -b feature/YourFeatureName
    
  3. Make Changes: Implement your changes and ensure that your code adheres to our coding standards.

  4. Write Tests: If applicable, add unit tests to cover your changes. Ensure that all tests pass before submitting your changes.

  5. Commit Your Changes: Use clear and concise commit messages that explain the purpose of the changes. Refer to the COMMIT_GUIDELINES.md for detailed commit message conventions.

  6. Push Your Branch: Push your changes to your fork:

    git push origin feature/YourFeatureName
    
  7. Submit a Pull Request: Navigate to the original repository and submit a pull request, explaining your changes and the motivation behind them.

  8. Respect the License: Ensure that any contributions you make do not violate the existing license terms. Contributions should not be commercialized without explicit permission.

Thank you for contributing to NestDict!

Commit Guidelines

We follow specific conventions for our commit messages to maintain clarity and consistency. Please refer to the COMMIT_GUIDELINES.md file for detailed commit message conventions.

License

This project is licensed under the MIT License. See the License file for more details.

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

nestdict-1.0.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

nestdict-1.0.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file nestdict-1.0.1.tar.gz.

File metadata

  • Download URL: nestdict-1.0.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for nestdict-1.0.1.tar.gz
Algorithm Hash digest
SHA256 19d17bce8eaef381261bc57e9e04796796234d4502d813cb3e5685ed320945c0
MD5 1ca2ebabf7d45eff4a0998a8006cb825
BLAKE2b-256 28962b4beb8fbd5d05ee2b3ff910ca2f4b4b4ee4003ca0eb79e0ac1909a897e5

See more details on using hashes here.

File details

Details for the file nestdict-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: nestdict-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for nestdict-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07d4c01e8ddd340eb4e02294712948539f55e30c53ab1a817d7fe8fa407f81ba
MD5 4ac57eba16a72f7c0fe487f61b588990
BLAKE2b-256 8f5fbcd60c3e620afc61a678e72da591a943e5093946398422715d60abae0d9a

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