Name is required.
Email address is required.
Invalid email address
Answer is required.
Exceeding max length of 5KB

python Script - context how to get the properties of a grid variable

Let's say I have a grid variable called GV in which I defined 3 columns ColA, ColB, ColC.
For each column I have 4 default rows.
ColA ColB ColC
ABC 123 A1B1
DEF 456 C2D2
GHI 789 E3F3
JKL 012 G4H4

I use the following python script to extract the data:
myVar = context.getGridVariable('GV')
print myVar

for x in range(len(myVar)):
for y in range(len(myVar[x])):

When I extract the data from the grid using pyhton. I get something like this.

How do I map the the Index for ColB? Because the order of the columns in the GV might change. And next Time ColB won't be Index 2 it will be index 3.
Is there anyway in the context object to get the index of ColB, or ColA? Or even get a list of the names?



3 Community Answers

Matillion Agent  

David Lipowitz —

Hi Thorsten,

At the moment the context object only has access to the data inside a Grid Variable (represented as a list of row values). Unfortunately no column name information is available at this time. However if you have a use case for making column names available, can you please send that to us? If so, we can open an enhancement request on your behalf.

That said, if you want to extract a list of values from the second column in a Grid Variable, you can do something like the following:

    second_column = [rec[1] for rec in context.getGridVariable('GV')]

Similarly if you make a list of column names available inside the Python Script, you could translate the Grid Variable into a list of dictionaries as follows. Then you could refer to the dictionary objects by column name, e.g.

    column_names = ['ColA', 'ColB', 'ColC']
    gv_as_dicts = [dict(zip(column_names, rec)) for rec in context.getGridVariable('GV')]
    second_column = [rec['ColB'] for rec in gv_as_dicts]

Hope that helps and makes sense.

Best Regards,

Thorsten Schwab —

Hi Dave,

thanks for the answer.
The column names would make it safer. If you can address a value by its name you can be sure that you get the correct value.

Right now I have to use the index and this can change if a new Column is added to the grid variable. And I have to remember that there is a python code which accesses the values which also has to be changed.
This is a problem if more than one developer are working on a project.

Kind Regards,

Matillion Agent  

David Lipowitz —

Hi Thorsten,

I’ve opened an enhancement request based on your feedback. Thanks for forwarding your use case; we use that kind of input to prioritize out development queue so definitely let us know if you have more feature requests.

Best Regards,

Post Your Community Answer

To add an answer please login