{"id":15942230,"url":"https://github.com/discretetom/refdict","last_synced_at":"2025-03-30T04:22:14.871Z","repository":{"id":57460812,"uuid":"187782001","full_name":"DiscreteTom/refdict","owner":"DiscreteTom","description":"Using string as chain keys to realize chain access and reference access of dict in python.","archived":false,"fork":false,"pushed_at":"2019-07-27T03:19:09.000Z","size":200,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-05T06:38:58.182Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pypi.org/project/refdict/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DiscreteTom.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-21T07:12:17.000Z","updated_at":"2019-08-14T08:22:33.000Z","dependencies_parsed_at":"2022-09-17T04:30:39.908Z","dependency_job_id":null,"html_url":"https://github.com/DiscreteTom/refdict","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DiscreteTom%2Frefdict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DiscreteTom%2Frefdict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DiscreteTom%2Frefdict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DiscreteTom%2Frefdict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DiscreteTom","download_url":"https://codeload.github.com/DiscreteTom/refdict/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246274619,"owners_count":20751109,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-07T07:40:43.248Z","updated_at":"2025-03-30T04:22:14.841Z","avatar_url":"https://github.com/DiscreteTom.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# refdict\n\n![PyPI](https://img.shields.io/pypi/v/refdict.svg)\n![GitHub](https://img.shields.io/github/license/DiscreteTom/refdict.svg)\n\n## Usage\n\n- Using string as chain keys to realize chain access(including `dict`/`list`/`tuple`/`str`).\n- String can perform as a reference of another item.\n\n![](https://raw.githubusercontent.com/DiscreteTom/refdict/master/img/readme.png)\n\n## Install\n\n`pip install refdict`\n\n## Features\n\n- Using `refdict` just like using `dict`/`list`/`tuple`/`str`.\n  - Methods of built-in types: `refdict({}).keys()` or `refdict([]).append(123)`.\n  - Iteration and containment check: `for x in refdict([1, 2, 3])`.\n  - Slice and slice assignment: `refdict([1, 2, 3])[:] =\u003e [1, 2, 3]`.\n  - ...\n- Chain accessing members using a string.\n  - `refdict({'1':{'1':{'1':{'1':'1'}}}})['1.1.1.1'] =\u003e 1`.\n- Using reference string to reference another item.\n\n## Description\n\nUse **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.\n\nThe `[]` 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.\n\nHere is an example:\n\n```python\ndata = {\n\t'player': {\n\t\t'name': 'DiscreteTom',\n\t\t'items': [\n\t\t\t'@potion.red'\n\t\t],\n\t\t'weapon': '@sword',\n\t\t'attack': '@player.weapon.attack',\n\t\t'me': '@player'\n\t},\n\t'potion': {\n\t\t'red': 'restore your health by 20%',\n\t},\n\t'sword': {\n\t\t'attack': 123\n\t},\n}\nrd = refdict(data)\nprint(rd['player.items.0']) # =\u003e restore your health by 20%\nprint(rd['player.attack']) # =\u003e 123\nprint(rd['player.items.:']) # =\u003e ['@potion.red']\nprint(rd.text('player.attack')) # =\u003e @player.weapon.attack\nprint('player.weapon' in rd) # =\u003e True\n```\n\n## Warnings\n\n**Recursive references** like `item: @item` will cause **infinite loop**, including indirect reference.\n\n```python\ndata = {\n\t'item': '@item', # =\u003e infinite loop!\n\t'wrapper': {\n\t\t'item': '@wrapper.item' # =\u003e infinite loop!\n\t},\n\t'a': '@b' # =\u003e infinite loop!\n\t'b': '@a' # =\u003e infinite loop!\n}\n```\n\n## FAQ\n\n- Q - Why I can't access the first item of `list`/`tuple`/`str` using `[1]`?\n  - 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.\n- Q - Why I got `KeyError` when using `rd['key1.key2']['key3.key4']`?\n  - 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_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiscretetom%2Frefdict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiscretetom%2Frefdict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiscretetom%2Frefdict/lists"}