2

In the Function editor I'm trying to edit a string something like:

from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Custom')
def testfunc2(value1, value2, feature, parent):
 s =trim(value1 + value2)
 return s

I get the error:

Eval Error:
global name 'trim' is not defined

Can I use functions like trim, concat etc. in the Function editor?

mgri
16.4k6 gold badges48 silver badges80 bronze badges
asked Feb 16, 2016 at 14:40

1 Answer 1

5

Yes you can.

The problem is, that there is no method trim available. But there is a method strip() available on every string. You probably want to do

s = value1 + value2 # Concat
s = s.strip() # Trim

In general, you need to import functions. So if you did not import the function trim, it will not be available.

answered Feb 16, 2016 at 14:56
5
  • Thanks, but how do I import functions like trim etc. I tried all the Qgis libraries like QGis.Utils. Commented Feb 16, 2016 at 15:09
  • Why do you think that there should be a function trim in QGIS? Normally, if it's not geo-specific, read the python documentation or look for generic python modules and if you look for geo-specific stuff read the pyqgis cookbook Commented Feb 16, 2016 at 15:15
  • I didn't but I hoped. In the QGis Expression dialog (2.12.3) you can use these functions for a regular expression. So I assumed they were also usable in the function editor which is also in the Expression Dialog. Commented Feb 16, 2016 at 15:52
  • 2
    Expression functions are not usable in the function editor. The function editor is pure python. But no reason to worry, python covers way more functionality than there are expression functions. Commented Feb 16, 2016 at 15:55
  • 1
    I found some good documented examples of python code in apps\python27\lib\string.py. Thank you for your time and quick response. Commented Feb 16, 2016 at 17:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.