Skip to main content
In my workbench, I've got ~10 python scripted private parameters.

 

 

Because I'm "lazy" and do not want to duplicate code in every parameters, I've come with a hack that look like this.

 

I define a python scripted variable that does something like this:

 

 

 def my_function(*args, **kargs): 	# blabla 	pass import sys sys.my_function = my_function return "Done" 
 

 

And in every other python scripted variable I can do this:

 

 

 import sys return sys.my_function(some_variable)
 

 

It work well on the desktop if the "hacking variable" is declared first.

 

 

My problem is that it does not work on the server.

 

 

I've tried to put this hack in "startup script" but it does not work either.

 

 

So here is my questions:

 

_how parameters are initialized on the server ?

 

_how can I share a python module with my workbench ?

 

_how can I embeded a python module with my workbench ?
Hi,

 

 

I'm not sure why your script doesn't work on the server.

 

Try a global variable containing the function object instead. This works on Desktop, but not tested on Server.

 

-----

 

# The first parameter

 

def my_function(arg):

 

    # bla bla

 

    return something

 

 

global my_func

 

my_func = my_function

 

return 'Done'

 

-----

 

# The second or later parameter

 

return my_func(somevalue)

 

-----

 

 

If it doesn't work on the server, I think there is no way in Python scripting, unfortunately.

 

Since Tcl can define macros in a scripted parameter, Tcl could be a workaround if Python global variable doesn't work.

 

 

Takashi
Hi,

 

 

the simplest and cleanest solution (in my opinion) is simply to create an external Python module and import into your workbench scripts. Just remember to save it in the same directory as the workspace file and to include it when you publish to FME server, and you should be fine.

 

 

David
Thanks guyes for your answers !

 

 

@Takashi: my problem is not the visibility of the function, just the fact the "python scripted variable" that create the function is not called before the other variable (that may be because variables are not initialised the same way on desktop and sever....)

 

 

@David: "And what for ?" would you say !

 

Well, what I want is to read a configuration file with some parameters (ex: login/pwd pwd in encrypted) so depending of my environnement (dev/prod/local etc...) db to read / write are well defined and I do not have to remember "ho it's prod server, so DB is this...).

 

So I need to open a simple json file with paremeters inside.

 

 

But FME store path like this: '$(FME_MF_DIR)COMMON\\\\Transformers\\\\parameters.json' or like this (a file next to the workbench '$(FME_MF_DIR)parameters.json')

 

 

So I've written a small function with regexp witch parse the string, see if it contain a variable, search the value if needed etc

 

 

That's why I do not want to duplicate the parameter file, nor the module python in every workbench (it broke the purpose of a separate file).

 

 

I would like a share a python module without the need to access the server (prod server are maybe more protected than Fort Knox...).

 

If really this is the only solution, maybe I can... but I would like not too.

 

 


Hi,

 

 

I understand completely, because my suggested solution is used for exactly the same functionality ;-)

 

 

What I did:
  • Create external Python module, e.g. GetConfig.py
  • Inside module, define class with methods to access and parse configuration settings, such as GetUsername() and GetPassword(), etc.
  • Inside first scripted parameter, import GetConfig and create a class instance
  • In the following scripted parameters, use the class instance methods to retrieve config settings. This means that the the parameter linked to the database username contains something like "return MyConfigClassInstance.GetPassword()"
This works really well and is easy to implement over a series of workspaces. The benefit is that you end up having only one single place to debug and update, rather than code spread over several parameters and workspaces.

 

 

David
And how do you share / deploy this python script ?

 

You publish it along the workbench ?
Yes, you deploy it normally along the workspace.

 

 

David
Ok, I'm trying this.

 

 

BTW, I'm using FME 2013, and in my python I'm loggin like this:

 

 

 import fmeobjects logger = fmeobjects.FMELogFile() logger.logMessageString("my message")
 

 

It work very well on desktop, but nothing is display on the server... any idea ?
@David I've put my module next to my workbench and the configuration file too.

 

It work well on local.

 

 

So I uploaded them on the server (workbench, module (py + pyc) and conf file).

 

And it broke... because I can't log in python I'm stuck... any idea ?

 

 

Maybe the server was not install properly ?

 

Do I need to check for somethin ?

 

A python SDK or somethin like this ?

 

 

Thanks again for your time.

Reply