A smarter python dictionary
Project description
Smarter PYthon Dicts (SPYD)
SPYD introduces a new SmartDict object, which has several features that the builtin python dict needs
Table of Contents
Installation
You can install the library via pip:
pip install spyd
You can also find the source code here
Usage
After importing the library:
from spyd import SmartDict
You can just create a SmartDict as such:
my_dict = SmartDict()
If you wish to convert an existing dict into a SmartDict, or you wish to assign values during creation, you can do it as such:
my_dict = SmartDict({"name": "Jeff"})
or:
normal_dict = {"name": "Jeff"}
my_dict = SmartDict(normal_dict)
One awesome feature of SmartDicts is dot notation, it works the same as bracket notation, but easier!
Fortunately, SmartDict accepts both:
my_dict = SmartDict()
# bracket notation
my_dict["hello"] = "world"
# dot notation
my_dict.hello = "world"
SmartDicts also automatically create nested SmartDicts when using dot notation:
my_dict = SmartDict()
my_dict.data.name = "Jeff"
In the above example, "data" would be created as a SmartDict within the original SmartDict. Returns:
SmartDict{"data": SmartDict{"name": "Jeff"}}
With SmartDicts you can also easily convert to and from JSON and YAML! Converting a SmartDict to a JSON format is easy:
my_dict = SmartDict()
my_dict.data.name = "test"
my_dict.to_json()
The JSON formatted SmartDict can be applied to a JSON like this:
my_dict = SmartDict()
my_dict.data.name = "test"
with open("data.json", "w") as file:
file.write(my_dict.to_json())
The above examples also apply to YAML.
You can also convert JSON and YAML data into SmartDict:
my_dict = SmartDict.from_json()
Here you don't have to create a new instance of SmartDict as you're only accessing a static method.
from_json() only parameter is formatted JSON or YAML data, which you can get like this:
with open("my_file.json", "r") as file:
my_dict = SmartDict.from_json(file.read())
Once again, the above examples also count for YAML files.
You can also merge a dict into an existing SmartDict:
my_dict = SmartDict()
my_dict.name = "Jeff"
my_dict.hello = "World!"
other_dict = {"hello": "world"}
my_dict.merge(other_dict, overwrite = False)
merge simply appends all values from other_dict into the existing SmartDict, if the overwrite paramter is true, any same key value from the existing SmartDict will be replaced with the new dict's value.
You can also flatten a SmartDict, essentially removing any nesting:
my_dict = SmartDict()
my_dict.data.name = "test"
my_dict.other.address = "321 idk street"
print(my_dict.flatten())
Returns:
{'data.name': 'test', 'other.address': '321 idk street'}
You can also find keys by looking up values:
my_dict = SmartDict()
my_dict.name = "Jeff"
my_dict.hello = "World!"
print(my_dict.reverse_lookup("World!"))
The above print would return "hello" as it is the first occuring key with the value "World!"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spyd-0.1.0.tar.gz.
File metadata
- Download URL: spyd-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f54fb9f0fb4356138505df347f85e2ef1feb47fbfba5bf94a3581d1b7bd73db
|
|
| MD5 |
54e2907c5f32e2140734d442227edea4
|
|
| BLAKE2b-256 |
6052cd780514a6d4d8a4ab9b1b4ee790b37d129021d80aed8662dc6a789f0492
|
File details
Details for the file SPYD-0.1.0-py3-none-any.whl.
File metadata
- Download URL: SPYD-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07d5a5f32462d38df8b37da3456e41a32cda0e471c133f07e155803a7a3f3130
|
|
| MD5 |
7e6ffbfbaf4242369b4f67b7aad7b89d
|
|
| BLAKE2b-256 |
240b41845c5dc6d0a0765ea8dd348444b0572533028ea0c6cb6f12dd5d0aaaf0
|