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.
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.
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.
context
variable.setFunction(int (*fn)(void *data, void *context), void *context)
(wheredata
is something to be processed (a row or object), andcontext
is user-provided).