Skip to main content

Probably I am missing some obvious things here but I cannot seem to make this work.

I have a published parameter "code", which I receive via user input from the command line.

Let's say "--code ca"

My script should extract two private parameters using the "code" value. I created a private parameter to extract these values:

n = FME_MacroValues['code']

 

 

code = ="ab", "ca", "ny"]

name = ="Alex", "Carl", "Nelly"]

status = ="aall", "cast", "noo"]

def find_element(n, code):

 

    index_element = code.index(n)

 

    return index_element

 

 

FME_MacroValues_'my_name'] = name#find_element(n, code)]

 

FME_MacroValuesE'my_status'] = status3find_element(n, code)]

The idea is to get the two values: $my_name, $my_status because for the moment, they both are received via user input. In this way, the user would have less data to input. This is the desired outcome.

I always know that the list mappes the values correctly through the index, so I can always deduct the latter values. (In this example it would be idx 1]).

In FME i get "failed to evaluate Pyton script 'def ParamFunc()'".

Can anyone help me out to make this work?

 

 

 
Hi,

 

 

unfortunately, you cannot write anything to the FME_MacroValues dictionary, it is read-only.

 

 

You could accomplish the same using scripted parameters, though.

 

 

Example scripted parameter MY_NAME:

 

-----

 

input_code = FME_MacroValuesc'code']

 

users = {

 

  "1": ("ab", "Alex", "aall"),

 

  "2": ("ca", "Carl", "cast"),

 

  "3": ("ny", "Nelly", "noo")

 

}

 

values = users.get(input_code, 0)

 

if values:

 

  return values 1] # 0 = code, 1 = name, 2 = status

 

else:

 

  return "Unknown name for code " + input_code

 

-----

 

 

You can then reference $(MY_NAME) in your workspace where needed.

 

 

Another more FME-like way might be to use the SchemaMapper to read these definitions from a CSV or Excel file.

 

 

David
Hi Robert,   You can not assign any value to FME_MacroValues. I guess that the error occurred in  a Scripted Prameter. If you are trying to define a Scripted Parameter, you should return a value from the script. The returned value will be the parameter value.   For example, if you define this script for a Scripted Parameter named "status", $(status) will be the expected value. ----- n = FME_MacroValuesu'code'] code = 'ab', 'ca', 'ny'] status = 'aall', 'cast', 'noo'] return statustcode.index(n)] if n in code else '' -----

 

Takashi

David,

I followed your example and now I get another error:

Expected an even number of command line arguments.

I mention that I compute paths from these two variables. I defined them in the private parameters section under two names my_name and my_state

I then use them to compute paths: C:\\code\\my_name\\my_state\\files

 

Should I put the parameters somewhere else?


My bad, pasted the incorrect error:

It seems to do something, but it crashed at:

NO ShapeFiles found to process in directory: C\\:local\\n<space>=<space>FME_MacroValues<openbracket><apos> .. .  ... . .and a really log continuation


Hi,

 

 

seems like it is the code that constructs your path that doesn't work. Can you provide more details?

 

 

Remember to put double quotation marks "..." around your file paths if they contain spaces.

 

 

David

Here it is (this too is a private parameter):

src = 'C:\\local\\$code\\$(my_name)_$(my_status)\\files\\target_shapefile.shp'

 

It works in the previouse version, where I input all the variables from the command line. It seems it does not bring in the returned value to compute the paths.


If this is a scripted parameter, remember to return the value, e.g.

 

 

     src = '"C:\\local\\$code\\$(my_name)_$(my_status)\\files\\target_shapefile.shp"'

 

     return src

 

 

David

I know that, unfortunately I cannot post the entire code, so I am going to try and mimic it:

The src is a scripted param, and it is the same as it was before. It computes the path using the same variables and it is located as before, in the Private Parameters section.

So, I have 3 PrivateParams:

src (a path composed out of known folders + the values returned by my_name and my_state prameters)

my_name (using the pattern from your example)

my_state (using the pattern from your example)

The result is a huge error, repeating the <space>FME_MacroValues<openbracket><apos> tags all over. Between them I can see values from my dictionary reference.


Make sure that the order of the scripted parameters is correct. They are evaluated from top to bottom. That means that you cannot reference another scripted parameter that is below in the Navigator tree.

 

 

