Skip to main content
Solved

extracting a substring using python caller

  • December 8, 2016
  • 6 replies
  • 127 views

arthy
Contributor
Forum|alt.badge.img+8

Hi,

I wrote a very long workspace and at some point I want to extract a substring of the filename using the python caller and have it as a new attribute in my workspace.

Then I tried to write a workspace with only that instruction (see attached) but I'm still having the same error:

Python Exception <KeyError>: 'fme_basename'

Error executing string `fme_basename=FME_MacroValues['fme_basename']


def thm(fme_basename):

    return fme_basename[4:]

'

Factory proxy not initialized

PythonFactory failed to process feature

PythonFactory failed to process feature
test.fmw

Any idea of what could be the problem?

Thanks

Best answer by david_r

Try this:

def thm(feature):
    basename = feature.getAttribute('fme_basename')
    if basename:
        basename = basename[4:]
        feature.setAttribute('fme_basename', basename)

Let me know if you don't understand what, how, etc.

View original
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.

6 replies

david_r
Celebrity
  • Best Answer
  • December 8, 2016

Try this:

def thm(feature):
    basename = feature.getAttribute('fme_basename')
    if basename:
        basename = basename[4:]
        feature.setAttribute('fme_basename', basename)

Let me know if you don't understand what, how, etc.


courtney_m
Contributor
Forum|alt.badge.img+23
  • Contributor
  • December 8, 2016

You could use a SubstringExtractor within the long workspace, instead of the PythonCaller:

And you can set the Result Attribute name to whatever you want.


david_r
Celebrity
  • December 8, 2016

You could use a SubstringExtractor within the long workspace, instead of the PythonCaller:

And you can set the Result Attribute name to whatever you want.

Yup, definitely a lot easier.

arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • December 9, 2016

You could use a SubstringExtractor within the long workspace, instead of the PythonCaller:

And you can set the Result Attribute name to whatever you want.

Thanks @courtney_m.

 

It's a good alternative but it does not help me to understand the problem with the python caller.

 

 


arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • December 9, 2016

Try this:

def thm(feature):
    basename = feature.getAttribute('fme_basename')
    if basename:
        basename = basename[4:]
        feature.setAttribute('fme_basename', basename)

Let me know if you don't understand what, how, etc.

@david_r,

 

 

Now I see my error.

 

 

I have also tried using fme macro values to get the attribute and I see with your suggestion that the good one is feature.getAttribute.

 

 

Many thanks

 

 


david_r
Celebrity
  • December 9, 2016
@david_r,

 

 

Now I see my error.

 

 

I have also tried using fme macro values to get the attribute and I see with your suggestion that the good one is feature.getAttribute.

 

 

Many thanks

 

 

The FME_MacroValues dictionary is for published/private parameters only, where as the getAttribute() and setAttribute() methods are only for feature attributes.

 

 

If you want to learn more, I recommend this free webinar: http://www.safe.com/webinars/python-scripting-in-fme/