Skip to main content

A custom wrapper object around dict that allows attribute-style access to dictionary items and support for nested JSON data.

Project description

objdict-bf

objdict-bf is a Python module that provides a wrapper class for conveniently manipulating dictionaries or dict-based JSON nested structures using attribute-like syntax. It is intended mostly to ease manipulation of JSON data, web requests responses, configuration files...

Features

  • Attribute-style access to dictionary items (e.g., obj.key instead of obj['key']).
  • Synchronization with the original dictionary if passed at instantiation.
  • Utility methods for recursive conversion of nested structures to and from objdict and dict.
  • JSON serialization and deserialization methods for both strings and files.

Installation

pip install objdict-bf

Usage

Here's an example of how to use the objdict wrapper:

from objdict_bf import objdict

# Create an objdict with some initial data
data = objdict(
    name='John',
    age=30,
    location='New York'
)

#Or synchronize with an existing dict
d={'name': 'John', 'age': 30, 'location': 'New York'}
data = objdict(d)

# Access data using attribute-style access
print(data.name)  # Output: John
print(data.age)   # Output: 30

# Modify data
data.age = 31

#Changes are reflected on the original dict
print(d['age']) #Ouput: 31

#Support for nested structures involving lists
d={
    'profile':{
        'name':'John',
        'hobbies':[
            {'type':'sport','title':'tennis'},
            {'type':'music','title':'guitar playing'}
        ]
    }
}
data = objdict(d)

print(data.profile.hobbies[1].title) #Output: guitar playing

#Conversion of dict items to their objdict version is automatic.
#The objdict being essentially a wrapper interface on the initial dict,  
#this conversion is reflected in the initial dict content as well

print(isinstance(data.profile.hobbies[1],objdict)) #Output: True
print(isinstance(d['profile']['hobbies'][1],objdict)) #Output: True

#to_dict returns the underlying dict, converting recursively all objdicts found in the nested structure back to dicts
print(d is data.to_dict()) #Ouptut: True
print(isinstance(d['profile']['hobbies'][1], dict) #Output: True 

# Serialize to JSON string
json_string = data.dumps()

#dump to a JSON file
data.dump("my_json_file.json")

#Or set a file reference and dump (the reference to the file is kept in the objdict instance)
data.set_json_file("my_json_file.json")
data.dump()

# Deserialize from JSON string (creates a new instance)
data = objdict.loads(json_string)

# Deserialize from a JSON file (new instance keeping reference to the json file)
data = objdict.load("my_json_file.json")

#update data
data.email="dummy.email@gmail.com"
data.user="dummy_username"

#dump changes to 'my_json_file.json'
data.dump()

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

objdict_bf-0.1.3.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

objdict_bf-0.1.3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file objdict_bf-0.1.3.tar.gz.

File metadata

  • Download URL: objdict_bf-0.1.3.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for objdict_bf-0.1.3.tar.gz
Algorithm Hash digest
SHA256 0e0b2bdda1b6a5e2148e39ca59e2909517f9be162eec015ef3640d0286db15f4
MD5 7989e8d3bda5a767eee05aa50a535238
BLAKE2b-256 2de7e702d1fbe0d966d53bfe414b51352473a80126578fe30ba0ce44ec22432e

See more details on using hashes here.

File details

Details for the file objdict_bf-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: objdict_bf-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for objdict_bf-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2073139a4898733d0f1afaebe023a24d6327ac4efd4b458c88339ebb3683b518
MD5 b2e74b03bfeb60ef917848a28159f3dc
BLAKE2b-256 3cee78950e492f6742ee2989ab26a4858c0d8a33b848a2a588069914b4deeb6e

See more details on using hashes here.

Supported by

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