Imporve object
Project description
imobject
If you want to make data handling easier in Python, you can use imobject. It is a library that has three helpful classes: ObjDict
, ImprovedList
and OrmCollection
.
The ObjDict
class allows you to create dictionary objects (Dictionary-like object or Dynamic Class as dict), which makes it easier to access and manipulate data elements. This class inherits from the built-in dict
class and adds features such as converting dictionaries to objects, handling missing keys and access dictionary keys as attributes.
The ImprovedList
class offers an improved alternative to the Python list class, which it provide additional methods for working with lists of ObjDict
. This class extends the built-in list
class and adds features such as inspecting the elements of a list and performing advanced mapping operations.
The OrmCollection
class providing an interface and additional methods for querying and manipulating objects in the ImprovedList
. This class is a list-like collection that extends ImprovedList
and that implements an ORM overlay (wrapper) for a list of ObjDict
.
Exemples
ObjDict
>>> from imobject import ObjDict
>>> class Person:
def __init__(self, name: str, age: int) -> None:
self.name = name
self.age = age
def __repr__(self) -> str:
return f"Person(name='{self.name}', age={self.age})"
>>> person = Person("zakaria", 33)
>>> obj = ObjDict({"a": 1, "test": {"zak": person}, "b": {"c": 2, "d": [3, 4]}})
>>> obj.a
1
>>> obj.select(['a'])
{'a': 1}
>>> res = obj.select(['a', 'b'])
>>> res
{'a': 1, 'b': {'c': 2, 'd': [3, 4]}}
>>> type(res)
<class 'imobject.obj_dict.ObjDict'>
>>> obj.select(1)
TypeError: Argument 'wanted_keys' should be a list, got 'int' instead.
>>> obj.select("d")
TypeError: Argument 'wanted_keys' should be a list, got 'str' instead.
>>> obj.select(["c"])
KeyError: "'ObjDict' object has no attribute 'c'"
>>> res.b
{'c': 2, 'd': [3, 4]}
>>> obj.select(['a', 'test'])
{'a': 1, 'test': {'zak': Person(name='zakaria', age=33)}}
>>> obj.select(['c'])
KeyError: "'ObjDict' object has no attribute 'c'"
>>> obj.a = 13
>>> obj.a
13
>>> obj['b']['c']
2
>>> obj.b.c
2
>>> obj.x = {"y": 5}
>>> obj.x
{"y": 5}
>>> obj.inspect
{ 'a': 13,
'b': {'c': 2, 'd': [3, 4]},
'test': {'zak': Person(name='zakaria', age=33)},
'x': {'y': 5}}
>>> del obj.a
>>> obj.a
AttributeError: 'ObjDict' object has no attribute 'a'
>>> obj_dict = obj.to_dict()
>>> obj_dict
{'test': {'zak': Person(name='zakaria', age=33)}, 'b': {'c': 2, 'd': [3, 4]}, 'x': {'y': 5}}
>>> type(obj_dict)
<class 'dict'>
>>> obj = ObjDict({
"name": "Jane",
"age": 35,
"address": {"street": "456 Second St", "city": "Othertown", "state": "MA"},
"scores": [85, 95],
})
>>> obj
{'name': 'Jane', 'age': 35, 'address': {'street': '456 Second St', 'city': 'Othertown', 'state': 'MA'}, 'scores': [85, 95]}
>>> obj.update({
"name": "Will",
"age": 50,
"address": {"street": "456 Second St", "city": "Othertown", "state": "LA"},
"scores": [85, 100],
})
>>> obj
{'name': 'Will', 'age': 50, 'address': {'street': '456 Second St', 'city': 'Othertown', 'state': 'LA'}, 'scores': [85, 100]}
>>> obj.items()
[('name', 'Will'), ('age', 50), ('address', {'street': '456 Second St', 'city': 'Othertown', 'state': 'LA'}), ('scores', [85, 100])]
>>> copied_dict = obj.copy()
>>> copied_dict
{'name': 'Will', 'age': 50, 'address': {'street': '456 Second St', 'city': 'Othertown', 'state': 'LA'}, 'scores': [85, 100]}
ImprovedList
OrmCollection
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.