Skip to main content

A Python library to access dictionaries using dot notation.

Project description

PyPI Downloads PyPI GitHub issues GitHub last commit License Python Tests

Dottify

Dottify is a lightweight Python library that converts dictionaries into objects with attribute-style access. Instead of the usual dict["key"] syntax, you can access dictionary values using dot notation like dict.key. All access is case-sensitive, but helpful suggestions are provided when a key is missing.

Installation

Install via pip:

pip install dottify

Usage

Here’s an example of how Dottify works:

from dottify import Dottify

# Initial data
persons = {
    "Alice": {
        "age": 30,
        "city": "Paris",
        "profession": "Engineer"
    },
    "Charlie": {
        "age": 35,
        "city": "Marseille",
        "profession": "Doctor"
    }
}

# Wrap with Dottify
people = Dottify(persons)

# Merge with + operator (__add__)
new_person = {
    "Bob": {
        "age": 2,
        "city": "Lyon",
        "profession": "Designer"
    }
}
people = people + new_person

# In-place merge with += (__iadd__)
people += Dottify({
    "John": {
        "age": 27,
        "city": "Toulouse",
        "profession": "Carpenter"
    }
})

# Access by dot notation and key lookup
print(people.Alice.age)             # 30
print(people["Charlie"].city)       # Marseille
print(people.Bob.profession)        # Designer

# Index-based access (__getitem__)
print(people[3].profession)         # Carpenter (John)

# Modify attributes
people.John.profession = "Developer"
people.Bob.age = 39

# Use get() with case-insensitive fallback
print(people.get("alice", "Not Found").city)     # Paris
print(people.get("ALICIA", "Not Found"))         # Not Found

# Remove a key by name
people.remove("Bob")  # Removes Bob

# Check if a key exists
print(people.has_key("charlie"))     # False (because 'charlie' != 'Charlie')
print(people.has_key("Charlie"))     # True
print(people.has_key("unknown"))     # False

# Use len(), keys(), values(), items()
print(len(people))                   # 3 (Alice, Charlie, John)
print(list(people.keys()))           # ['Alice', 'Charlie', 'John']
print([v.city for v in people.values()])  # ['Paris', 'Marseille', 'Toulouse']
print([(k, v.age) for k, v in people.items()])  # [('Alice', 30), ('Charlie', 35), ('John', 27)]

Features

  • Converts standard and nested dictionaries into objects with attribute access.
  • Supports both dot notation (obj.key) and dictionary-style (obj["key"]) access.
  • All access is case-sensitive.
  • Friendly error messages with key suggestions (case-insensitive search).
  • Key removal with .remove("Key") is case-sensitive, but also provides suggestions if the key doesn't match.
  • Easily convert back to a standard dict using .to_dict().
  • Supports .keys(), .values(), .items(), iteration, and len() additions.
  • Well-documented and fully tested with pytest.

Tests

To run the test suite:

pytest tests/

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests on the GitHub repository.

License

Distributed under the MIT License. See LICENSE for more information.

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

dottify-1.1.4.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

dottify-1.1.4-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file dottify-1.1.4.tar.gz.

File metadata

  • Download URL: dottify-1.1.4.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dottify-1.1.4.tar.gz
Algorithm Hash digest
SHA256 d60a7fc3cf6ad2c69902ef1c0b806cd48aea2a05815a64de9de9d803310a7507
MD5 7e315a42a4730943e6cad2a8dce525a5
BLAKE2b-256 4beb02f441da3dfea8b3d50a93e64b45112f3ae2a12a4faf58bc294a677c1183

See more details on using hashes here.

File details

Details for the file dottify-1.1.4-py3-none-any.whl.

File metadata

  • Download URL: dottify-1.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dottify-1.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2ba886800e20ca678eb3ec72c5439bf421b764ea8c6b842b59190c12c8565507
MD5 f416551c44cf160d97d7b9f62327c817
BLAKE2b-256 b81ecff07f1aaa21df4b351af0031a6840690a7cd10bc2ef44d321743aa0fb5f

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