In a PythonCreator, I create a dictionnary. I'd like to use it in the following Transformer, a PythonCaller as an attribute.
So In the Creator, I finish with "return myDict". In the Caller, I call the attribute myDict.
But when I run the project, in the log, it tells me that :
TypeError: 'NoneType' object is not iterable
Do you have any suggestions?
Thanks
Best answer by david_r
Try this instead:
import json import fmeobjects
class FeatureCreator(object):
    def __init__(self):         pass
    def close(self):         feature = fmeobjects.FMEFeature()         myDict = {'Commune' : 'Tokyo'}         feature.setAttribute('my_json', json.dumps(myDict))         self.pyoutput(feature)
Notice how self.pyoutput(feature) always comes last.
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.
    def close(self):         feature = fmeobjects.FMEFeature()         myDict = {'Commune' : 'Tokyo'}         feature.setAttribute('my_json', json.dumps(myDict))         self.pyoutput(feature)
Notice how self.pyoutput(feature) always comes last.
Is that because I'm doing it in a method in a class?
Hi @slerendu, found two issues.
A global variable should be declared in the method definition that refers to the variable. In this case, you have to declare the 'myDict' global variable in the 'close' method.
The feature should be output AFTER assigning a value to the global variable. If the feature was output BEFORE assigning a value to the variable, its instance haven't been created yet when the feature arrived to the next PythonCaller.
Is that because I'm doing it in a method in a class?
In addition, if you save the dictionary into a feature attribute as a JSON document, it's not necessary to use a global variable. See @david_r's answer.
    def close(self):         feature = fmeobjects.FMEFeature()         myDict = {'Commune' : 'Tokyo'}         feature.setAttribute('my_json', json.dumps(myDict))         self.pyoutput(feature)
Notice how self.pyoutput(feature) always comes last.
Someone at my place told me that I can create my dictionnary in a startup python script. Is that a correct use? I find the json method cleaner.
What do you think?
Apples and oranges.
If the dictionary stays the same during the entire translation, I'd just make it a global variable or something like that. You can create a global variable wherever you want, basically, including in a startup script.
If the dictionary is feature specific, I'd go the JSON route.
If the dictionary stays the same during the entire translation, I'd just make it a global variable or something like that. You can create a global variable wherever you want, basically, including in a startup script.
If the dictionary is feature specific, I'd go the JSON route.
A global variable should be declared in the method definition that refers to the variable. In this case, you have to declare the 'myDict' global variable in the 'close' method.
The feature should be output AFTER assigning a value to the global variable. If the feature was output BEFORE assigning a value to the variable, its instance haven't been created yet when the feature arrived to the next PythonCaller.
Thanks for your answer. My problem was indeed the feature output.
In addition, if you save the dictionary into a feature attribute as a JSON document, it's not necessary to use a global variable. See @david_r's answer.
When you say global variable, you mean "global myDict" in the PythonCreator or in the startupPythonScript?
When you say global variable, you mean "global myDict" in the PythonCreator or in the startupPythonScript?
I thought that you needed to define the global variable in the PythonCreator since I found the declaration within your script. However global variables can be defined anywhere according to your requirement. All depends on what you want to do with scripting ;)
I thought that you needed to define the global variable in the PythonCreator since I found the declaration within your script. However global variables can be defined anywhere according to your requirement. All depends on what you want to do with scripting ;)