Skip to main content

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对象。可以使用refdict(1)(2)[3]这样使用引用特性与链式访问,因为()会返回一个临时的子refdict对象

Change Log

v3.1.0

  • 添加函数refdict.get(keys, default = None)实现类似于dict.get(key, default)的功能,但是refdictkeys可以是链式的
  • 修复refdict.__str__()
  • 修复refdict.__repr__()

v3.0.0

  • 添加静态函数findItem()使非refdict对象也可以使用引用与链式访问的功能
    • refdict.findItem(data, 'key1.key2')
  • 添加多次链式访问的实现方案,使用()实现
    • rd('player.me')会返回一个子refdict,里面包含父refdict的所有数据,但是自身表示了父refdict的部分数据。比如rd('player.me')包含整个rd的数据,但是它仅表示player
    • 可以链式使用(),且参数可以是链式的,如rd('player.me')('me.me.me')
    • 最后使用[]返回非refdict的对象完成取值,如rd('player.me')('me.me')['attack'] = 0
  • 为了实现上述“局部结果”的功能,成员变量data变为refdictprivate内容,使refdict不向前兼容

v2.2.0

  • __contains__支持链式访问特性
    • 'player.weapon.attack' in rd会返回True

v2.1.0

  • 可以调用根数据类型的非内置函数
    • 比如refdict({})可以使用dictkeys函数,refdict([])可以使用listappend函数
  • []运算的参数keys添加int类型和slice类型的支持,以便更好地访问list/tuple/str。目前参数keys仅支持strintslice三种类型
  • 实现__contains__
  • 实现__str__以便输出
  • 实现__delitem__以使用引用解析来删除元素

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-3.1.0.tar.gz (5.3 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.1.0-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: refdict-3.1.0.tar.gz
  • Upload date:
  • Size: 5.3 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.1.0.tar.gz
Algorithm Hash digest
SHA256 6a731d25f4a27ce93e7c395c1b5234599ffa415521dee4b1459f932056e61969
MD5 6649d3115a54644b6e7fada28fe4f8a4
BLAKE2b-256 baf78fff993e0f02bf4e5c588aa89174a432ef4e6802f431d5b8579f747a32c3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: refdict-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6ab166309f025914e18426f5619c8b2eafa17bfe50912ae0c8f89926634d7a09
MD5 642eed96fc545ace5660bb3906b5f02b
BLAKE2b-256 6d92384e96159c309ffbe9a6bedaea58c37f64b3bcc5f66d799a4dd049d35682

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