DotDict
The DotDict
class is a generic dictionary implementation that allows accessing dictionary keys using both dictionary syntax (d["key"]
) and attribute syntax (d.key
).
Features
Generic type support for values
Optional default value for missing attributes
Dictionary-like operations (get, items, keys, values, update, etc.)
Attribute-style access to dictionary items
Protected attributes preservation
Shallow copy support
Type Parameters
T
: The type of values stored in the dictionary
Constructor
DotDict[T](missing_attr_val: Any = NULL_OBJ, **kwargs: T)
- Parameters:
missing_attr_val
: Value to return when accessing non-existent attributes. If not provided, raises AttributeError**kwargs
: Initial key-value pairs for the dictionary
Usage Examples
Basic Usage:
# String values
d1 = DotDict[str](a="hello", b="world")
print(d1.a) # "hello"
print(d1["b"]) # "world"
# Integer values
d2 = DotDict[int](a=1, b=2)
print(d2.a) # 1
# Mixed values with Union
from typing import Union
d3 = DotDict[Union[str, int]](a="hello", b=2)
# Mixed values with object
d4 = DotDict[object](a="hello", b=2)
Default Values:
# With default value for missing attributes
d = DotDict[str]("default", a="hello")
print(d.missing) # "default"
# With None as default
d = DotDict[str](None, a="hello")
print(d.missing) # None
Dictionary Operations:
d = DotDict[str](a="hello", b="world")
# Get value
value = d.get("a") # "hello"
default = d.get("missing", "default") # "default"
# Update
d.update({"c": "!"})
# Items, keys, values
items = dict(d.items())
keys = list(d.keys())
values = list(d.values())
# Copy
d_copy = d.copy()
d_dict = d.copy_dict() # returns standard dict
Notes
Protected Attributes: The following attributes are protected and not included in dictionary operations: -
_missing_attrib_value
-_internal_keys
-_is_protocol
Overriding Built-ins: It’s possible but not recommended to override built-in attributes like
keys
,copy
, anditems
:d = DotDict[str](a="hello", keys="world") print(d.keys) # "world" (not the keys() method)
Version History
Version 0.52.6: Added
__bool__
methodVersion 0.52.2: Added generic type support and missing attribute value feature
Class DotDict
- class ooodev.utils.helper.dot_dict.DotDict(missing_attr_val=<ooodev.utils.gen_util._null_obj object>, **kwargs)[source]
Generic class for accessing dictionary keys as attributes or keys as attributes.
- Type Parameters:
T: Value type
- Parameters:
missing_attr_val (Any, optional) – Value to return if attribute is not found. If omitted then AttributeError is raised if attribute is not found.
kwargs (T) – Keyword arguments.
Note
It is possible to override class attributes such as keys, copy, and items attributes. This is not recommended.
d = DotDict[str](a="hello", keys="world") assert d.keys == "world"
Example
# String values d1 = DotDict[str](a="hello", b="world") # Integer values d2 = DotDict[int](a=1, b=2) # Mixed values with Union d3 = DotDict[Union[str, int]](a="hello", b=2) # Mixed values with object d4 = DotDict[object](a="hello", b=2) # Mixed values with no generic type d5 = DotDict(a="hello", b=2) # Mixed values missing attribute value d6 = DotDict[object](None, a="hello", b=2) assert d6.missing is None
- __init__(missing_attr_val=<ooodev.utils.gen_util._null_obj object>, **kwargs)[source]
Constructor
- Parameters:
missing_attr_val (Any, optional) – Value to return if attribute is not found. If omitted then AttributeError is raised.
kwargs (T) – Keyword arguments.
- Return type:
None
- clear()[source]
Clears all items from the dictionary while preserving protected attributes.
- Return type:
None
- copy_dict()[source]
Returns a shallow copy as a standard dictionary.
- Return type:
Dict
[str
,TypeVar
(T
)]
- get(key, default=None)[source]
Get value from dictionary.
- Parameters:
key (KT) – Key to get value.
default (T | None, optional) – Default value if key not found. Defaults to None.
- Returns:
Value of key or default value.
- Return type:
T | None
- items()[source]
Returns all items in the dictionary in a set like object.
- Return type:
Generator[tuple[str, T], None, None]
- keys()[source]
Returns all keys in the dictionary in a set like object.
- Return type:
Generator
[str
,None
,None
]