Skip to main content

I am trying to create a Python FME shutdown script.  I need it to reference two types of variable - (1) a static variable; and (2) a variable whose value is only known at FME runtime.

 

 

Option (1) I can ebale through a Published Parameter and reference it in my shutdown script as $FME_MacroValues(myParam)

 

 

Option (2) is trickier.  I tried to use a VariableSetter transformer in the workbench but the shutdown script did not seem to recognise the variable.  I also tried to create a dummy published parameter and reset its value at runtime using the VariableSetter again, but the shutdown script only recognised it default value, not its runtime value.

 

 

Do I need to set a global variable first in a startup script; modfiy it at runtime; and the use it in a shutdown script - not quite sure on the syntax if this is the best practise here?

 

 

So how are you meant to expose a global variable and/or a published parameter that gets updated at FME runtime in a python shutdown script?  If someone can provide some sample python syntax that would really help me out.

Regards

 

Mike
Hi,   PythonCaller might be available.   # Startup # declare a global variable and set default value myParam = 'default value'   # PythonCaller # modify value in the function def modifyMyParam(feature):     global myParam     myParam = 'modified value'   # Shutdown # refer the global variable print myParam  
takashi's approach is a good one - but in fact you don't need to declare the variable in a start-up script.  Here is a workspace example where we set the count attribute with a PythonCaller and get it in a Shutdown script: 

 

https://dl.dropbox.com/u/2377887/GetCounter.fmw
Hello Ken, Thanks your supplements. If saying a little more strictly, when the following conditions are satisfied, it's sure that declaration of the global variable in startup script is not essential. - Default value of the variable is unnecessary. - It's guaranteed that the function in which the global variable was declared is called at least 1 time when running the workspace.  
Apparently the workspace link I posted above is broken.  Below is the code you can use to make an attribute value accessible in a Shutdown script. (Note: you can use the same method to make a Parameter Value  accessible by using a ParameterFetcher transformer first to convert your parmater value into an attribute value)

 

 

Assuming you have an attribute called _count

 

 

1. PythonCaller Tranformer
  • Set the parameter "Class or Function " to processFeature
  • Then enter this script
import fmeobjects

 

 

def processFeature(feature):

 

  global MyCounter  

 

  MyCounter = feature.getAttribute('_count')  

 

  pass

 

2. In your Shutodown Script

 

 

import fmeobjects

 

MyCounter = str(MyCounter)

 

print MyCounter

 


I need some suggestions on how to implement the dataset format parameter in my shutdown script. 

My current shutdown script below has a hardcoded html path/filename and I'd like to use the FeatureWriter dataset parameter as url to my shutdown script.

import fme
import fmeobjects
import webbrowser
new = 2 # open in a new tab, if possible

# open an HTML file on my own (Windows) computer
url = "file://///H:/2016/DGN_Conversions/Solution/output/HelloWorld.html"
webbrowser.open(url,new=new)

I added a pythonCaller after my featureWriter with the script below

print MyFile outputs 

 

\\alxapfs23\RSALVALE$\2016\DGN_Conversions\Solution\output\HelloWorld.html
import fme
import fmeobjects
# Template Function interface:
# When using this function, make sure its name is set as the value of
# the 'Class or Function to Process Features' transformer parameter
def processFeature(feature):
    global MyFile  
    MyFile = feature.getAttribute('_dataset')
    print MyFile
    pass
I get this error message from FME:  FME_END_PYTHON: evaluating python script from string...

2016-07-21 10:57:03|   1.3|  0.0|INFORM|END - ProcessID: 13812, peak process memory usage: 70520 kB, current process memory usage: 70416 kB







Python Exception <TypeError>: unsupported operand type(s) for &: 'str' and 'str'




Traceback (most recent call last):

  File "<string>", line 12, in <module>

TypeError: unsupported operand type(s) for &: 'str' and 'str'




Error executing string

@ken and @takashi do you have any suggestions? Thanks!


@geospatiallover

This works for me:

import fmeobjectsimport webbrowser# Template Function interface:def popup(feature):    MyFile = feature.getAttribute('_dataset')    print MyFile    new = 2 # open in a new tab, if possible    # open an HTML file on my own (Windows) computer    url = MyFile    webbrowser.open(url,new=new)

Here's it running in a test Workspace:

htmlbrowserpopup.fmw


I need some suggestions on how to implement the dataset format parameter in my shutdown script. 

My current shutdown script below has a hardcoded html path/filename and I'd like to use the FeatureWriter dataset parameter as url to my shutdown script.

import fme
import fmeobjects
import webbrowser
new = 2 # open in a new tab, if possible

# open an HTML file on my own (Windows) computer
url = "file://///H:/2016/DGN_Conversions/Solution/output/HelloWorld.html"
webbrowser.open(url,new=new)

I added a pythonCaller after my featureWriter with the script below

print MyFile outputs 

 

\\alxapfs23\RSALVALE$\2016\DGN_Conversions\Solution\output\HelloWorld.html
import fme
import fmeobjects
# Template Function interface:
# When using this function, make sure its name is set as the value of
# the 'Class or Function to Process Features' transformer parameter
def processFeature(feature):
    global MyFile  
    MyFile = feature.getAttribute('_dataset')
    print MyFile
    pass
I get this error message from FME:  FME_END_PYTHON: evaluating python script from string...

2016-07-21 10:57:03|   1.3|  0.0|INFORM|END - ProcessID: 13812, peak process memory usage: 70520 kB, current process memory usage: 70416 kB







Python Exception <TypeError>: unsupported operand type(s) for &: 'str' and 'str'




Traceback (most recent call last):

  File "<string>", line 12, in <module>

TypeError: unsupported operand type(s) for &: 'str' and 'str'




Error executing string

Can you show us your shutdown script?


@geospatiallover

This works for me:

import fmeobjectsimport webbrowser# Template Function interface:def popup(feature):    MyFile = feature.getAttribute('_dataset')    print MyFile    new = 2 # open in a new tab, if possible    # open an HTML file on my own (Windows) computer    url = MyFile    webbrowser.open(url,new=new)

Here's it running in a test Workspace:

htmlbrowserpopup.fmw

Thanks and I tested your FMW and it worked as suggested. 


Hello @geospatiallover - Please try publishing the Dataset Value as a parameter in the FeatureWriter and then access this parameter in the python shutdown script using something like: SourceDataset = FME_MacroValues['URL'] - where 'URL' is the published parameter name.


@CandaceAtSafe, I got your email and sample fmw and it worked for me. Thanks!


Hi, On the issue of passing runtime data from FME Workspace to shutdown script this one, bit old fashioned may be, however works for me hFME 2015], might not work on all platforms and might be a bit dirty:

I use a Creator and a PythonCaller executing the following script that creates an environment variable:

import fme
import fmeobjects
import os

# Template Function interface:
def FeatureProcessor(feature):  
  os.environc'A_VAR'] = 'My value'
  print os.environr'A_VAR']  
  pass

The shutdown python script just references the Env var created in the PythonCaller script:

import os

print 'THIS IS SHUTDOWN\n'
print os.environp'A_VAR']

Here is the result:

.
.
.
.
Translation was SUCCESSFUL with 0 warning(s) (0 feature(s) output)
FME Session Duration: 1.2 seconds. (CPU: 0.2s user, 0.5s system)
END - ProcessID: 5796, peak process memory usage: 60748 kB, current process memory usage: 60644 kB
FME_END_PYTHON: evaluating python script from string...
FME_END_PYTHON: python script execution complete.
Translation was SUCCESSFUL

My value
THIS IS SHUTDOWN
My value

Basically environ is a global struct too.


Reply