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

时间:2016-03-24 14:35:50

标签: python object dynamic objectname

我对我的一个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

这是很多重复的代码,数字不断变化。我怎样才能改善这个?

1 个答案:

答案 0 :(得分:1)

使用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'])