a string-based refdict
Project description
SmartDict
Usage
- String as references of another item
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
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.0.7.tar.gz
(3.4 kB
view details)
File details
Details for the file smartdict-0.0.7.tar.gz
.
File metadata
- Download URL: smartdict-0.0.7.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45882c10a8d080d26486c7c5ab5201158690db4f76e18020162836c9cccea917 |
|
MD5 | 42f97b918ca0b1d4acb739e2aec30dc4 |
|
BLAKE2b-256 | 24f177cf0c44716728e6aedaf139cc1ade97f52e693357f5d5ccec57334f4f07 |