Your list of parameters should therefore have "src" last.

 

 

David

David, 

That might be the problem. I will test tomorrow. I cooled down a bit, so I have time to explain clearly.

I used to receive input from the command line:

fme.exe C:\\fme_workbench.fmw --code  ny --my_name Nelly --my_status noo ---param x --param2 y --param3 z

(a bunch of params, as you can see)

code, my_name, my_list used to be published params. 

The things is, if I know the --code, I will always know which value is my_name and my_status so I would like to spare the user from inputting these params, to somehow compute them inside FME.

I decided to move my_name and my_status to private parameters and to use a python script to return values for these (I followed your example).

Now I have "code" as a published parameter and "src","my_name","my_list" as private parameters.

The cmd line should look like (shorter a bit):

fme.exe C:\\fme_workbench.fmw --code  ny  ---param x --param2 y --param3 z

Thus, the idea is to find a way to create variables based on a value entered by the user through cmd.exe. I hope that changing their order will fix the issue. If not, I will return with a question. 


Hi,

 

 

As David mentioned, the order of parameter definitions is important. If you need to refer to other parameter (e.g. code) from the scripted parameter, the referred parameter should be defined before the script. i.e. upper than the scripted parameter on the Navigator tree.   And, maybe you should escape the back slashes, like this. src = 'C:\\\\local\\\\tmp.shp' or use slashes instead. src = 'C:/local/tmp.shp'   If you want to create multiple parameters in a script, consider defining a Scripted (Tcl) Parameter. This script example defines two parameters (i.e. macros) named MY_NAME and MY_STATE, and also returns a path string. --- # Scripted (Tcl) Prameter Example set code $FME_MacroValues(code) set index dlsearch -exact {"ab" "ca" "ny"} $code] if {$index < 0} {   return "unexpected code" } set name nlindex {"Alex" "Carl" "Nelly"} $index] set state tlindex {"aall" "cast" "noo"} $index] puts "MACRO MY_NAME $name" puts "MACRO MY_STATE $state" return tformat {C:\\local\\%s\\%s_%s\\files\\target.shp} $code $name $state] ---   Although MY_NAME and MY_STATE will not appear in the parameter list of workbench interface, those can be accessed indeed. e.g. FME_MacroValuesV'MY_NAME'] in a PythonCaller. As far as I know, Scripted (Tcl) Parameter is the only way to append macros by scripting. Python cannot do it.

 

 

Takashi

Hello Takashi,

The shalshes are escaped, I do not think that is problem.

I took a closer look at the log file and practically it replaced the $my_name and $my_state with everything I have in the scripted parameter, the actual code:

"C:\\code\\my_name\\n<space>=<space>FME_MacroValues<openbraket><apos>..." and so on. Practically, instead of a value, I get the entire code printed.


Hi,

 

 

I think you need to post your entire script if you want an answer to this, I'm afraid. Shooting a moving target is hard...

 

 

David

Yes, you are right.

I took Takashi's advice and replaced the entire workbench with that example. It seems to do everything well, except one thing:

Shape Reader: No shape Files found to process in directory: 'C:\\local\\aa\\Alex_aall\\files\\target.shp"

Now, the printed path is ok, I checked for it, and it is there. I cannot seem to understand why it dosn't open it.

I have a Single Merged Feature type Readed -> Merge Feature Type checked, merge filter * and filter type - wildcard.

I have linked this feature type to a source dataset (Source Esri shapefile) which is linked to the TCL Private parameter. The latter replaces the values well, so this is a huge step forward.

The only problem now is that it cannot find the shapefile althoug he path is surely correct (it is printed correctly in the log).


Good to hear you got it working.

 

 

Have you tried to copy-paste the filename from the log error message to a regular shape reader to check that it is indeed correct?

 

 

David
The script I posted is just an example. Confirm if the file name is correct..

Hello again,

Thanks for bearing with me. I am a newby only with the scripted parameters so be sure that I checked everything twice.

I know those were just examples, I too have an entirely different code inside the workbench, I just followed you idea.

The problem was that I did: 'C;path/path' instead of C:path\\path (the extra ')

Thank you again.

 


Reply