Skip to main content

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-2.0.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyobjict-2.0.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file pyobjict-2.0.0.tar.gz.

File metadata

  • Download URL: pyobjict-2.0.0.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.16 Darwin/24.3.0

File hashes

Hashes for pyobjict-2.0.0.tar.gz
Algorithm Hash digest
SHA256 690f07760ec2e168766053e96efc65ea3cd9780d8668f1fd0dd4994cfe690d18
MD5 9b20e8fef8c82189c62f8a9eccab2438
BLAKE2b-256 137a730f70686808bc32890d6f34ae8fc9472aedebed486f8da64baaad5362ae

See more details on using hashes here.

File details

Details for the file pyobjict-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyobjict-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.16 Darwin/24.3.0

File hashes

Hashes for pyobjict-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21d20e1f3bf16210c00b09b6ae6f2ed0838c9e0e6cc879a6e508cc55fef795a5
MD5 344eae20872d1a485d2f5b5acfc6a588
BLAKE2b-256 8da50a3821da19096f3009ff9ce65f683201ff49293390c16bf44e56c77bb822

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page