Skip to main content
Solved

extracting a substring using python caller


arthy
Contributor
Forum|alt.badge.img+8
  • Contributor

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
Did this help you find an answer to your question?

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+12
  • 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
courtney_m wrote:

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
courtney_m wrote:

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
david_r wrote:

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
arthy wrote:
@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/

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings