Making Python's `dict` easier to work with by adding object notation and grouping
Project description
EZDict
Making Python's dict
easier to work with by adding object notation and grouping.
- Allows keys to be accessed as attributes, so
ezdict.attr
can be used instead ofezdict["attr"]
. Note, this only works with string keys. Additionally, any keys that overlap with method names will be shadowed, so to access the keykeys
, useezdict["keys"]
instead ofezdict.keys
. - Provide two methods,
incrementer
andappender
, to support the common operations of counting the occurrences of a key and grouping values by a key, respectively.
Installation
pip install ezdict
Import
from ezdict import EZDict
Usage
Accessing Keys as Attributes
from ezdict import EZDict
ez = EZDict({
"id": "5e2797c05aa0585816ce8b8c",
"title": "EZDict",
"description": "Super cool Python package",
"author": "Jacob Morris (BlendingJake)",
"meta": {
"created": "March 27th, 2020"
}
})
print(ez.title) # >>> "EZDict"
print(ez.meta.created) # >>> "March 27th, 2020"
Grouping/Counting Like a Pro
from ezdict import EZDict
# tried of this?
iterable = [ ... ]
grouped = {}
for item in iterable:
if item["key"] in grouped:
grouped[item["key"]] += 1
# OR grouped[item["key"]].append(item)
else:
grouped[item["key"]] = 1
# OR grouped[item["key"]] = [item]
# do this instead
iterable = [ ... ]
grouped = EZDict()
for item in iterable:
grouped.incrementer(item["key"])
# OR grouped.appender(item["key"], item)
incrementer(key, increment=1)
Increment
key
byincrement
if the key has already been seen. Otherwise, set the key equal toincrement
.
appender(key, value)
Append
value
to the list of values associated withkey
if that key was already been seen. Otherwise, set the key equal to[value]
.
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
ezdict-1.0.0.tar.gz
(2.9 kB
view hashes)
Built Distribution
ezdict-1.0.0-py3-none-any.whl
(15.7 kB
view hashes)