A Python dict that supports attribute-style access as well as hierarchical keys, JSON serialization, ZIP compression, and more.
Project description
Turn a dict into an Object or objict!
Based on uberdict(https://github.com/eukaryote/uberdict)
Installation
pip install pyobjict
Some Differences:
- Support for to/from JSON
- Support for to/from XML
- Support for to/from ZIP compression (base64)
- Support to/from file
- When an attribute is not found it returns None instead of raising an Error
- Support for .get("a.b.c")
- Support for delta between to objicts (obj.changes())
- Will automatically handle key conversion from "a.b.c" to "a -> b -> c" creation
Simple to use!
>>> from objict import objict
>>> d1 = objict(name="John", age=24)
>>> d1
{'name': 'John', 'age': 24}
>>> d1.name
'John'
>>> d1.age
24
>>> d1.gender = "male"
>>> d1
{'name': 'John', 'age': 24, 'gender': 'male'}
>>> d1.gender
'male'
>>> import datetime
>>> d1.dob = datetime.datetime(1985, 5, 2)
>>> d1.dob
datetime.datetime(1985, 5, 2, 0, 0)
>>> d1.toJSON()
{'name': 'John', 'age': 24, 'gender': 'male', 'dob': 483865200.0}
>>> d1.save("test1.json")
>>> d2 = objict.fromFile("test1.json")
>>> d2
{'name': 'John', 'age': 24, 'gender': 'male', 'dob': 483865200.0}
>>> d2.toXML()
'<name>John</name><age>24</age><gender>male</gender><dob>483865200.0</dob>'
>>> d3 = objict(user1=d2)
>>> d3.user2 = objict(name="Jenny", age=27)
>>> d3
{'user1': {'name': 'John', 'age': 24, 'gender': 'male', 'dob': 483865200.0}, 'user2': {'name': 'Jenny', 'age': 27}}
>>> d3.toXML()
'<user1><name>John</name><age>24</age><gender>male</gender><dob>483865200.0</dob></user1><user2><name>Jenny</name><age>27</age></user2>'
>>> d3.toJSON(True)
'{\n "user1": {\n "name": "John",\n "age": 24,\n "gender": "male",\n "dob": 483865200.0\n },\n "user2": {\n "name": "Jenny",\n "age": 27\n }\n}'
>>> print(d3.toJSON(True))
{
"user1": {
"name": "John",
"age": 24,
"gender": "male",
"dob": 483865200.0
},
"user2": {
"name": "Jenny",
"age": 27
}
}
>>> d3.toZIP()
b'x\x9c\xab\xe6R\x00\x02\xa5\xd2\xe2\xd4"C%+\x85j0\x17,\x94\x97\x98\x9b\n\x14Q\xf2\xca\xcf\xc8S\xd2A\x88\'\xa6\x83\x84\x8dL\x90\x84\xd2S\xf3RR\x8b@\x8as\x13sR\x91\x15\xa7\xe4\'\x01\x85M,\x8c-\xccL\x8d\x0c\x0c\xf4\x0c\xc0R\xb5:\x08[\x8dp\xd8\x9a\x9a\x97W\x89\xc5Zs\x88\x01\\\xb5\x00^\x1c\'I'
>>> dz = d3.toZIP()
>>> d4 = objict.fromZIP(dz)
>>> d4
{'user1': {'name': 'John', 'age': 24, 'gender': 'male', 'dob': 483865200.0}, 'user2': {'name': 'Jenny', 'age': 27}}
>>> d5 = d4.copy()
>>> d5.user1.name
'John'
>>> d5.user1.name = "Jim"
>>> d5
{'user1': {'name': 'Jim', 'age': 24, 'gender': 'male', 'dob': 483865200.0}, 'user2': {'name': 'Jenny', 'age': 27}}
>>> d5.changes(d4)
{'user1': {'name': 'John'}}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyobjict-1.1.12.tar.gz
(9.0 kB
view details)
Built Distribution
File details
Details for the file pyobjict-1.1.12.tar.gz
.
File metadata
- Download URL: pyobjict-1.1.12.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.11.1 Darwin/22.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fa23457455669302502361206b76693fdf249d7be9a52be80c540d23e0f3cc5 |
|
MD5 | cd007a82abe4acd60a511b35c112160c |
|
BLAKE2b-256 | 31f039cb06da1ee07688317c5631444a45d5e2b2f25c4443aa1944ec86c4b821 |
File details
Details for the file pyobjict-1.1.12-py3-none-any.whl
.
File metadata
- Download URL: pyobjict-1.1.12-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.11.1 Darwin/22.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9326ab8d4b73392c69de1a94f9d825d91c2bd3f651cac3a8c5a29bf44696d1ed |
|
MD5 | a7c3a470ce786802d07b3e6b858dae7e |
|
BLAKE2b-256 | 80e33a493804aa705dde9b044798d4b2f390665eb8e08f073dc4ee1b64cdba0f |