如何使用动态对象名称访问python对象



我对我的一个python脚本有一个问题。我正在使用图书馆解开(https://github.com/stchris/untangle)将xml配置文件导入并转换到主脚本中。

问题是:我在配置文件中有多个用户的用户信息,我在循环中使用这些信息。它工作得很好,但由于xml文件中生成的对象的名称,它使脚本非常难看。

具体来说,这意味着我不能(或者我只是不知道如何)更改我想使用动态对象的名称。

示例代码如下:

            if employee == 0:
                if str(configobj.config.modes.employee.employee_1.name.cdata) != '':
                    display.drawtext(0,0,str(configobj.config.modes.employee.employee_1.name.cdata),"7x13B",255,255,255,True)
                    if str(configobj.config.modes.employee.employee_1.line1.cdata) != '':
                        display.drawtext(int(configobj.config.modes.employee.employee_1.line1['x']),
                                         int(configobj.config.modes.employee.employee_1.line1['y']),
                    if str(configobj.config.modes.employee.employee_1.line2.cdata) != '':
                        display.drawtext(int(configobj.config.modes.employee.employee_1.line2['x']),
                                         int(configobj.config.modes.employee.employee_1.line2['y']),
                    if str(configobj.config.modes.employee.employee_1.line3.cdata) != '':
                        display.drawtext(int(configobj.config.modes.employee.employee_1.line3['x']),
                                         int(configobj.config.modes.employee.employee_1.line3['y']))
                    displayimage = True
            elif employee == 1:
                if str(configobj.config.modes.employee.employee_2.name.cdata) != '':
                    display.drawtext(0,0,str(configobj.config.modes.employee.employee_2.name.cdata),"7x13B",255,255,255,True)
                    if str(configobj.config.modes.employee.employee_2.line1.cdata) != '':
                        display.drawtext(int(configobj.config.modes.employee.employee_2.line1['x']),
                                         int(configobj.config.modes.employee.employee_2.line1['y']),
                    if str(configobj.config.modes.employee.employee_2.line2.cdata) != '':
                        display.drawtext(int(configobj.config.modes.employee.employee_2.line2['x']),
                                         int(configobj.config.modes.employee.employee_2.line2['y']),
                    if str(configobj.config.modes.employee.employee_2.line3.cdata) != '':
                        display.drawtext(int(configobj.config.modes.employee.employee_2.line3['x']),
                                         int(configobj.config.modes.employee.employee_2.line3['y']),
                    if str(configobj.config.modes.employee.employee_2.image.cdata) != '':
                        display.showimage(160,0,str(configobj.config.modes.employee.employee_2.image.cdata))
                    displayimage = True

这是一个数字不断变化的重复代码。我该如何改进?

使用getattr:

getattr(configobj.config.modes.employee, 'employee_' + str(employee + 1)).name.cdata

您还可以为员工创建单独的变量:

employee = getattr(configobj.config.modes.employee, 'employee_' + str(employee + 1))
print(employee.name.cdata)
print(employee.line1['x'])

相关内容

  • 没有找到相关文章

最新更新