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
Noneinstead 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
objictto JSON. - to_xml(): Convert
objictto XML. - to_zip(): Compress
objict. - from_json(json_string): Create
objictfrom JSON. - from_file(path): Load
objictfrom 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.
Release files for pyobjict 2.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyobjict-2.1.3.tar.gz | 10.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyobjict-2.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.0 kB
Release files / pyobjict-2.1.3.tar.gz
| Download URL | pyobjict-2.1.3.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1caf36d4a67df9cc720eb0b0789451f70e848802140537acce968db97f15a843
|
|
BLAKE2b-256 checksum How to use checksums |
c1296cf7f91a79beb81eab672bbf047b669854bb83be610e99bf9c86da3b1e13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.2.1 CPython/3.12.10 Darwin/24.6.0
|
Release files / pyobjict-2.1.3-py3-none-any.whl
| Download URL | pyobjict-2.1.3-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
12ce79c51aa31aa28b93fb009626ac8c763c1f4c2185343bf84b1f8a3320a46b
|
|
BLAKE2b-256 checksum How to use checksums |
b7c200ea20e9104a1db76bb288eeb4b1f9d0c20accf5014e290a99bcdbc41fba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.2.1 CPython/3.12.10 Darwin/24.6.0
|