Skip to main content

add reference for dict in python using formatted string

Project description

refdict

在Python使用refdict更好地组织数据

使用data['key.key.key.1.2.3']

实现data['key']['key']['key'][1][2][3]的效果

兼容dict/list/tuple/string!

Usage

在字符串值前面加上引用前缀使其变成另一个对象的引用。默认的引用前缀是@。可以在构造函数中传入参数refPrefix来改变引用前缀

[]运算符中使用一个字符串实现refdict内容的链式访问。使用.作为多个key的连接符。可以在构造函数传入参数divider来改变分隔符

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和其他支持[]运算符的数据类型
    • 如果使用默认的分隔符.,那么dict的key中不能出现.
    • list/tuple/str之外的类型在解析key的时候作为str解析
  • 可以在value中使用.连接属性
    • rd['player.items.1'] = '@potion.red' = 'restore your health by 20%'
  • 链式解析
    • 因为rd['player.weapon'] = '@sword',所以rd['player.attack'] = '@player.weapon.attack' = '@sword.attack' = 123
  • 赋值操作。如果被赋值的对象是引用字符串,则仅解引用,不删除引用目标
    • rd['player.items.1'] = 'empty'解除了原本其指向apple的引用并赋值为empty,但是并没有删除apple
  • 切片取值与赋值。对于list/tuple/str可以使用[:]进行切片
    • rd['player.items.:.1'] = ['@apple', 'empty'][:][1] = ['@apple', 'empty'][1] = 'empty'
  • 使用text函数取字面值。text函数正常计算前n-1个key,然后直接返回最后一个key对应的字面值
    • rd.text('player.me.attack')计算rd['player.me']得到player对象,attack作为最后一个key直接返回字面值@player.weapon.attack

Warnings

使用形如item: @item的递归引用会导致死循环,包括间接递归引用

data = {
	'item': '@item', # => infinite loop!
	'wrapper': {
		'item': '@wrapper.item' # => infinite loop!
	},
	'a': '@b' # => infinite loop!
	'b': '@a' # => infinite loop!
}

Change Log

v2.0.0

改名。还是小写看起来舒服。但是不向前兼容了所以就用2.0.0的版本号好了

v1.0.0

  • 实现[]取值与赋值
  • 实现text函数以获得字面值

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-2.0.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

refdict-2.0.1-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: refdict-2.0.1.tar.gz
  • Upload date:
  • Size: 3.5 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

Hashes for refdict-2.0.1.tar.gz
Algorithm Hash digest
SHA256 22c65a2103962cf47a0030686c3c109738a9d6c4950d2ae5b927b3dd20edca56
MD5 0b774cfcc4f18d6c3a4775da573b7c75
BLAKE2b-256 db323b16e8c3c3866d02840d87ed2950bfdf5a89edd4ece40174c4c707681fe2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: refdict-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.9 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

Hashes for refdict-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eabbb11e28dcc07565b9367fdc9d894d30eb6aa9a91707a772cb7965a0f8ecd9
MD5 c61815f80de1362102da836e18b29107
BLAKE2b-256 9303192b95404dbc863ca6ed202690ffebf52941c08091490d5591078ed5c059

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