如何在Python中动态选择和传递命名参数?

时间:2012-07-11 21:17:22

标签: python

payload是一个逗号分隔的数据块,表示来自逆变器的读数。这些读数需要存储在名为InverterHistory的表中。每个字段都不会总是有值。该模型已设置为允许字段为空/空白。因此,我想在将其分配给字段之前检查是否存在值。以下是我到目前为止的情况:

i = Inverter.objects.get(mac=u)
payload.reverse()

try:
    ac_volts_a = str(payload.pop())
    ac_volts_b = str(payload.pop())
    ac_volts_c = str(payload.pop())
    ac_current_a = str(payload.pop())
    ac_current_b = str(payload.pop())
    ac_current_c = str(payload.pop())
    dc_volts = str(payload.pop())
    dc_current = str(payload.pop())
    kw_out = str(payload.pop())
    mwh_total = str(payload.pop())
    current_time  = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")

    i_data = InverterHistory(
        inverter = i,
        if ac_volts_a: 
            voltage_ac_a = float(ac_volts_a),
        if ac_volts_b: 
            voltage_ac_b = float(ac_volts_b),   
        if ac_volts_c: 
            voltage_ac_c = float(ac_volts_c),
        if ac_current_a: 
            current_ac_a = float(ac_current_a), 
        if ac_current_b: 
            current_ac_b = float(ac_current_b),
        if ac_current_c: 
            current_ac_c = float(ac_current_c),
        if dc_volts: 
            voltage_dc = float(dc_volts),   
        if dc_current: 
            current_dc = float(dc_current), 
        if kw_out: 
            kwout = float(kw_out),
        if mwh_total: 
            mwh = float(mwh_total),
        recordTime  = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
    i_data.save()
except Exception, e:
    print >> sys.stderr, "Error While Recording Inverter History"
    print >> sys.stderr, e
    raise Http404

我是python的新手,不知道如何正确编写上面的代码。当我尝试编译代码时,我收到if语句开始的错误。我如何修复上面的代码,以便根据需要编译和工作?

4 个答案:

答案 0 :(得分:5)

当您编写i_data = InverterHistory(...)时,您正在创建类型为InverterHistory的对象,而...是您传递给构造函数的参数。在Python中构造一个对象就像调用一个函数或一个方法,你不能把逻辑放在括号内。因此,您收到错误,因为在那里放置if语句的语法不正确。

您需要做的是将您的逻辑从括号中移出并将所有变量放在Python dict中。然后,您可以将dict作为参数传递给您对InverterHistory的调用。

params = {
    'inverter': i,
    'recordTime': datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
}
if ac_volts_a:
    params['voltage_ac_a'] = float(ac_volts_a)
if ac_volts_b: 
    params['voltage_ac_b'] = float(ac_volts_b)

# Do the same with all your attributes
# ...

# Then you can create your object and pass it the dictionary you have created
i_data = InverterHistory(**params)

**params解包字典。这意味着口译员将从词典中取出所有key: value并将其转换为key=value。这样,if语句为True的每个参数都将作为命名参数传递给InverterHistory

答案 1 :(得分:3)

您应该在调用方法之前构造参数字典,然后使用python关键字unpack运算符将字典作为参数传递。

简化示例:

kwargs = {"inverter":i}
if ac_volts_a: 
  kwargs['voltage_ac_a'] = float(ac_volts_a)
i_data = InverterHistory(**kwargs)

相关的SE问题: What does *args and **kwargs mean?

答案 2 :(得分:1)

我不太了解你要做的事情,但错误是因为在第一个if语句之前有两行不匹配的括号。调用函数时,参数列表中不能包含if语句。

答案 3 :(得分:0)

有一件事 - 我认为你不能说def some_func(if blah: x = 0),但你可以将其重写为def some_func(x = 0 if blah else None)。您将if then替换为if then else,但它应该更适合口译员。

所以这一部分:

 i_data = InverterHistory(
    inverter = i,
    if ac_volts_a: 
        voltage_ac_a = float(ac_volts_a),
    if ac_volts_b: 
        voltage_ac_b = float(ac_volts_b),   
    if ac_volts_c: 
        voltage_ac_c = float(ac_volts_c),
    if ac_current_a: 
        current_ac_a = float(ac_current_a), 
    if ac_current_b: 
        current_ac_b = float(ac_current_b),
    if ac_current_c: 
        current_ac_c = float(ac_current_c),
    if dc_volts: 
        voltage_dc = float(dc_volts),   
    if dc_current: 
        current_dc = float(dc_current), 
    if kw_out: 
        kwout = float(kw_out),
    if mwh_total: 
        mwh = float(mwh_total),
    recordTime  = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))

所有if语句都是“向后”。

编辑:

更好的方法可能就是:

i_data = InverterHistory(**keywords)

然后在定义中,您使用此构造:

 def InverterHistory(**kws):
    voltage_ac_a = float(kws.get('ac_volts_a',0.00));

 def InverterHistory(**kws):
    voltage_ac_a = kws.get('ac_volts_a',None);
    voltage_ac_a = float(voltage_ac_a) if voltage_ac_a else None # avoids error if none