14

When writing PyQGIS scripts, especially processing script and expression function, I come across the term context. I use it, but I don't know what all contexts mean actually.

There are also Qgs***Context classes. For example, QgsExpressionContext, QgsSymbolWidgetContext, QgsProcessingContext, QgsAttributeEditorContext, ... .

What does context refer in all those class names and in some methods in PyQGIS?

3
  • 1
    This question is different from What are parameters and context in PyQGIS processAlgorithm function?. The other is about how to define our own context variable. Commented Dec 25, 2021 at 13:39
  • 4
    In basic software engineering, "context" is used to connote ancillary information needed to generate results, usually dynamic, and associated with processing or the state of processing. It is especially used in interfaces where a developer provides a user function, as custom data to be provided to the custom function to maintain state information on the progress of execution. There's usually a setter for a function in the form of setFunction(int (*fn)(void *data, void *context), void *context) (where data is something to be processed (a row or object), and context is user-provided).
    – Vince
    Commented Dec 25, 2021 at 14:30
  • 2
    see this one : stackoverflow.com/questions/6145091/… Commented Dec 22, 2022 at 13:06

1 Answer 1

8
+100

I would like to explain as far as I know what context means.

Context can be seen as a bucket to pass information around.(Ref)

Let's say you write a function (funcA) which calls another function (funcB) and passes multiple arguments. And funcB calls a funcC, and passes additional arguments with argument taken from funcA.

def funcA ():
       
    param1 = "value1"
    param2 = "value2"
    ...
    param10 = "value10"
    
    funcB(param1, param2, ... ,param10)


def funcB(param1, param2, ... param10):

    param11 = "value11"
    ...
    param20 = "value20"

    funcC("ANY_VALUE", param1, ..., param10, param11, ... param20) 


def funcC(PARAM, param1, param2, ..., param20):

    # use PARAM
    # use other parameters if needed.

In that case, funcC access all information about the environment by means of funcA and funcB. As you imagine, passing multiple arguments is not a preferable way. So developers wrap all information, which funcC may need, into one container. That container can be a key:value pairs (dictionary in Python), a class, or another thing.

Let's look at how we pass parameters using a dictionary or a class.

Using dictionary:

def funcA ():
       
    dictionary = {"param1": "value1", ..., "param10": "value10"}
    
    funcB(dictionary)


def funcB(dictionary):

    new_dictionary = {"param11": "value11", ..., "param20": "value20"}
    dictionary.update(new_dictionary)

    funcC("ANY_VALUE", dictionary) 


def funcC(PARAM, dictionary):

    #
    #

Using class:

class Information():
    pass


def funcA ():
       
    information = Information()
    information.param1 = "value1"
    ...
    information.param10 = "value10"
    
    funcB(information)


def funcB(information):

    information.param11 = "value11"
    ...
    information.param20 = "value20"

    funcC("ANY_VALUE", information) 


def funcC(PARAM, information):

    #
    #

You see? You send all information as a bucket to funcC. So, what is the context here? Well! information and dictionary have the same duty as context, which is passing information.

Now let's get to PyQGIS part.

Define a function in Expression Function Editor:

@qgsfunction(args='auto', group='Custom')
def my_func(feature, parent, context):
    print(context)

and call my_func in Expression window. You will get a QgsExpressionContext reference. This context variable provides you information related to the Expression environment.

enter image description here

Then make a new processing script (Processing Toolbox > Script button > Create New Script from Template), and define processAlgorithm as follows and run:

def processAlgorithm(self, parameters, context, feedback):
    
    print(context)
    
    return {}

It prints the reference of a QgsProcessingContext instance. That means, this context provides you all information related to the Processing environment, which you may need while scripting a processing algorithm.

enter image description here

In a nutshell, if you see a context variable, it is fantastic. You should be glad that you can easily access information about the environment. Otherwise, you would have to write the relevant code yourself when you needed that information.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.