首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python -基于json输入构建函数

Python -基于json输入构建函数
EN

Stack Overflow用户
提问于 2019-07-31 19:36:04
回答 1查看 55关注 0票数 1

我在Python模块中有以下代码

代码语言:javascript
复制
    self.helper.layout = Layout(
        Fieldset( _('Basic Information'), Field('form_filler', css_class='enabler'), Div('form_filler_other', css_class='dependent'), 'child_dob','age', 'sex', Field('country', css_class='enabler'), Div('zip_code', css_class='dependent'),'birth_order', Field('multi_birth_boolean', css_class='enabler'), Div('multi_birth', css_class='dependent'), self.birth_weight_field, Field('born_on_due_date', css_class='enabler'), Div('early_or_late', 'due_date_diff', css_class='dependent')),
        Fieldset( _('Family Background'), Field('primary_caregiver', css_class='enabler'), Div('primary_caregiver_other', css_class='dependent'), 'mother_yob', 'mother_education',Field('secondary_caregiver', css_class='enabler'), Div('secondary_caregiver_other', css_class='dependent'), 'father_yob', 'father_education', 'annual_income'),
        Fieldset( _("Child's Ethnicity"),HTML("<p> " + ugettext("The following information is being collected for the sole purpose of reporting to our grant-funding institute, i.e.,  NIH (National Institute of Health).  NIH requires this information to ensure the soundness and inclusiveness of our research. Your cooperation is appreciated, but optional.") + " </p>"), 'child_hispanic_latino', 'child_ethnicity'),
        Fieldset( _("Caregiver Information"), 'caregiver_info'),
        Fieldset( _("Language Exposure"), Field('other_languages_boolean', css_class = 'enabler'), Div(Field('other_languages', css_class='make-selectize'),'language_from', 'language_days_per_week', 'language_hours_per_day', css_class='dependent')),
        Fieldset( _("Health"), 
            Field('ear_infections_boolean', css_class = 'enabler'), Div('ear_infections', css_class='dependent'),
            Field('hearing_loss_boolean', css_class = 'enabler'), Div('hearing_loss', css_class='dependent'),
            Field('vision_problems_boolean', css_class = 'enabler'), Div('vision_problems', css_class='dependent'),
            Field('illnesses_boolean', css_class = 'enabler'), Div('illnesses', css_class='dependent'),
            Field('services_boolean', css_class = 'enabler'), Div('services', css_class='dependent'),
            Field('worried_boolean', css_class = 'enabler'), Div('worried', css_class='dependent'),
            Field('learning_disability_boolean', css_class = 'enabler'), Div('learning_disability', css_class='dependent'),
        ),
    )

我想替换它并使用一个JSON输入文件构建它,所以我创建了这个文件(我只显示第一个字段集)。

代码语言:javascript
复制
[{
    "fieldset" : "Basic Information",
    "fields" : [
        {
            "field" : "form_filler",
            "div" : ["form_filler_other"]
        },{
            "field" : "child_dob"
        },{
            "field" : "age"
        },{
            "field" : "sex"
        },{
            "field" : "country", 
            "div" : ["zip_code"]
        },{
            "field" : "birth_order"
        }, {
            "field" : "multi_birth_boolean",
            "div" : ["multi_birth"]
        }, {
            "field" : "self.birth_weight_field"
        }, {
            "field" : "born_on_due_date", 
            "div" : ["early_or_late", "due_date_diff"]
        }

    ]
}
]

这个代码是:

代码语言:javascript
复制
    rows = []
    fieldsets = json.load(open(self.filename))
    for fieldset in fieldsets:
        fields = []
        for field in fieldset['fields']:
            if 'div' in field:
                this_field = Field(field['field'], css_class="enabler"), Div(lambda x,: x in field['div'], css_class="dependent"),
            else:
                this_field = field['field'],
            fields.append(this_field)

        rows.append(Fieldset(fieldset['fieldset'], lambda field: field in fields))
    x = lambda row,: row in rows
    self.helper.layout = Layout(x)

事后看来,这显然是行不通的,给出了这个错误:

代码语言:javascript
复制
WARNING:root:Could not resolve form field '<function <lambda> at 0x7f8f3acab668>'.

如何基于json输入动态构建此代码?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-31 22:09:51

我觉得你不明白兰博达是怎么做的。它正在创建一个动态生成的匿名函数。因此,您的代码正在创建一个函数,作为第一个参数传递给Div。而在其他地方,你似乎认为这是需要做的清单理解。

看起来,您真正想要的是将字段‘Div’中的列表扁平化,而不是将列表作为第一个参数传递给Div,而是将列表的每个成员作为后续的位置参数传递给Div。

考虑一下

代码语言:javascript
复制
>>> def f(a, b=None, c=None, d=None):
...     print(a)
...     print(b)
...     print(c)
...     print(d)
... 
>>> x = [1, 2, 3]
>>> f(lambda a,: a in x, d='a')
<function <lambda> at 0x7faee8124758>
None
None
a
>>> f(x, d='a')
[1, 2, 3]
None
None
a
>>> f(*x, d='a')
1
2
3
a
>>> 

最后是你想要的,所以

代码语言:javascript
复制
Div(lambda x,: x in field['div'], css_class="dependent")

带着

代码语言:javascript
复制
Div(*field['div'], css_class="dependent")

应该得到你的场集参数。然后用类似的方式调用列表上的Fieldset。类似于:

代码语言:javascript
复制
rows = []
fieldsets = json.load(open(self.filename))
for fieldset in fieldsets:
    fields = []
    for field in fieldset['fields']:
        if 'div' in field:
            this_field = Field(field['field'], css_class="enabler"), Div(*field['div'], css_class="dependent"),
        else:
            this_field = field['field'],
        fields.append(this_field)

    rows.append(Fieldset(fieldset['fieldset'], *fields)
self.helper.layout = Layout(x)

嗯。其实我觉得你现在还有些问题。这不会调用第一个Fieldset参数的下划线函数,看起来您需要对一些字段值做一些额外的工作,但这可以帮助您克服lambda难题。

编辑

以上所述使我在一定程度上达到了这个目的,这就是解决办法:

代码语言:javascript
复制
    rows = []
    fieldsets = json.load(open(self.filename))
    for fieldset in fieldsets:
        fields = []
        for field in fieldset['fields']:
            if 'div' in field:
                fields.append(Field(field['field'], css_class="enabler"))
                fields.append(Div(*field['div'], css_class="dependent"))
            else:
                fields.append(field['field'])

        rows.append(Fieldset(fieldset['fieldset'], *fields))
    self.helper.layout = Layout(*rows)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57297798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档