A reference resolver for nested dictionaries that supports default values, partial references, and circular dependency detection.
Project description
SmartDict
Usage
- String as references of another item
Updates
v0.1.0
- ChatGPT is used to refine the codes, making it more readable and maintainable.
- New Feature: reference supports default value!
${key:default_value}
Install
pip install smartdict
Description
Normal String-based Referencing ${}
import smartdict
data = {
"dataset": "spotify",
"load": {
"base_path": "~/data/${dataset}",
"train_path": "${load.base_path}/train",
"dev_path": "${load.base_path}/dev",
"test_path": "${load.base_path}/test",
},
"network": {
"num_hidden_layers": 3,
"num_attention_heads": 8,
"hidden_size": 64,
},
"store": "checkpoints/${dataset}/${network.num_hidden_layers}L${network.num_attention_heads}H/"
}
data = smartdict.parse(data)
print(data['load']['base_path']) # => ~/data/spotify
print(data['load']['dev_path']) # => ~/data/spotify/dev
print(data['store']) # => checkpoints/spotify/3L8H/
# feel free to use oba.Obj
from oba import Obj
data = Obj(data)
print(data.load.base_path) # => ~/data/spotify
print(data.load.dev_path) # => ~/data/spotify/dev
print(data.store) # => checkpoints/spotify/3L8H/
Full-Match Referencing ${}$
import oba
import smartdict
data = dict(
a='${b.v.1}+1', # normal referencing
b='${c}$', # full-match referencing, supported by smartdict>=0.0.4
c=dict(
l=23,
v=('are', 'you', 'ok'),
)
)
data = smartdict.parse(data)
print(data['b']) # => {'l': 23, 'v': ('are', 'you', 'ok')}
data = oba.Obj(data)
print(data.a) # => you+1
print(data.b.l) # => 23
Fancy Class Referencing
import smartdict
import random
import string
class Rand(dict): # if you want to further be JSON stringify, please derive your class from dict, list, etc.
"""
get random string of n length by Rand()[n]
"""
chars = string.ascii_letters + string.digits
def __init__(self):
super(Rand, self).__init__({})
def __getitem__(self, item):
return ''.join([random.choice(self.chars) for _ in range(int(item))])
def __contains__(self, item):
return True
data = dict(
filename='${utils.rand.4}', # fancy referencing, supported by smartdict>=0.0.4
utils=dict(
rand=Rand(),
)
)
data = smartdict.parse(data)
print(data['filename']) # => toXE (random string with length 4)
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
smartdict-0.3.2.tar.gz
(7.9 kB
view details)
File details
Details for the file smartdict-0.3.2.tar.gz.
File metadata
- Download URL: smartdict-0.3.2.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
473148c9f0319db3c386ec9d25526e76c99743a89b19ea580e953e32e519c5aa
|
|
| MD5 |
1593d7a452ec91905e7bf0a2164a0330
|
|
| BLAKE2b-256 |
641dbd1bd12505b421d875abc7dbf486ca4360237799483f5d8f82d936cc3e1a
|