1

I have a question to one of my python scripts. I'm using the library untangle (https://github.com/stchris/untangle) to import and convert xml config files into the main script.

The problem: I have user informations in the config file for more than one user and I'm using this information in a loop. It works very well, but it makes the script very ugly due to the name of the generated objects from the xml file.

Concrete this means I can't (or I just don't know how) change the name of the object I would like to use dynamic.

The example code is below:

            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

And this is a lot of repeated code with a changing number. How can I improve this?

1
  • Something in the design of your XML or that library seems very wrong if you can't access individual employees as list elements: configobj.config.modes.employee[0].name.cdata, etc. Commented Mar 24, 2016 at 14:46

1 Answer 1

1

Use getattr:

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

You can also create separate variable for employee:

employee = getattr(configobj.config.modes.employee, 'employee_' + str(employee + 1))
print(employee.name.cdata)
print(employee.line1['x'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.