0

I'm attempting to read the following data from a separate .py file in a different directory, specifically: "assets.ovs.screen0".

Show here is the contents of assets.ovs.screen0.placement:

selection_Size = [600,110]
selection_Loc  = [1500-600,208]
selection_Mov  = [0,115]

At the base of the tree is a file, test.py, currently reading:

import assets.ovs.screen0.placement as placement

requiredVar = "select_"

print placement.select_Size
print eval("placement."+ requiredVar)

Assuming there is a safer/easier way of doing this, what would it be?

derkyzer
  • 161
  • 1
  • 1
  • 9
  • If i understand you correctly you want to get an attribute by name. Try `val = getattr(obj, 'attr_name', default_value)`. Also see (https://docs.python.org/2/library/functions.html#getattr) – jandob Mar 13 '15 at 18:23

1 Answers1

2

Can i try this way:

param = ["selection_Size", "selection_Loc", "selection_Mov", "error"]
for par in param:
    print par, placement.__dict__.get(par, None)

    # this should work too
    # print par, getattr(placement, par, None)

Output:

selection_Size [600, 110]
selection_Loc [900, 208]
selection_Mov [0, 115]
error None
James Sapam
  • 16,036
  • 12
  • 50
  • 73