3

I’d like to write a QGIS Expression function in Python that takes a list of strings and joins them with a separator that can be given as an optional named argument sep which also has a default value. For example, I would use it in an expression with the default separator concat_sep('a', 'b', 'c') or with a custom separator concat_sep('a', 'b', 'c', sep := ' - ').

How can I pass Qgis expression named parameters to Python? My Python code currently is:

@qgsfunction(args='auto', group='custom', handlesnull=True, usesGeometry=False)
def concat_sep(*strings, sep=', ', feature, parent):
    non_empty_strings = filter(lambda s: s is not None and s != '', strings)
    if not non_empty_strings:
        return ''
    result = sep.join(non_empty_strings)
    return result

It works fine when using the default separator, i.e. writing an expression without sep := …. But when I try to use sep := …, there’s an error popup in the expression editor that says “concat_sep does not support named QgsExpressionFunction::Parameters”.

How can I define a named parameter in the expression that is passed through to the Python function? Is it at all possible? I guess something needs to happen in the @qgsfunction decorator, but I couldn’t find anything in the documentation.

1
  • 1
    As far as I remember QGIS's expression parser does not support named arguments in function calls Commented Jul 2 at 14:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.