Question

PythonCaller

  • 14 January 2014
  • 8 replies
  • 22 views

Badge
Guys,

 

 

This question may be supid, but I'm trying to get values of attribute within a python script. 

 

 

Basicly, I've written the following code :

 

 

def processFeature(feature):     txt = feature.getAttribute('text_log')     pass

 

And I want to obtain the variable "txt" to used it later on (... log.write(txt) ...)

 

 

How do I access to the function ?

 

 

Any suggestions ?

 

 

Thx

 

 

regis

 

 

 

 

 

 

8 replies

Userlevel 4
Hi,

 

 

you are defining the variable "txt" inside a function block, which means that the variable will only exist inside the function block (this is called the scope of the variable). Once the function block is terminated, the variable "txt" is destroyed (it goes out of scope).

 

 

You can, however, force your variable to be globally accessible using the "global" reserved word in Python. Your variable will then have a global scope, meaning it will exist for the entire duration of your Python instance. Example:

 

 

def processFeature(feature):

 

    global txt     txt = feature.getAttribute('text_log')     pass

 

You can now access the variable "txt" from other Python blocks within the same workspace instance, e.g. other PythonCallers, shutdown scripts, etc.

 

 

David

 

 

Badge
Ok thank's for your answer, but I'm still stuck, 

 

 

How can I access to this variable outside the block :

 

 

def processFeature(feature):     global txt     txt = feature.getAttribute('text_log')     txt = "lpplp"     pass   [.. Something here is missing ...]   log.write(txt)  [... this doesn't work ]

 

 

The string is the result of this function, When I call it by using "string = processFeature()" -> error, similarly "string = processFeature(feature)" -> again error. 

 

 

What must I specify within the () ? 

 

 

thank's

 

 

 

 
Userlevel 2
Badge +17
Hi,

 

 

I have some questions. I guess that "log" refers to a file object. Is it right? If so, where and how did you open the file? And when do you need to write "txt" into the file? Need to write "text_log" of every feature when the PythonCaller receives it?

 

 

Takashi
Badge
Well here is the entire code : 

 

 

----

 

path = "C:\\\\Python27\\\\Codes\\\\new\\\\"      log = open(''+path+'log.txt', 'r+') old = log.read() log.seek(0)    def processFeature(feature):     global txt     txt = feature.getAttribute('text_log')     pass   log.write(txt) log.write("\\n" +old) log.close() 

 

---

 

 

What I try to do is to open a txt file, write the value of an attribute (text_log in that case) and close it.

 

 

Any clue ?
Userlevel 2
Badge +17
FME calls function or class method which is specified to "Class or Function to Process Features" parameter of the PythonCaller. Codes which are written in the global scope in the PythonCaller script usually will not be executed. A workaround would be: 1) Open the file in Startup Python Script. 2) Write "txt" and close the file in Shutdown Python Script.
Badge
Thank's Takashi,

 

I'm gonna try this, but I'm wondering if there is not an easiest way to do that. What I want is to write information in ONE text file. My workbench is pretty big and I want to write down intermediary results during the running. However, it is not possible to duplicate text file writer, and targetting every intermediate result into one writer is not feasible (at least, not human readable...)  

 

Does anyone know how to do that ?

 

 

CU

 

Regis
Userlevel 2
Badge +17
Just as an example. If you opened a file in Startup Script like this: ---- # Startup Python Script g_log = open('C:\\\\FME\\\\tmp\\\\log.txt', 'w') ----   since "g_log" is a global variable, it can be accessed from anywhere in the same workspace instance. For example, a PythonCaller with this script writes "text_log" for every input feature into the file.  ----- # PythonCaller Script import fmeobjects def processFeature(feature):     g_log.write('%s\\n' % feature.getAttribute('text_log')) -----
Userlevel 2
Badge +17
Forgot closing the file. ----- # Shutdown Python Script g_log.close() -----   In addition, if you need to write "text_log" into the same file at multiple locations in the workspace, you can define a common function in Startup Script, like this. ----- # Startup Python Script Example import fmeobjects g_log = open('C:/FME/tmp/log.txt', 'w') def logText(feature):     g_log.write('%s\\n' % feature.getAttribute('text_log')) ----- In that case, it's not necessary to write script for each PythonCaller. Just specify the function name ("logText" in the example above) to "Class or Function to Process Features" of every PythonCaller.

 

 

Most appropriate way depends on your target and conditions.

Reply