用于处理邪恶/复杂数据的人类的python字典。

python-benedict的Python项目详细描述


Build StatuscodecovCodacy BadgeScrutinizer Code QualityRequirements StatusPyPI versionPyPI downloadsPy versionsLicense

Python本尼迪克特

用于处理邪恶/复杂数据的人类的python字典。

功能

  • 完整的keypath支持(默认使用点语法)
  • 许多utilityparse方法根据需要检索数据(下面列出的所有方法)
  • 在返回值(它们接受benedict casting)
  • 简单的I/O操作最常用的格式是:base64jsonquery-stringtomlyamlxml
  • ^ {EM1}$(可以替换现有的DITCS无痛苦)

要求

  • python 2.7、3.4、3.5、3.6、3.7

安装

  • 运行pip install python-benedict

测试

  • 运行tox/python setup.py test

用法

^ {CD11>}是一个^ {< CD1>}子类,因此可以将其用作普通字典^ {EM1}$(您可以只投一个现有的DICT)

导入

frombenedictimportbenedict

初始值

创建新实例:

d=benedict()

…或铸造现有的^ {< CD1> }:

d=benedict(existing_dict)

如果现有的DICT键包含键路径分隔符,将引发A^ {}。

在这种情况下,您应该使用custom keypath separator

键槽

.是默认的密钥路径分隔符。

d=benedict()# set values by keypathd['profile.firstname']='Fabio'd['profile.lastname']='Caccamo'print(d)# -> { 'profile':{ 'firstname':'Fabio', 'lastname':'Caccamo' } }print(d['profile'])# -> { 'firstname':'Fabio', 'lastname':'Caccamo' }# check if keypath exists in dictprint('profile.lastname'ind)# -> True# delete value by keypathdeld['profile.lastname']

自定义键槽分隔符

可以在构造函数中自定义传递keypath_separator参数的keypath分隔符。

d=benedict(existing_dict,keypath_separator='/')

api

键槽方法
  • 关键路径
# Return a list of all keypaths in the dict.d.keypaths()

实用方法

这些方法是常用的工具,可以加快你的日常工作。

  • 清洁
# Clean the current dict removing all empty values: None, '', {}, [], ().# If strings, dicts or lists flags are False, related empty values will not be deleted.d.clean(strings=True,dicts=True,lists=True)
  • 深度复制
# Return a deepcopy of the dict.d.deepcopy()
  • 转储
# Return a readable representation of any dict/list.s=benedict.dump(d.keypaths())print(s)
  • 倾倒物品
# Return a readable representation of the dict for the given key (optional).s=d.dump_items(key=None)print(s)
  • 过滤器
# Return a filtered dict using the given predicate function.# Predicate function receives key, value arguments and should return a bool value.predicate=lambdak,v:visnotNoned.filter(predicate)
  • 移除
# Remove multiple keys from the dict.d.remove(['firstname','lastname','email'])
  • 子集
# Return a dict subset for the given keys.d.subset(['firstname','lastname','email'])

分析方法

这些方法是get方法的包装器,它们解析试图以预期类型返回的数据。

  • 获取布尔值
# Get value by key or keypath trying to return it as bool.# Values like `1`, `true`, `yes`, `on`, `ok` will be returned as `True`.d.get_bool(key,default=False)
  • 获取布尔列表
# Get value by key or keypath trying to return it as list of bool values.# If separator is specified and value is a string it will be splitted.d.get_bool_list(key,default=[],separator=',')
  • 获取日期时间
# Get value by key or keypath trying to return it as datetime.# If format is not specified it will be autodetected.# If options and value is in options return value otherwise default.d.get_datetime(key,default=None,format=None,options=[])
  • 获取日期时间列表
# Get value by key or keypath trying to return it as list of datetime values.# If separator is specified and value is a string it will be splitted.d.get_datetime_list(key,default=[],format=None,separator=',')
  • 获得十进制
# Get value by key or keypath trying to return it as Decimal.# If options and value is in options return value otherwise default.d.get_decimal(key,default=Decimal('0.0'),options=[])
  • 获取十进制列表
# Get value by key or keypath trying to return it as list of Decimal values.# If separator is specified and value is a string it will be splitted.d.get_decimal_list(key,default=[],separator=',')
  • 获取指令
# Get value by key or keypath trying to return it as dict.# If value is a json string it will be automatically decoded.d.get_dict(key,default={})
  • 获取电子邮件
# Get email by key or keypath and return it.# If value is blacklisted it will be automatically ignored.# If check_blacklist is False, it will be not ignored even if blacklisted.d.get_email(key,default='',options=None,check_blacklist=True)
  • 获取浮动
# Get value by key or keypath trying to return it as float.# If options and value is in options return value otherwise default.d.get_float(key,default=0.0,options=[])
  • 获取浮动列表
# Get value by key or keypath trying to return it as list of float values.# If separator is specified and value is a string it will be splitted.d.get_float_list(key,default=[],separator=',')
  • 获取积分
# Get value by key or keypath trying to return it as int.# If options and value is in options return value otherwise default.d.get_int(key,default=0,options=[])
  • 获取内部列表
# Get value by key or keypath trying to return it as list of int values.# If separator is specified and value is a string it will be splitted.d.get_int_list(key,default=[],separator=',')
  • 获取列表
# Get value by key or keypath trying to return it as list.# If separator is specified and value is a string it will be splitted.d.get_list(key,default=[],separator=',')
  • 获取列表项
# Get list by key or keypath and return value at the specified index.# If separator is specified and list value is a string it will be splitted.d.get_list_item(key,index=0,default=None,separator=',')
  • 获取电话号码
# Get phone number by key or keypath and return a dict with different formats (e164, international, national).# If country code is specified (alpha 2 code), it will be used to parse phone number correctly.d.get_phonenumber(key,country_code=None,default=None)
  • 获取鼻涕虫
# Get value by key or keypath trying to return it as slug.# If options and value is in options return value otherwise default.d.get_slug(key,default='',options=[])
  • 获取“slug”列表
# Get value by key or keypath trying to return it as list of slug values.# If separator is specified and value is a string it will be splitted.d.get_slug_list(key,default=[],separator=',')
  • 获取str
# Get value by key or keypath trying to return it as string.# Encoding issues will be automatically fixed.# If options and value is in options return value otherwise default.d.get_str(key,default='',options=[])
  • 获取str列表
# Get value by key or keypath trying to return it as list of str values.# If separator is specified and value is a string it will be splitted.d.get_str_list(key,default=[],separator=',')

django

benedictdjango视图中也非常有用:

params=benedict(request.GET.items())

许可证

MIT License下发布。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
javascript如何从Java中的seleniun访问Chrome上的PDF查看器DOM?   如何打印字符串中的大写字母数   java使用htmlUnit处理动态html表单   java等待事件分派线程创建的GUI   java如何在屏幕上移动/动画单个子类对象而不移动其他子类?   java拟合是从PRNG到最大-最小范围的结果   java如何隐藏所有其他片段   java OpenGL ES纹理图谱失真   java排队/退队或报价/投票   java step/stp文件在另外两个点之间的距离   java如何设置水平仪栏的动画?   在Flex和Java中压缩和解压缩XML   java如何使用AutoIt安装JRE   java我想删除NavigationView布局上的一个栏   java Android Studio按钮小部件OnClick。找不到函数   java ImageView隐藏和显示   java类型的方法和构造函数未定义   swing Java UDP数据包未从客户端程序到达服务器程序   java如何在我自己的服务器上配置推送通知?