我正在尝试找到一种加载JSON对象的方法,然后将键映射到Python函数的关键字参数.
这就是现在的一个案例.
def load_new_category(self, filename):
basepath = os.path.dirname(__file__)
filepath = os.path.abspath(os.path.join(basepath, "..", "JSON", filename))
with open(filepath) as sample:
self.create_new_category(is_active=case['is_active'],
description=case['description'], page_title=case['page_title'],
meta_keywords=case['meta_keywords'], meta_description=case['meta_description'],
navigation_menu=case['navigation_menu'],
parent_filter_setting=case['parent_filter_setting'],
show_filter=case['show_filter'], sort_filter=case['sort_filter'])
Run Code Online (Sandbox Code Playgroud)
这种模式可以在许多函数中看到,我正在寻找一种可以推广到任何JSON文件和函数的方法.也许装饰师?
只需使用**kwargs
此处的语法,因为所有关键字参数都将一对一映射到JSON对象键:
self.create_new_category(**case)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
457 次 |
最近记录: |