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

Uploaded Source

Built Distribution

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

pyobjict-1.1.13-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyobjict-1.1.13.tar.gz
  • Upload date:
  • Size: 8.1 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-1.1.13.tar.gz
Algorithm Hash digest
SHA256 69b9580e5293f113cfb5f2f36ab22c933e2d5bf7868ab726d203f7448e7a8bf0
MD5 fae7e666b23f0f2f01805b7fb8a98cbe
BLAKE2b-256 c681a17aacaa820952771566184c396e896c696af458c719dcc88b34394b85d9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyobjict-1.1.13-py3-none-any.whl
  • Upload date:
  • Size: 8.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-1.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 d81b679f7ef01520d66e3ed967c0709cacc9998b02246c957e480a1450835364
MD5 2ea0941e93554017af242694fe2fe308
BLAKE2b-256 4700c6dc4f6b10eb07e52200284dc1ad4a5d0a9dc065932219727777bad1992b

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