obj¶
Object base class with dict-like attribute access and recursive serialization.
The Obj class provides a convenient container where attributes can be set via keyword arguments and accessed like dictionary keys. It supports recursive conversion to plain dicts via as_dict(), shallow cloning, and equality comparison based on attribute contents.
Classes:
| Name | Description |
|---|---|
Obj |
A flexible object container with dict-like attribute access. |
Functions:
| Name | Description |
|---|---|
str_value |
Formats a value for string representation, escaping strings. |
Obj
¶
A flexible object container with dict-like attribute access.
Attributes set via keyword arguments are stored in __dict__ and can be
accessed normally. Supports recursive conversion to plain dicts, shallow
cloning with overrides, and equality comparison based on attribute contents.
Example
from py_misc_utils import obj as objm
o = objm.Obj(a=1, b='x', nested=objm.Obj(c=2))
assert o.as_dict() == {'a': 1, 'b': 'x', 'nested': {'c': 2}}
c = o.clone(b='y')
assert c.b == 'y' and c.a == 1
assert o == objm.Obj(a=1, b='x', nested=objm.Obj(c=2))
Source code in py_misc_utils/obj.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
update(**kwargs)
¶
Updates the object's attributes from keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Attribute names and values to set or override. |
{}
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in py_misc_utils/obj.py
48 49 50 51 52 53 54 55 56 57 58 59 | |
update_from(obj)
¶
Copies all attributes from another object into this one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
An object whose |
required |
Returns:
| Type | Description |
|---|---|
|
|
Source code in py_misc_utils/obj.py
61 62 63 64 65 66 67 68 69 70 71 72 | |
clone(**kwargs)
¶
Returns a shallow copy of this object with optional attribute overrides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Attribute names and values to override on the copy. |
{}
|
Returns:
| Type | Description |
|---|---|
|
A new |
|
|
specified attributes updated. |
Source code in py_misc_utils/obj.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
as_dict()
¶
Recursively converts this object and nested Obj instances to a plain dict.
Nested Obj instances, lists/tuples containing Obj instances, and dicts containing Obj instances are all recursively converted.
Returns:
| Type | Description |
|---|---|
|
A plain dictionary representation of this object's attributes. |
Source code in py_misc_utils/obj.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
str_value(v)
¶
Formats a value for string representation.
String values are quoted and have internal quotes escaped. Non-string values
are converted via str().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
The value to format. |
required |
Returns:
| Type | Description |
|---|---|
|
A string representation of |
Source code in py_misc_utils/obj.py
159 160 161 162 163 164 165 166 167 168 169 170 171 | |