Notice how self.pyoutput(feature) always comes last.
Also notice that you have to import the json module.
If you attach a Logger after this PythonCreator you should get:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Feature Type: `Logger_LOGGED'
Attribute(string): `fme_feature_type' has value `PythonCreator'
Attribute(string): `my_json' has value `{"Commune": "Tokyo"}'
Geometry Type: Unknown (0)
===========================================================================
Notice how self.pyoutput(feature) always comes last.
Also notice that you have to import the json module.
If you attach a Logger after this PythonCreator you should get:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Feature Type: `Logger_LOGGED'
Attribute(string): `fme_feature_type' has value `PythonCreator'
Attribute(string): `my_json' has value `{"Commune": "Tokyo"}'
Geometry Type: Unknown (0)
===========================================================================
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.
Notice how self.pyoutput(feature) always comes last.
Also notice that you have to import the json module.
If you attach a Logger after this PythonCreator you should get:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Feature Type: `Logger_LOGGED'
Attribute(string): `fme_feature_type' has value `PythonCreator'
Attribute(string): `my_json' has value `{"Commune": "Tokyo"}'
Geometry Type: Unknown (0)
===========================================================================
Hope this helps.
Perfect. Thanks a lot.
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.
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 ;)
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.