Not sure what you try to do exactly, but this just works for me.
I always use the Generic way (single output port) when using scripted files.
I'm trying to read a list (or string) of file names/paths generated from a Python Script. For testing I am using a simple script with a single hard coded path, but I couldn't even get that to work. I'll try the Generic way you mentioned.
No luck with generic transformer. There is something I'm not understanding with how a private user parameter return info to dataset fields. Any pointers would be appreciated.
Thank you.
Tyler
Hi All,
I am adding some screenshots to help define my question/issue.
--Reader with Dataset linked to private parameter osWalk:
--Private Parameter Definition with Value holding Python script:Python script: Simple hardcoded file raw filepath to be returned.
I am getting 'Undefined Macro, Update Reader failed' when updating the reader with this setup. I feel like the Python might not firing and therefore not returning the file path string. On the other hand, if it is firing, it is not clear to me that the Dataset field is accepting the returned file path string. Is there any way to test private parameter scripts? What data types and format does a reader dataset field accept from Python? Any recommended fixes or other pointers would be appreciated.
Sincerely,
Tyler
Hi @townest
Please find attached a small workspace based on your description.
import os
file = r'C:\temp\TrafficSignals.dwg'
return file
Hope this helps!
Hi @townest ,
to get a list of files you can use the Directory and File Pathnames Reader (this is the simplest and best choice).
If, for some reason, you have to go on with a python script, you could try to define a private parameter of type "Scripted (Python)" and in the "Value" parameter write a script that returns what you want. This will become the value of the parameter.
Here is an example I use to get the date and time of workspace execution:
# Import fme module and datetime.datetime class
import fme
from datetime import datetime
# Use the now() method to get the current date and time
# Format date as YYYYMMDD_HHMMSS with strftime method
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
# Return unique current time which becomes the value of the scripted parameter
return current_time
Maybe you can adapt the example to your case.
Hope that helps!
Ok. I made some progress. My original script actually was working, I'm just trying to get used to FME behavior. "Updating" the Reader in the navigator was erroring out, however "Running" the Reader in the workspace worked just fine. Ok, baby steps. Read one file from scripted private parameter. Check. Now, how can I read a list of files from scripted private parameter. Recall, my original script, which works:
----
file = r'C:\\data\\dwgfoldertest\\Test1.dwg'
return file
-----
Now, for a list I have tried the following:
----
file = r'C:\\data\\dwgfoldertest\\Test1.dwg, C:\\data\\dwgfoldertest\\Test2.dwg'
return file
-----
and I have tried...
----
file = r'C:\\data\\dwgfoldertest\\Test1.dwg C:\\data\\dwgfoldertest\\Test2.dwg'
return file
-----
and I have tried...
----
file = r'C:\\data\\dwgfoldertest\\Test1.dwg, r'C:\\data\\dwgfoldertest\\Test2.dwg']
return file
-----
Any help in returning a list of files from a scripted private parameter to a dataset field would be appreciated. I am comfortable writing Python scripts, so my question is focusing on FME behavior and what FME expects as script returns. In other words, If I understand what FME is expecting as a return I can write the script to return that value and format. Thank you.
Tyler
Ok. I made some progress. My original script actually was working, I'm just trying to get used to FME behavior. "Updating" the Reader in the navigator was erroring out, however "Running" the Reader in the workspace worked just fine. Ok, baby steps. Read one file from scripted private parameter. Check. Now, how can I read a list of files from scripted private parameter. Recall, my original script, which works:
----
file = r'C:\\data\\dwgfoldertest\\Test1.dwg'
return file
-----
Now, for a list I have tried the following:
----
file = r'C:\\data\\dwgfoldertest\\Test1.dwg, C:\\data\\dwgfoldertest\\Test2.dwg'
return file
-----
and I have tried...
----
file = r'C:\\data\\dwgfoldertest\\Test1.dwg C:\\data\\dwgfoldertest\\Test2.dwg'
return file
-----
and I have tried...
----
file = r'C:\\data\\dwgfoldertest\\Test1.dwg, r'C:\\data\\dwgfoldertest\\Test2.dwg']
return file
-----
Any help in returning a list of files from a scripted private parameter to a dataset field would be appreciated. I am comfortable writing Python scripts, so my question is focusing on FME behavior and what FME expects as script returns. In other words, If I understand what FME is expecting as a return I can write the script to return that value and format. Thank you.
Tyler
Maybe it is easier to run the Python code in a PythonCaller and send the results to a FeatureReader. Something like Creator, PythonCaller, ListExploder, FeatureReader.
You also can put the results of the list in the Published Parameter and split the list to Features in workbench, then starting the FeatureReader. Something like Creator, AttributeCreator, AttributeSplitter, ListExploder, FeatureReader.
Maybe it is easier to run the Python code in a PythonCaller and send the results to a FeatureReader. Something like Creator, PythonCaller, ListExploder, FeatureReader.
You also can put the results of the list in the Published Parameter and split the list to Features in workbench, then starting the FeatureReader. Something like Creator, AttributeCreator, AttributeSplitter, ListExploder, FeatureReader.
I've considered that but the Readers play nicer than the FeatureReader when it comes to exposing attributes. Plus, it seems I'm very close, this method is clean and it works fine with one file, just need that tip from someone out there of how to return more than one file to the dataset field. Anybody?
Thanks,
Tyler
Maybe it is easier to run the Python code in a PythonCaller and send the results to a FeatureReader. Something like Creator, PythonCaller, ListExploder, FeatureReader.
You also can put the results of the list in the Published Parameter and split the list to Features in workbench, then starting the FeatureReader. Something like Creator, AttributeCreator, AttributeSplitter, ListExploder, FeatureReader.
Hi @townest ,
have a look at article Python Scripted Parameters in FME, I think there are some useful ideas.
Hope that helps!
Maybe it is easier to run the Python code in a PythonCaller and send the results to a FeatureReader. Something like Creator, PythonCaller, ListExploder, FeatureReader.
You also can put the results of the list in the Published Parameter and split the list to Features in workbench, then starting the FeatureReader. Something like Creator, AttributeCreator, AttributeSplitter, ListExploder, FeatureReader.
I had seen those examples. None of those examples pass a list to a dataset field.
Hi @townest
Please find a sample python script.
- The script will take a list of all the files and convert them to string
- Join the list with "spaces", to mimic adding multiple files in dataset required a space delimited list of files
- Finally, add double quotes around the files name(as required)
import os
file_list = dr'C:\TEMP\Data1.csv', r'C:\TEMP\Data2.csv']
#Surround each file in list with double quotes and join with space delimiter
ListToStr = ' '.join(f"\"{f}\"" for f in file_list)
#print(f"\"{ListToStr}\"")
#Return with string with double quotes around entire string
return f"\"{ListToStr}\""
Hi @townest
Please find a sample python script.
- The script will take a list of all the files and convert them to string
- Join the list with "spaces", to mimic adding multiple files in dataset required a space delimited list of files
- Finally, add double quotes around the files name(as required)
import os
file_list = dr'C:\TEMP\Data1.csv', r'C:\TEMP\Data2.csv']
#Surround each file in list with double quotes and join with space delimiter
ListToStr = ' '.join(f"\"{f}\"" for f in file_list)
#print(f"\"{ListToStr}\"")
#Return with string with double quotes around entire string
return f"\"{ListToStr}\""
That's the answer! I needed the syntax and data type of the object to pass back to FME. A string, double quoted elements, single spaced between elements, finally all encase in double quotes. Thank you @rahulsharma !