Oba: Converting a Python iterable object to an attribute class
Introduction
Converting iterable objects (such as dictionaries, tuples, lists, sets, and subclasses) to access values using class attributes instead of square brackets []. Supports recursive operations and supports both getting and setting values.
Installation
1
pip install oba
Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from oba import Oba
# Convert a dictionary to an attribute class d = dict(a=[1, 2, 3], b=[4, dict(x=1)], c=dict(l='hello')) o = Obj(d)
Additionally, Oba also has a unique tolerance for unknown attributes. In cases where some attributes do not exist, for example:
1 2
d = dict(a=1) print(d['b']) # KeyError
Other libraries will immediately raise an error. However, in some scenarios (such as reading configuration files), the absence of sub-attributes is a common problem, and we hope to be able to tolerate and monitor the existence of such errors.
1 2 3 4 5 6 7 8 9
from oba import Obj
d = dict(a=1) o = Obj(d)
print('x'in o) # => False ifnot o.x.y.z: # OK print('not exist') # => not exist print(o.x.y.z) # => ValueError: NoneObj (x.y.z) # locating the non-existent attribute chain
Its internal implementation is that when an attribute does not exist, the object automatically switches to the NoneObj class and records the attribute chain.