Skip to main content
Solved

Python startup or parameter issue declaring multiple variables

  • November 27, 2015
  • 4 replies
  • 11 views

Forum|alt.badge.img


Hi

I would like to run 1 python published parameter or alternatively a startup script that can set to variables to access.

These are file names but the reason i need two is to ensure they both exist.

Here is the python with a few hard coded values while testing:

import os

inFolder = r'C:/temp/2015_11_26'

flow_data = []
speed_data = []
setlist = []

# Scan all files/sub directories etc
for root, dirs, files in os.walk(inFolder):
    # reason xml is used is they wanted xml...no idea
    for files in [f for f in files if f.endswith('.xml')]:
        #print files
         if 'speed' in files:
             #print files
             speed_file = os.path.join(root, files.split('_')[0])
             speed_data.append(speed_file)
         elif 'flow' in files:
             flow_file = os.path.join(root, files.split('_')[0])
             flow_data.append(flow_file)
         else:
             print 'Incorrect file', f

# put the matched items into a list to check if they exist in both
for setmatch in set(flow_data) & set(speed_data):
    setlist.append(setmatch)

for itemlist in setlist[:1]:
    speed_data_xml = os.path.join(root, itemlist + '_2015-10-01_TO_2015-10-31_avespeeddata.xml')
    flow_data_xml = os.path.join(root, itemlist + '_2015-10-01_TO_2015-10-31_flowdata.xml')

Now i would like to have both speed_data_xml and flow_data_xml available to me as seperate parameters to call as two sources?

so i would have another python parameter ie:

import pyfme

speed_filename = speed_data_xml

return speed_filename

thanks

Best answer by takashi

Hi,

The execution order of scripts is: firstly scripted parameters are executed (here the parameter values are determined), and then the start up script will be executed. Therefore, variables defined in the start up script cannot be used in scripted parameters anyway.

One of these could be a workaround:

  • Use a PythonCreator to fetch the value (file path string) of the global variable defined in the start up script and create a feature having an attribute that stores the value, then read the source file with the FeatureReader using the attribute value as Dataset.
  • Or, write entire codes in the scripted parameter, rather than the start up script.
  • Or, create another workspace to create the source file path, use the WorkspaceRunner to pass the file path to a published parameter of the main workspace, and run it.

Takashi

View original
Did this help you find an answer to your question?
<strong>This post is closed to further activity.</strong><br /> It may be a question with a best answer, an implemented idea, or just a post needing no comment.<br /> If you have a follow-up or related question, please <a href="https://community.safe.com/topic/new">post a new question or idea</a>.<br /> If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

takashi
Evangelist
  • Best Answer
  • November 28, 2015

Hi,

The execution order of scripts is: firstly scripted parameters are executed (here the parameter values are determined), and then the start up script will be executed. Therefore, variables defined in the start up script cannot be used in scripted parameters anyway.

One of these could be a workaround:

  • Use a PythonCreator to fetch the value (file path string) of the global variable defined in the start up script and create a feature having an attribute that stores the value, then read the source file with the FeatureReader using the attribute value as Dataset.
  • Or, write entire codes in the scripted parameter, rather than the start up script.
  • Or, create another workspace to create the source file path, use the WorkspaceRunner to pass the file path to a published parameter of the main workspace, and run it.

Takashi


takashi
Evangelist
  • November 29, 2015
takashi wrote:

Hi,

The execution order of scripts is: firstly scripted parameters are executed (here the parameter values are determined), and then the start up script will be executed. Therefore, variables defined in the start up script cannot be used in scripted parameters anyway.

One of these could be a workaround:

  • Use a PythonCreator to fetch the value (file path string) of the global variable defined in the start up script and create a feature having an attribute that stores the value, then read the source file with the FeatureReader using the attribute value as Dataset.
  • Or, write entire codes in the scripted parameter, rather than the start up script.
  • Or, create another workspace to create the source file path, use the WorkspaceRunner to pass the file path to a published parameter of the main workspace, and run it.

Takashi

Additional comment about the second choice - scripted parameter.

You cannot create two or more user parameters by just one scripted Python parameter, but can define 3 scripted parameters to get 2 values as user parameters, for example:

1st parameter (e.g. named "XML_PATHS") returns a concatenated string value containing required two file paths separated by a preferable delimiter, e.g. a semicolon.

...
return '%s;%s' % (speed_data_xml, flow_data_xml)


2nd parameter returns the first part of the concatenated paths.

return FME_MacroValues['XML_PATHS'].split(';')[0]

3rd parameter returns the second part.

return FME_MacroValues['XML_PATHS'].split(';')[1]

Alternatively, if you declare global variables and assign required values to them in the 1st scripted parameter, the 2nd and 3rd scripts can access them.

1st parameter:

global speed_data_xml, flow_data_xml
# assign required file paths to the global variables.
# return any value that will not be used.
return 0

2nd/3rd parameter:

# return a global variable value.
return speed_data_xml 

These are possible but I don't know whether using scripted parameter is the best practice.


takashi
Evangelist
  • November 29, 2015
takashi wrote:

Hi,

The execution order of scripts is: firstly scripted parameters are executed (here the parameter values are determined), and then the start up script will be executed. Therefore, variables defined in the start up script cannot be used in scripted parameters anyway.

One of these could be a workaround:

  • Use a PythonCreator to fetch the value (file path string) of the global variable defined in the start up script and create a feature having an attribute that stores the value, then read the source file with the FeatureReader using the attribute value as Dataset.
  • Or, write entire codes in the scripted parameter, rather than the start up script.
  • Or, create another workspace to create the source file path, use the WorkspaceRunner to pass the file path to a published parameter of the main workspace, and run it.

Takashi

P.S. This is also available:

1st parameter creates 2 file paths, returns one, and saves another into a global variable.

global speed_data_xml, flow_data_xml
# assign required paths to the global variables.
# it's not essential to declare 'speed_data_xml' as a global variable.
return speed_data_xml

2nd parameter returns the global variable.

return flow_data_xml

Forum|alt.badge.img
  • Author
  • December 1, 2015

Hi Takashi

This works as required, perfect and now i can loop it through my 1000's of files to find the match file for me!


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