Skip to main content

Using string as chain keys to realize chain access and reference access of dict in python

Project description

refdict

PyPI GitHub

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 using dict/list/tuple/str.
    • Methods of built-in types: refdict({}).keys() or refdict([]).append(123).
    • Iteration and containment check: for x in refdict([1, 2, 3]).
    • Slice and slice assignment: refdict([1, 2, 3])[:] => [1, 2, 3].
    • ...
  • 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 using list/tuple/str. It's a little bit counter-intuitive but reasonable.
  • Q - Why I got KeyError when using rd['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 use rd('key1.key2')['key3.key4'].

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

refdict-3.2.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

refdict-3.2.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file refdict-3.2.1.tar.gz.

File metadata

  • Download URL: refdict-3.2.1.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

Hashes for refdict-3.2.1.tar.gz
Algorithm Hash digest
SHA256 658b4a985c94eea0afdfc39bdc831a0f920d4932bffdeb61326979d00e156af8
MD5 0c457de49d84c76bb47d1778afb0631c
BLAKE2b-256 eab31691389ecb5aca2dfd5960bd2f1cd1a1cc6e5826739d5c2f55c2ee42a274

See more details on using hashes here.

File details

Details for the file refdict-3.2.1-py3-none-any.whl.

File metadata

  • Download URL: refdict-3.2.1-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

Hashes for refdict-3.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 22303f15ab87dc90d0ca07e03b9844227d34f584883d12b58b1eb7b23734c3fd
MD5 ec5bfbaf04db6289fe2ce5c30f00ec85
BLAKE2b-256 26fe4c6f4fb83b8fd9420b43cfa0142fe5b1bf4f61855ebc43e57a301714e1ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page