Skip to main content

一个便捷的访问多层级dict的方法

Project description

Example Package

一个便捷的访问多层级dict的方法

像操作类属性一样访问dict的属性,支持多级访问

针对场景:

针对复杂的多级dict的属性访问,如 user_info = {"name": "xxx", "address": {"city": "xxx", "country": "xxx"}}

老的方式:

user_info = {"name": "xxx", "address": {"city": "xxx", "country": "xxx"}}

# 方式一 (当user_info没有address或address不为dict时,后面get会报错):
user_info.get("address").get("city")
# 方式二(当user_info没有address或address不为dict时,后面get会报错):
user_info['address']['city']

# 方式三 (增加多级判断)
(user_info.get("address") or {}).get("city")

使用amaze_dict:

from amaze_dict import wrap_value
user_dict = {"name": "xiaoming", "age": 30, "address": {"city": "beijing", "country": "china"}}
user_amaze_dict = wrap_value(user_dict)
print(user_amaze_dict.address.city)
>> output: beijing
print(user_amaze_dict.address.country)
>> output: china
print(user_amaze_dict.contact.phone_num)
>> output: <amaze_dict.amaze_dict.LB_None object at 0x7f8664882cd0>

多级访问直接用属性递进查找,不受中间属性没有影响。 可直接对需要的属性进行访问和判断。

使用方法

from amaze_dict import wrap_value

user_dict = {"name": "xiaoming", "age": 30, "address": {"city": "beijing", "country": "china"}}
user_amaze_dict = wrap_value(user_dict)

访问一级属性:

>>> print(user_amaze_dict.name)
xiaoming
>>>>print(user_amaze_dict.age)
30

访问多级属性:

>>> print(user_amaze_dict.address.city)
beijing
>>>>print(user_amaze_dict.address.country)
china

条件判断:

判断属性是否存在:
>>> if user_amaze_dict.address.community:
...     print(user_amaze_dict.address.city)
beijing

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

amaze-dict-0.0.4.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

amaze_dict-0.0.4-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file amaze-dict-0.0.4.tar.gz.

File metadata

  • Download URL: amaze-dict-0.0.4.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.2 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.5.9

File hashes

Hashes for amaze-dict-0.0.4.tar.gz
Algorithm Hash digest
SHA256 6e0335db42bf9e8abe27ff6440a64e3dcf566ee6058506aa424a13c463e033b6
MD5 acebbb85a4e4b2b8792c4779b2d356a0
BLAKE2b-256 d971340eb1f41f77f72c63155ae4636c19cfa4d12cc8e7688e3c72aa702ba18b

See more details on using hashes here.

File details

Details for the file amaze_dict-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: amaze_dict-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.2 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.5.9

File hashes

Hashes for amaze_dict-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6ea2e95df64e0f6551c8ec0645d9f71af002e4a629312b37cda6be5c968d7513
MD5 4ee0d16d63ee9ccccee883040d3ce10d
BLAKE2b-256 364989c356d215e3d0b93e560fa23292173e39ab36dae0f856798f402e0b81cb

See more details on using hashes here.

Supported by

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