add reference for dict in python using formatted string
Project description
refdict
在Python使用refdict更好地组织与访问数据
兼容dict/list/tuple/str!
Install
pip install refdict
Usage
在字符串值前面加上引用前缀使其变成另一个对象的引用。默认的引用前缀是@。可以在构造函数中传入参数refPrefix来改变引用前缀
在[]运算符中使用一个字符串实现refdict内容的链式访问。使用.作为多个key的连接符。可以在构造函数传入参数separator来改变分隔符
data = {
'player': {
'name': 'DiscreteTom',
'items': [
'@apple',
'@potion.red'
],
'weapon': '@sword',
'attack': '@player.weapon.attack',
'me': '@player'
},
'apple': 'restore your health by 10%',
'potion': {
'red': 'restore your health by 20%',
},
'sword': {
'attack': 123,
'value': 50
},
}
rd = refdict(data)
print(rd['player.items.1']) # => restore your health by 20%
print(rd['player.attack']) # => 123
rd['player.items.1'] = 'empty'
print(rd['player.items.1']) # => empty
print(rd['player.items.:.1']) # => empty
rd['player.items.:'] = []
print(rd['player.items']) # => []
print(rd.text('player.me.attack')) # => @player.weapon.attack
Features
- 能够像使用
dict/list/tuple/str一样使用refdict- 能够调用对应类型的函数,如
refdict({}).keys()或refdict([]).append(123) - 迭代与成员判断
for x in refdict([1, 2, 3]) - 切片与切片赋值
refdict([1, 2, 3])[:] => [1, 2, 3] - ...
- 能够调用对应类型的函数,如
- 能够通过一个字符串链式访问内部成员
refdict({'1':{'1':{'1':{'1':'1'}}}})['1.1.1.1'] => 1
- 能够通过引用字符串实现对象与对象之间的互相引用
Warnings
使用形如item: @item的递归引用会导致死循环,包括间接递归引用
data = {
'item': '@item', # => infinite loop!
'wrapper': {
'item': '@wrapper.item' # => infinite loop!
},
'a': '@b' # => infinite loop!
'b': '@a' # => infinite loop!
}
FAQ
- Q - 为什么我用1作为下标访问不到list/tuple/str的第一个元素?
- A - 和python一样,下标从0开始计数。虽然用起来有时候感觉有些反直觉,但是是合理的
- Q - 为什么我不能像
refdict[1][2][3]这样使用引用的特性?- A - 引用解析仅限于第一个
[],第一个[]会返回一个正常的tuple/list/dict/str而不是一个refdict对象,所以后面的操作无法使用引用特性
- A - 引用解析仅限于第一个
Change Log
v2.1.0
- 可以调用根数据类型的非内置函数
- 比如
refdict({})可以使用dict的keys函数,refdict([])可以使用list的append函数
- 比如
[]运算的参数keys添加int类型和slice类型的支持,以便更好地访问list/tuple/str。目前参数keys仅支持str、int和slice三种类型- 实现
__contains__ - 实现
__str__以便输出 - 实现
__delitem__以使用引用解析来删除元素
v2.0.0
改名。还是小写看起来舒服。但是不向前兼容了所以就用2.0.0的版本号好了
v1.0.0
- 实现基本的
[]取值与赋值 - 实现
text函数以获得字面值
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 refdict-2.1.0.tar.gz.
File metadata
- Download URL: refdict-2.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
febac44c612a677f25088b29b305869f0a0ed066ae95b56a8bd5cf9192badfdf
|
|
| MD5 |
8257b72f31c1c158cd5c194a5b100209
|
|
| BLAKE2b-256 |
2c7887fe0a67e24da7a5d770b8556246b21dc03bce8d40118180221d0d5581f7
|
File details
Details for the file refdict-2.1.0-py3-none-any.whl.
File metadata
- Download URL: refdict-2.1.0-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0c9839c946d34643a503e2a462004e2c4770d3e5ce40172c34a7da756ff8db6
|
|
| MD5 |
d082849ae07e81c1e871d9c1ac1d4bce
|
|
| BLAKE2b-256 |
276d6a6df654bcb3f67d265d60f891bfa3e38649e824ecab5e03cf031f3ca502
|