Skip to main content
https://img.shields.io/pypi/v/yydict.svg https://img.shields.io/pypi/dm/yydict.svg

YYDict

YYDict can convert a normal dictionary into a property dictionary. YYDict is a module that exposes a dictionary subclass that allows items to be set like attributes. Values are gettable and settable using both attribute and item syntax.

YYDict 可以将普通字典转换为属性字典。 YYDict 是一个模块,它公开了一个dictionary子类,允许像设置属性一样设置项。 使用属性和项语法,值是可获取和可设置的。

INSTALL

YYDict 安装也非常方便,只要使用pip 工具即可.

pip install yydict

USAGE

HOW TO USE THE YYDict ?

YYDict 使用起来非常方便,可以使用 obj.xxx 来访问字典中对应的key , 对于字典中的key 可以通过访问属性的方式进行访问.

from yydict import YYDict

>>> data = {'foo': 3, 'bar': {'x': 1, 'y': 2}}
... d = YYDict(data)
>>>
>>> d.foo
3
>>> d.bar
{'x': 1, 'y': 2}
>>> d.bar.x
1
>>> d.bar.y
2
>>> d.aaa
Traceback (most recent call last):
...
AttributeError: 'YYDict' object has no attribute 'aaa'

>>> # 转成 常规的词典
>>> regular  = d.to_dict()
>>> regular
{'foo': 3, 'bar': {'x': 1, 'y': 2}}
>>> data
{'foo': 3, 'bar': {'x': 1, 'y': 2}}
>>> data  == regular
True
>>> data is regular
False
>>> type(data),type(regular)
(<class 'dict'>, <class 'dict'>)


>>> d = YYDict({'foo':3})
>>> d['foo']
3
>>> d.foo
3
>>> d.bar
Traceback (most recent call last):
...
AttributeError: 'YYDict' object has no attribute 'bar'


Works recursively
>>> d = YYDict({'foo':3, 'bar':{'x':1, 'y':2}})
>>> isinstance(d.bar, dict)
True
>>> d.bar.x
1

Bullet-proof

>>> YYDict({})
{}
>>> YYDict(d={})
{}
>>> YYDict(None)
{}
>>> d = {'a': 1}
>>> YYDict(**d)
{'a': 1}
>>> YYDict((('a', 1), ('b', 2)))
{'a': 1, 'b': 2}

Set attributes

>>> d = YYDict()
>>> d.foo = 3
>>> d.foo
3
>>> d.bar = {'prop': 'value'}
>>> d.bar.prop
'value'
>>> d
{'foo': 3, 'bar': {'prop': 'value'}}
>>> d.bar.prop = 'newer'
>>> d.bar.prop
'newer'


>>> # Values extraction
>>> d = YYDict({'foo':0, 'bar':[{'x':1, 'y':2}, {'x':3, 'y':4}]})
>>> isinstance(d.bar, list)
True
>>> from operator import attrgetter
>>> list(map(attrgetter('x'), d.bar))
[1, 3]
>>> list(map(attrgetter('y'), d.bar))
[2, 4]
>>> d = YYDict()
>>> list(d.keys())
[]
>>> d = YYDict(foo=3, bar=dict(x=1, y=2))
>>> d.foo
3
>>> d.bar.x
1

Still like a dict though

>>> o = YYDict({'clean':True})
>>> list(o.items())
[('clean', True)]

And like a class

>>> class Flower(YYDict):
...     power = 1
...     mean = {}
...     color = {"r": 100, "g": 0, "b": 0}
...
>>> f = Flower()
>>> f.power
1
>>> f.color.r
100
>>> f.mean.x = 10
>>> f.mean.x
10
>>> f = Flower({'height': 12})
>>> f.height
12
>>> f['power']
1
>>> sorted(f.keys())
['color', 'height', 'mean', 'power']

update and pop items
>>> d = YYDict(a=1, b='2')
>>> e = YYDict(c=3.0, a=9.0)
>>> d.update(e)
>>> d.c
3.0
>>> d['c']
3.0
>>> d.get('c')
3.0
>>> d.update(a=4, b=4)
>>> d.b
4
>>> d.pop('a')
4
>>> d.a
Traceback (most recent call last):
...
AttributeError: 'YYDict' object has no attribute 'a'

Similar tools

CHANGELOG

0.0.1 (2023-12-09)

  • first version

0.0.2 (2023-12-21)

  • fix setting not allow setting keys,values problem.

For more info check out the README at https://gitee.com/changyubiao/yydict.

Metadata

Release files for yydict 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yydict 0.0.2
File Size Uploaded
yydict-0.0.2.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for yydict 0.0.2
File Interpreter ABI Platform
yydict-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / yydict-0.0.2.tar.gz

Download URL yydict-0.0.2.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ee372c066c2e735022f42b20555c786d7336104f6d7e10eeaf09f33cb41851c1
BLAKE2b-256 checksum
How to use checksums
c791909ce921c77fc7b256eaab64f541cd479113f0292d350603b71b5d348273
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / yydict-0.0.2-py3-none-any.whl

Download URL yydict-0.0.2-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
238b26a25c795eac3b873fed1b8ffb02dd9d8029ef1b98e7c82273dab21fc1bf
BLAKE2b-256 checksum
How to use checksums
dacf1a716ed03e0d34deda5b4044cca43f1b0b1f0de4d6e18a92522d0cb83eff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page