A Python library to access dictionaries using dot notation.
Project description
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 (case-sensitive)
people.remove("Bob") # Removes Bob
# Check if a key exists (case-insensitive)
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, andlen()additions.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dottify-1.1.0.tar.gz.
File metadata
- Download URL: dottify-1.1.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a25ae9b9659887dd608d906be7096def960998b330278f81e658f44b9f7ee992
|
|
| MD5 |
c292dc31c9f267fecad39898fd7c4da6
|
|
| BLAKE2b-256 |
79672596ad1274b3e1f83793475fdc1e389e531f61813523096ba42762ceba25
|
File details
Details for the file dottify-1.1.0-py3-none-any.whl.
File metadata
- Download URL: dottify-1.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ee7e867a1e37ac82a456d9bbec20e50c36048c18461149bc3d8b3fbeba7efb3
|
|
| MD5 |
5519c9cbf4cd5809fbd35515a83bb930
|
|
| BLAKE2b-256 |
04909e6176e33c89e1f9ef6a4de35cc051e6e71e1c57e6d2070c7db09a28d059
|