Using string as chain keys to realize chain access and reference access of dict in python
Project description
refdict
Usage
- Using string as chain keys to realize chain access(including
dict
/list
/tuple
/str
). - String can perform as a reference of another item.
Install
pip install refdict
Features
- Using
refdict
just like usingdict
/list
/tuple
/str
.- Methods of built-in types:
refdict({}).keys()
orrefdict([]).append(123)
. - Iteration and containment check:
for x in refdict([1, 2, 3])
. - Slice and slice assignment:
refdict([1, 2, 3])[:] => [1, 2, 3]
. - ...
- Methods of built-in types:
- Chain accessing members using a string.
refdict({'1':{'1':{'1':{'1':'1'}}}})['1.1.1.1'] => 1
.
- Using reference string to reference another item.
Description
Use reference prefix to turn a string into a reference of another item. The default reference prefix is @
. Reference prefix can be changed by the parameter refPrefix
of the constructor of refdict.
The []
operator can be used for chain access and reference access. The separator of keys is .
by default, and it can be changed by the parameter separator
of the constructor.
Here is an example:
data = {
'player': {
'name': 'DiscreteTom',
'items': [
'@potion.red'
],
'weapon': '@sword',
'attack': '@player.weapon.attack',
'me': '@player'
},
'potion': {
'red': 'restore your health by 20%',
},
'sword': {
'attack': 123
},
}
rd = refdict(data)
print(rd['player.items.0']) # => restore your health by 20%
print(rd['player.attack']) # => 123
print(rd['player.items.:']) # => ['@potion.red']
print(rd.text('player.attack')) # => @player.weapon.attack
print('player.weapon' in rd) # => True
Warnings
Recursive references like item: @item
will cause infinite loop, including indirect reference.
data = {
'item': '@item', # => infinite loop!
'wrapper': {
'item': '@wrapper.item' # => infinite loop!
},
'a': '@b' # => infinite loop!
'b': '@a' # => infinite loop!
}
FAQ
- Q - Why I can't access the first item of
list
/tuple
/str
using[1]
?- A - Of course you should use
[0]
to access the first item, just like usinglist
/tuple
/str
. It's a little bit counter-intuitive but reasonable.
- A - Of course you should use
- Q - Why I got
KeyError
when usingrd['key1.key2']['key3.key4']
?- The first
[]
operator will return a non-refdict object, so you can't use chain keys in the following[]
. To get a sub-refdict, you should use()
operator, which means you should userd('key1.key2')['key3.key4']
.
- The first
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
File details
Details for the file refdict-3.3.0.tar.gz
.
File metadata
- Download URL: refdict-3.3.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 810fd8cc805633f7bf46b6be0b04f8405a3de159f9de0129a76959ed357b5c76 |
|
MD5 | 77a9ed8b282c935b8a1970d9db38d776 |
|
BLAKE2b-256 | baef01854fed92610a9d88b491d04df4de6d82410b5850036aa0e9ef514a9071 |
File details
Details for the file refdict-3.3.0-py3-none-any.whl
.
File metadata
- Download URL: refdict-3.3.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/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e894c0047411915da74488b922eb16981a30d0ee83f625e5b535c86fc4e9c075 |
|
MD5 | 76ddaa4cc3e5d1ac2137af88cd50729c |
|
BLAKE2b-256 | 5e1f4de580c2bb4b3f5f30950c935e838bf6b10a99fecf15c4e670673ced974d |