Skip to main content
Question

Dynamic Lookup in Python? e.g. inline FeatureMerger


Forum|alt.badge.img

Good evening,

Is there a way to do a lookup (like a FeatureMerger) within a Python script? I have multiple attributes that I need to lookup against a single list, but I'm not sure how to supply this out-of-band (supplier) to the python without muddying/creating duplicate features.

Basically I'm trying to avoid creating 20 FeatureMergers for each attribute lookup I need to do.

Cheers,

Peter

14 replies

Forum|alt.badge.img
  • June 11, 2018
Hi @peterx,

 

 

What is the single list? is it a predefined list of values or dynamic as well?

 

Dave

 


  • June 11, 2018

My first thought here is a combo of "some transformer(s)" and the attributevaluemapper but I really think the problem needs to be specified a little more for the community to be of any help.


david_r
Celebrity
  • June 11, 2018

The InlineQuerier would be able to do all the merges in one single go, using e.g. a LEFT JOIN on all the lookup tables.


Forum|alt.badge.img
  • Author
  • June 11, 2018
paalsund wrote:

My first thought here is a combo of "some transformer(s)" and the attributevaluemapper but I really think the problem needs to be specified a little more for the community to be of any help.

The Attribute Value Mapper "Import" seems to be static? I would like to have a dynamic list from a SQL database.

 

 

Just for some background, as the Schema Mapper can't be dynamic (i.e. filtered by a specific type), I have used Python to dynamically map values: https://knowledge.safe.com/questions/58038/schema-mapper-and-a-dynamic-where-clause.html?childToView=70359#answer-70359

 

 

However, once I've performed this mapping in Python, I then need to map (again!) to a new value based on the dynamically generated values.

 

 


Forum|alt.badge.img
  • Author
  • June 11, 2018
davidrich wrote:
Hi @peterx,

 

 

What is the single list? is it a predefined list of values or dynamic as well?

 

Dave

 

Dynamic as well (from SQL DB). As a further complication, I need to filter the list based on a related value (Type List). So, for example:

 

 

Asset, Type List, Type --> Type Code

 

1234, SensorType, Pressure Monitor --> PM

 

1235, FitOutType, External Door --> EX_DOOR

 

 

I have used Python to dynamically arrive at the "Type" value above, but then need to translate to "Type Code" and re-filtering on "Type List" to ensure I don't get the wrong code. (This can be up to ~10 different values needing to be mapped per feature).

Forum|alt.badge.img
  • Author
  • June 12, 2018
I almost need some kind of looping construct to pre-filter this lookup list and then perform the mapping per attribute.

takashi
Influencer
  • June 12, 2018

I assume that you need to perform a sort of attribute value mapping operation (like the AttributeValueMapper transformer) for multiple attributes simultaneously, according to an identical lookup table which defines mapping rules between old value and new value.

If my assumption is correct, you can read the lookup table first and create a Python dictionary (key = old value, value = new value) with a PythonCaller, and then use the dictionary to map attribute values for every feature which will be input to the PythonCaller subsequently.

Note that Python interpreter would distinguishes between string and numeric. Probably it would be better to treat every attribute value as string in the script, even if there were numeric values.

If you would provide sample source datasets (a CSV lookup table and a dataset containing some attributes that should be mapped according to the lookup table) and desired destination table, a workflow example including a PythonCaller (script) could be considered.


Forum|alt.badge.img
  • Author
  • June 12, 2018
takashi wrote:

I assume that you need to perform a sort of attribute value mapping operation (like the AttributeValueMapper transformer) for multiple attributes simultaneously, according to an identical lookup table which defines mapping rules between old value and new value.

If my assumption is correct, you can read the lookup table first and create a Python dictionary (key = old value, value = new value) with a PythonCaller, and then use the dictionary to map attribute values for every feature which will be input to the PythonCaller subsequently.

Note that Python interpreter would distinguishes between string and numeric. Probably it would be better to treat every attribute value as string in the script, even if there were numeric values.

If you would provide sample source datasets (a CSV lookup table and a dataset containing some attributes that should be mapped according to the lookup table) and desired destination table, a workflow example including a PythonCaller (script) could be considered.

Thanks Takashi - how would I read the lookup table in Python? Currently I'm pursuing the Inline query solution suggested by david_r below, it's a bit clunky, but better than ~20 Feature Mergers.

takashi
Influencer
  • June 12, 2018
takashi wrote:

I assume that you need to perform a sort of attribute value mapping operation (like the AttributeValueMapper transformer) for multiple attributes simultaneously, according to an identical lookup table which defines mapping rules between old value and new value.

If my assumption is correct, you can read the lookup table first and create a Python dictionary (key = old value, value = new value) with a PythonCaller, and then use the dictionary to map attribute values for every feature which will be input to the PythonCaller subsequently.

Note that Python interpreter would distinguishes between string and numeric. Probably it would be better to treat every attribute value as string in the script, even if there were numeric values.

If you would provide sample source datasets (a CSV lookup table and a dataset containing some attributes that should be mapped according to the lookup table) and desired destination table, a workflow example including a PythonCaller (script) could be considered.

No, I didn't say you need to read the lookup table with Python script.

 

You can read the lookup table with an appropriate FME reader (or transformer), enter the features output from the reader into a PythonCaller, then create a Python dictionary from them with a script.

 

In the script, you can distinguish the lookup table features from data features by adding a specific attribute to them beforehand, for example.

 

Just be aware that the lookup table should be read before any data feature.

Forum|alt.badge.img
  • Author
  • June 13, 2018
takashi wrote:
No, I didn't say you need to read the lookup table with Python script.

 

You can read the lookup table with an appropriate FME reader (or transformer), enter the features output from the reader into a PythonCaller, then create a Python dictionary from them with a script.

 

In the script, you can distinguish the lookup table features from data features by adding a specific attribute to them beforehand, for example.

 

Just be aware that the lookup table should be read before any data feature.
Ok thanks, that makes sense - pass in these additional "rows" and then filter them on the way out.

 

 


Forum|alt.badge.img
  • Author
  • June 13, 2018
takashi wrote:
No, I didn't say you need to read the lookup table with Python script.

 

You can read the lookup table with an appropriate FME reader (or transformer), enter the features output from the reader into a PythonCaller, then create a Python dictionary from them with a script.

 

In the script, you can distinguish the lookup table features from data features by adding a specific attribute to them beforehand, for example.

 

Just be aware that the lookup table should be read before any data feature.
Actually, I'm confused, how do I read-in those values, if the PythonCaller is reading in the Features one-at-a-time. I need to provide the PythonCaller a list?

takashi
Influencer
  • June 13, 2018
peterx wrote:
Actually, I'm confused, how do I read-in those values, if the PythonCaller is reading in the Features one-at-a-time. I need to provide the PythonCaller a list?
repost: "If you would provide sample source datasets (a CSV lookup table and a dataset containing some attributes that should be mapped according to the lookup table) and desired destination table, a workflow example including a PythonCaller (script) could be considered."

 

 


  • June 13, 2018

Sorry for not being able to answer, bit it seems @takashi is on track (again!)


david_r
Celebrity
  • June 13, 2018

Here's a simplified description of how to accomplish this using Python lookups, as I've implemented this myself in scenarios fairly similar to the one described here:

Create reader(s) for all the lookup values, send these into a single PythonCaller. Make sure these features come before the main data features. Inside the PythonCaller one, create a global dict which you populate with all the lookup values.

Direct all the main data features in to a second PythonCaller. Reference the global dict (populated in the other PythonCaller) to retrieve the lookup values.

I hope this explains the general concept.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings