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

objict

objict is a Python library that extends the standard dictionary to allow attribute-style access, hierarchical keys, and additional functionalities like JSON/XML serialization, delta comparison, and more. It's inspired by the uberdict library.

Installation

To install objict, use the following pip command:

pip install pyobjict

Features

  • Attribute Access: Access dictionary values using dot notation (e.g., d.key).
  • Hierarchical Keys: Seamlessly handle nested dictionary structures with keys like a.b.c.
  • Serialization: Convert to/from JSON, XML, and ZIP formats.
  • File Operations: Read from and write to files conveniently.
  • Delta Comparison: Compute differences between two objicts.
  • Resilient Missing Keys: Returns None instead of raising an error for missing attributes.
  • Type Safety and Conversion: Supports type-safe fetching of values.

Basic Usage

from objict import objict

# Creating an objict
d1 = objict(name="John", age=24)
print(d1.name)  # Output: John
print(d1["age"])  # Output: 24

# Adding new attributes
d1.gender = "male"
print(d1.gender)  # Output: male

# Nested objict
d2 = objict(user=d1)
print(d2.user.name)  # Output: John

Advanced Usage

Hierarchical Keys

d3 = objict()
d3.set("address.street", "123 Elm St")
print(d3.address.street)  # Output: 123 Elm St

Serialization

JSON

json_data = d1.to_json(as_string=True, pretty=True)
print(json_data)

XML

xml_data = d1.to_xml()
print(xml_data)

ZIP Compression

zip_data = d1.to_zip(as_string=True)

File Operations

d1.save("data.json")
d3 = objict.from_file("data.json")
print(d3)  # Reconstructed objict from file

Delta Comparison

d4 = objict(name="John", age=25)
changes = d1.changes(d4)
print(changes)  # Output: {'age': 25}

Type Safety and Conversion

dob = d1.get_typed("dob", typed=datetime.datetime)

Handling Missing Keys and Defaults

print(d1.unknown_key)  # Output: None

d1.setdefault("country", "USA")
print(d1.country)  # Output: USA

Utility Methods

  • to_json(): Convert objict to JSON.
  • to_xml(): Convert objict to XML.
  • to_zip(): Compress objict.
  • from_json(json_string): Create objict from JSON.
  • from_file(path): Load objict from a file.
  • copy(shallow=True): Create a copy of the objict.
  • extend(*args, **kwargs): Merge another dictionary into this objict.

Conclusion

objict is a versatile tool for developers who want more flexible dictionary operations in Python. Whether you're dealing with nested data, performing file I/O, or converting data formats, objict simplifies these tasks with a clean and Pythonic interface.

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.4.tar.gz (10.4 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.4-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyobjict-2.0.4.tar.gz
Algorithm Hash digest
SHA256 55848696f754b3adfb07f214ccc9d0efd7ab01c444acd2e93e0d35c79a241a35
MD5 946786f34ed435e20297581ebb63aec3
BLAKE2b-256 4bd417634ab464879a8af73bfd8f7767d658e18eeb74860692f29d91e4e61fe4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyobjict-2.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fe60e57367f90e54a73f41879377f93b3adecf68a5a85394edbccb457e27efd4
MD5 9aeef96b2e8a1188f7edfb3fd400eb25
BLAKE2b-256 fb40120ee3356e5883fe0c3c6ef9533cba40b61d78a5e5a2df2f735165de506c

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