瑞士陆军数据提取刀
glyphs的Python项目详细描述
字形
示例
来自api调用的示例响应json如下所示。假设我们保留了对那本词典的引用。
my_json_dict={'expand':'schema,names','issues':[{'expand':'operations,versionedRepresentations,editmeta,changelog,renderedFields','fields':{'status':{'description':'','id':'10001','name':'Done','statusCategory':{'colorName':'green','id':3,'key':'done','name':'Done',}}},'id':'10022','key':'XXX-23'},{'expand':'operations,versionedRepresentations,editmeta,changelog,renderedFields','fields':{'status':{'description':'','id':'10001','name':'Done','statusCategory':{'colorName':'green','id':3,'key':'done','name':'Done',}}},'id':'10021','key':'XXX-22'},],'maxResults':50,'startAt':0,'total':17}
对于“问题”的每一项,您如何获得status
&;statusCategory
的id
?
issues=my_json_dict['issues']ifissues:# add a lot of code to check if the type returns worked for you# add a lot of code to handle exceptionsforissueinissues:status_name=issue['fields']['status']['name']# add a lot of code to check if the type returned works for you# add a lot of code to handle exceptions# add a code convert the data or have the rest of your code handle unpredictable returnsstatus_id=issue['fields']['status']['statusCategory']['id']# add a lot of code to check if the type returned works for you# add a lot of code to handle exceptions# add a code convert the data or have the rest of your code handle unpredictable returns
这是你能写的最短的。现在你必须希望从那里得到最好的结果,写很多 处理异常、数据转换等的代码。
带有图示符:
fromglyphs.ro.ROGlyphimportROGlyphfromglyphs.utils.DictUtilsimportDictUtilsissues_glyph=ROGlyph('issues',r_translation_function=list,r_default_value=tuple())name_glyph=ROGlyph('fields>status>name',r_translation_function=unicode,)cat_id_glyph=ROGlyph('fields>statusCategory>id',r_translation_function=int,r_default_value=-1)issues=DictUtils.get(my_json_dict,issues_glyph)forissueinissues:status_name=DictUtils.get(my_json_dict,name_glyph)status_id=DictUtils.get(my_json_dict,cat_id_glyph)
类似的代码有一个扭曲:
- 我们知道我们得到的正是我们想要的。
- 更简单的错误和丢失的数据会自动为我们处理(使用有品位的默认设置)
- 如果我们想使代码更短,它还支持
functools.partial
- 符号是无限共享的!!重复使用和滥用!
- 如果
my_json_dict
的类型改变,我们可以用不同的util(任何您想要的)交换DictUtils
(在以后的代码版本中)所有的东西仍然会按照我们的预期工作。