动态字典迭代工具,允许您使用单键声明设置和检索值

keymapper的Python项目详细描述


键映射器

keymapper是dictionary类的一个子类,它允许您:

  • 动态分配键和值
  • 选择所需的分隔符(默认为点号:.)
  • 访问单个键声明中的所有键和值,而不考虑类型(list、tuple、set)
  • 默认情况下被视为dict类型
  • 初始化空字典,或在类声明
  • 上接受现有的DICT
  • 非常适合键迭代和循环
  • keymapper功能不是强加给您的

安装

  • 您可以通过pypi安装:

      pip install keymapper
    
  • 或者,在软件包未归档后在本地安装:

      pip install -e /destination
    

示例

标准dict:

fromkeymapperimportKeyMapperkm_dict=KeyMapper()print(km_dict)# Prints: {} km_dict['key1']='All the keys!'print(km_dict)# Prints: {'key1': 'All the keys!'}

嵌套dict:

fromkeymapperimportKeyMappermy_dict={'messages':{'message_1':'Hey there!'}}km_dict=KeyMapper(my_dict)# Standardprint(km_dict['messages']['message_1'])# Prints: 'Hey there!'# KeyMapperprint(km_dict['messages.message_1'])# Prints: 'Hey there!'

dict与iterables:

fromkeymapperimportKeyMappermy_dict={'messages':{'message_1':'Hey there!'},'objects':[{'obj1':'Hi!'},{'obj2':'There!'},{'obj3':'Peoples!'}]}km_dict=KeyMapper(my_dict)# Standardprint(km_dict['objects'][1]['obj2'])# Prints: 'There!'# KeyMapper - does not care what type of iterableprint(km_dict['objects.1.obj2'])# Prints: 'There!'print(km_dict['objects.1.obj2'])# Prints: 'There!'print(km_dict['objects.1.obj2'])# Prints: 'There!'# Or don't even declare the data typeprint(km_dict['objects.1.obj2'])# Prints: 'There!'

dict和不同的delimier:

fromkeymapperimportKeyMappermy_dict={'messages':{'message_1':'Hey there!'},'objects':[{'obj1':'Hi!'},{'obj2':'There!'},{'obj3':'Peoples!'}]}km_dict=KeyMapper(my_dict,delimiter=',')# Standardprint(km_dict['messages']['message_1'])# Prints: 'Hey there!'# KeyMapper with new delimeterprint(km_dict['messages,message_1'])# Prints: 'Hey there!'

示例

fromkeymapperimportKeyMapperquestions=['What is your name: ','What is your email: ','What is your password: ','How many servers do you want to add: ','What is the ip: ','What is the port: ']keys=['user.name','user.email','user.password','servers']config={'user':{'name':'','email':'','password':''},'servers':[]}km_dict=KeyMapper(config)foriinrange(len(questions)):ifi<3:km_dict[keys[i]]=input(questions[i])else:forrinrange(int(input(questions[i]))):server_info={'ip':'','port':''}fori,kinenumerate(questions[4:]):ifi<1:server_info['ip']=input(k)else:server_info['port']=input(k)km_dict[keys[len(keys)-1]].append(server_info)breakprint(km_dict)# Prints: ... Well, whatever you entered as your input values! Try it if you don't believe me ;)foriinrange(len(km_dict['servers'])):print(km_dict['servers.{}.ip'.format(i)])# Prints the IP for each index iterated through

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

推荐PyPI第三方库


热门话题
java如何通过编程将图像插入Word文档?   java Maven在Quickstart原型中更改jUnit版本   OS X上的eclipse java版本和路径   java在hibernate中将具有依赖对象的类映射到单独的表   JavaJSF2如何在复合组件子组件完成操作后执行操作?   java无法从列表<节点>强制转换为列表<元素>   java如何将数组链接到方法   检查数组中是否存在一个范围内的所有值的最佳方法是什么?(爪哇)   redis Java:我们应该尽快退出trywithresource块来释放资源吗?   对象不会出现在java swing中的按键上   SQLServerJava。sql。SQLException:客户端尝试签出连接已超时   java如何使用JPA以线程安全的方式保存或更新对象?   java如何在不显示滚动条的情况下消除SWT ScrolledComposite中浪费的空间   如何在Eclipse中从Java编辑器显示scala文档?