Parameters

Parameters are the DBND equivalent of creating a constructor for each task.

Most of the time, you can control your parameters' behavior by using standard Python syntax.
Parameter type and a default value can be defined like in the following example:

@task
def prepare_data(data: DataFrame=None) -> DataFrame: 
      return data

Occasionally, more info needs to be provided to the parameter definition.
So DBND introduces the ParamaterFactory concept to control it.

Every decorated task parameter can be controlled via parameter definition at function:

@task
def prepare_data(data = paramater.default(None)[DataFrame] ) -> DataFrame: 
     return data

If you want to keep your function code in touch, you can do that at the decorator.

@task(data = paramater.default(None)[DataFrame])
def prepare_data(data) -> DataFrame: 
     return data

Default Value

You can use value factory. This defines the default value and also infers parameter type for you, i.e.:

@task(alpha = parameter.value(0.5))
def calculate_alpha(alpha) -> DataFrame: 
      return data

πŸ‘

Parameter value resolution

For parameter value resolution, see DBND Object Configuration.

Parameter Types

In the examples above, the type of the parameter is determined by using dbnd.parameter.

Python is not a statically typed language and you don't have to specify the types
of any of your parameters. You can simply use the base class dbnd.parameter[object].

The reason you would use an explicit typing like parameter[datetime.date] is that DBND needs to know its type for the command line interaction. That's how it knows how to convert a string provided via the command line to the corresponding type (i.e. datetime.date instead of a string).
DBND tasks support a variety of parameters.

Python simple types are supported:

@task
def prepare_data(v_int: int, v_bool: bool, v_str: str) -> str:
    pass

datetimes types and many other basic types are also supported:

@task
def prepare_data(
    v_datetime: datetime.datetime, v_date: datetime.date, v_period: datetime.timedelta
) -> str:
    pass

A list of various types can be provided:

@task
def prepare_data(
    v_list: List,
    v_list_obj: List[object],
    v_list_str: List[str],
    v_list_int: List[int],
    v_list_date: List[datetime.date],
) -> str:
     pass

Paths can be provided using strings, pathlib Path object, or DBND target types:

@task
def prepare_data(pathlib_path: Path, str_as_path: PathStr, target_path: Target) -> str:
    assert isinstance(str_as_path, str)
    pass

If the path is input from another task, DBND will not resolve it into an "in-memory" type, but will keep the Path as an input to function.

Insignificant Parameters

If a parameter is created with significant=False, it is ignored as far as the task signature is concerned. Tasks created that differ only in insignificant parameters have the same signature and therefore are the same instance:

from dbnd import PythonTask, parameter
@task
def  calculate_alpha(alpha = parameter[int], debug_level=parameter(significant=False).value(0)[int]): 
    return alpha

In this case, changes of debug_level will not affect the task's signature.


Did this page help you?