Name | smartdict JSON |
Version |
0.2.1
JSON |
| download |
home_page | https://github.com/Jyonn/smartdict |
Summary | A reference resolver for nested dictionaries that supports default values, partial references, and circular dependency detection. |
upload_time | 2025-01-06 05:34:07 |
maintainer | None |
docs_url | None |
author | Jyonn Liu |
requires_python | None |
license | MIT Licence |
keywords |
dict
reference
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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 `${}`
```python
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 `${}$`
```python
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
```python
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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Jyonn/smartdict",
"name": "smartdict",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "dict, reference",
"author": "Jyonn Liu",
"author_email": "i@6-79.cn",
"download_url": "https://files.pythonhosted.org/packages/89/7f/a578c0becd3b6ee74981141cc4b3e26924c688d8b7c54010d9430a1e5335/smartdict-0.2.1.tar.gz",
"platform": "any",
"description": "# SmartDict\n\n\n## Usage\n\n- String as references of another item\n\n## Updates\n\n### v0.1.0\n\n- ChatGPT is used to refine the codes, making it more readable and maintainable.\n- New Feature: reference supports default value! `${key:default_value}`\n\n## Install \n\n`pip install smartdict`\n\n## Description\n\n### Normal String-based Referencing `${}`\n\n```python\nimport smartdict\n\ndata = {\n \"dataset\": \"spotify\",\n \"load\": {\n \"base_path\": \"~/data/${dataset}\",\n \"train_path\": \"${load.base_path}/train\",\n \"dev_path\": \"${load.base_path}/dev\",\n \"test_path\": \"${load.base_path}/test\",\n },\n \"network\": {\n \"num_hidden_layers\": 3,\n \"num_attention_heads\": 8,\n \"hidden_size\": 64,\n },\n \"store\": \"checkpoints/${dataset}/${network.num_hidden_layers}L${network.num_attention_heads}H/\"\n}\n\ndata = smartdict.parse(data)\nprint(data['load']['base_path']) # => ~/data/spotify\nprint(data['load']['dev_path']) # => ~/data/spotify/dev\nprint(data['store']) # => checkpoints/spotify/3L8H/\n\n# feel free to use oba.Obj\n\nfrom oba import Obj\ndata = Obj(data)\nprint(data.load.base_path) # => ~/data/spotify\nprint(data.load.dev_path) # => ~/data/spotify/dev\nprint(data.store) # => checkpoints/spotify/3L8H/\n```\n\n### Full-Match Referencing `${}$`\n\n```python\nimport oba\nimport smartdict\n\ndata = dict(\n a='${b.v.1}+1', # normal referencing\n b='${c}$', # full-match referencing, supported by smartdict>=0.0.4 \n c=dict(\n l=23,\n v=('are', 'you', 'ok'),\n )\n)\n\ndata = smartdict.parse(data)\nprint(data['b']) # => {'l': 23, 'v': ('are', 'you', 'ok')}\n\ndata = oba.Obj(data)\nprint(data.a) # => you+1\nprint(data.b.l) # => 23\n```\n\n### Fancy Class Referencing\n\n```python\nimport smartdict\nimport random\n\nimport string\n\n\nclass Rand(dict): # if you want to further be JSON stringify, please derive your class from dict, list, etc. \n \"\"\"\n get random string of n length by Rand()[n]\n \"\"\"\n chars = string.ascii_letters + string.digits\n\n def __init__(self):\n super(Rand, self).__init__({})\n\n def __getitem__(self, item):\n return ''.join([random.choice(self.chars) for _ in range(int(item))])\n \n def __contains__(self, item):\n return True\n\n\ndata = dict(\n filename='${utils.rand.4}', # fancy referencing, supported by smartdict>=0.0.4\n utils=dict(\n rand=Rand(),\n )\n)\ndata = smartdict.parse(data)\n\n\nprint(data['filename']) # => toXE (random string with length 4)\n```\n\n",
"bugtrack_url": null,
"license": "MIT Licence",
"summary": "A reference resolver for nested dictionaries that supports default values, partial references, and circular dependency detection.",
"version": "0.2.1",
"project_urls": {
"Homepage": "https://github.com/Jyonn/smartdict"
},
"split_keywords": [
"dict",
" reference"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "897fa578c0becd3b6ee74981141cc4b3e26924c688d8b7c54010d9430a1e5335",
"md5": "d8e436c69a6659d6f3e344bd8b6bbbd6",
"sha256": "13515c4d8e4b908295004b2976a786842d4802c0404f56416b6ad1ecccc695a9"
},
"downloads": -1,
"filename": "smartdict-0.2.1.tar.gz",
"has_sig": false,
"md5_digest": "d8e436c69a6659d6f3e344bd8b6bbbd6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4892,
"upload_time": "2025-01-06T05:34:07",
"upload_time_iso_8601": "2025-01-06T05:34:07.741288Z",
"url": "https://files.pythonhosted.org/packages/89/7f/a578c0becd3b6ee74981141cc4b3e26924c688d8b7c54010d9430a1e5335/smartdict-0.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-06 05:34:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Jyonn",
"github_project": "smartdict",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "smartdict"
}