A dictionary extension that supports regex-based key search.
Project description
Dictoria: Regex-based Dictionary Extension
Dictoria is an extension of Python's built-in dict type that allows searching for values using regular expressions. The library supports recursive searching in nested dictionaries and provides convenient methods for filtering results.
Keys in the dictionary are represented as paths in the format /root/key/subkey, making it intuitive and powerful to work with complex data structures.
Table of Contents
- Description
- Installation
- Quick Start
- Key Features
- Examples
- Documentation
- Requirements
- License
- Authors
- Contributing
- Contact
Description
Dictoria extends Python's standard dictionary to enable regex-based key searches. It is particularly useful for scenarios where dynamic access to nested data structures is required. With Dictoria, you can:
- Search for values using regular expressions.
- Filter results by type or content.
- Handle non-existent keys gracefully with a
defaultparameter.
This library simplifies working with deeply nested dictionaries and provides a flexible way to retrieve data.
Installation
You can install Dictoria using pip:
pip install dictoria
If you're using poetry, add the library to your project:
poetry add dictoria
Quick Start
from dictoria import Dictoria
# Create a Dictoria instance
data = Dictoria({
"users": {
"user1": {"name": "Alice", "age": 25},
"user2": {"name": "Bob", "age": 30}
},
"settings": {
"timeout": 60,
"debug": False
}
})
# Search for all user ages
ages: list[int] = list(data[r"/users/.+/age"])
print(ages) # Output: [25, 30]
# Search for a specific user name
name: str = data.get(r"/users/.+/name", target=r"Alice")
print(name) # Output: Alice
# Handle non-existent keys
result: str = data.get(r"/nonexistent", default="Not found")
print(result) # Output: Not found
Key Features
Initialization
Dictoria accepts a dictionary as input when creating an instance:
data = Dictoria({
"key1": "value1",
"key2": {
"subkey1": "subvalue1",
"subkey2": "subvalue2"
}
})
Method __getitem__
The __getitem__ method allows retrieving values from the dictionary using regular expressions. Keys are represented as paths in the format /root/key/subkey.
# Retrieve all values matching the pattern
ages: list[int] = list(data[r"/users/.+/age"])
print(ages) # Output: [25, 30]
Method get
The get method extends the standard dict.get functionality and supports additional parameters for filtering results:
typeof: Filters results by the specified type.target: Matches values against a regular expression.default: Specifies a default value if no matches are found.
# Filter by type
age: int = data.get(r"/users/.+/age", typeof=int)
print(age) # Output: 25
# Match value content
name: str = data.get(r"/users/.+/name", target=r"Alice")
print(name) # Output: Alice
# Handle missing keys
result: str = data.get(r"/nonexistent", default="Not found")
print(result) # Output: Not found
Examples
Example 1: Recursive Age Search
from dictoria import Dictoria
data = Dictoria({
"users": {
"user1": {"name": "Alice", "age": 25},
"user2": {"name": "Bob", "age": 30}
}
})
# Search for all user ages
ages: list[int] = list(data[r"/users/.+/age"])
print(ages) # Output: [25, 30]
Example 2: Combined Filtering
# Filter by type
age: int = data.get(r"/users/.+/age", typeof=int)
print(age) # Output: 25
# Match value content
name: str = data.get(r"/users/.+/name", target=r"Alice")
print(name) # Output: Alice
Example 3: Error Handling
# If no matches are found, return a default value
result: str = data.get(r"/nonexistent", default="Not found")
print(result) # Output: Not found
Documentation
You can also generate the documentation locally using Sphinx :
- Install Sphinx:
poetry add sphinx
- Build the documentation:
sphinx-build -b html docs/ docs/_build
- Open the generated documentation:
open docs/_build/index.html
Requirements
- Python 3.8+
- Built-in libraries:
re(for regular expressions)
License
This project is licensed under the MIT License.
Authors
- Tamerlan Larsanov
@tamerlanlarsanov
Contributing
Contributions are welcome! To contribute to Dictoria:
- Fork the repository.
- Create a new branch (
git checkout -b feature/new-feature). - Make your changes and submit a pull request. Please ensure your code adheres to the existing style and includes appropriate tests.
Contact
@tamerlanlarsanov
Project details
Release history Release notifications | RSS feed
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 dictoria-0.1.9.tar.gz.
File metadata
- Download URL: dictoria-0.1.9.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-51-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00e203ecc29d41d5971859b34a386f590f1efc01c0f926f8e62a600a77181dda
|
|
| MD5 |
79790646d2ac98b161e0e0a545f15d3c
|
|
| BLAKE2b-256 |
67dbd8b40da9cf0ad292d7595d322b6acf8928ca9ff34890d4016ab2085f051a
|
File details
Details for the file dictoria-0.1.9-py3-none-any.whl.
File metadata
- Download URL: dictoria-0.1.9-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-51-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
384e9c9d41f4081ff932bb1254a88cbbe20b5332948ab7f97fee648bfc9be667
|
|
| MD5 |
32ea51ba4683adc091b4c3486c035dfd
|
|
| BLAKE2b-256 |
a9b55e71d3d3877ab2a57b84194bca21b81047ee79da65ec64837b340ffb30b6
|