Script Function#

This module provides base classes for functions used in Script instances.

These functions allow dynamic script configuration and are essential to the scripting process.

class BaseScriptFunc(**data)[source]#

Bases: BaseModel, ABC

Base class for any script function.

Defines wrapped_call() that wraps call() and handles exceptions and types conversions.

return_type: ClassVar[Union[type, Tuple[type, ...]]]#

Return type of the script function.

abstract async call(ctx)[source]#

Implement this to create a custom function.

async wrapped_call(ctx, *, info='')[source]#

Exception-safe wrapper for __call__().

Returns:

An instance of return_type if possible. Otherwise, an Exception instance detailing what went wrong.

async __call__(ctx)[source]#

Handle call():

  • Call it (regardless of whether it is async);

  • Cast returned value to return_type.

Returns:

An instance of return_type.

Raises:

TypeError – If call() returned value of incorrect type.

class ConstScriptFunc(**data)[source]#

Bases: BaseScriptFunc

Base class for script functions that return a constant value.

root: None#

Value to return.

async call(ctx)[source]#

Implement this to create a custom function.

classmethod validate_value(data)[source]#

Allow instantiating this class from its root value.

class BaseCondition(**data)[source]#

Bases: BaseScriptFunc, ABC

Base class for condition functions.

These are used in chatsky.core.transition.Transition.cnd.

return_type#

alias of bool

abstract async call(ctx)[source]#

Implement this to create a custom function.

Return type:

bool

async wrapped_call(ctx, *, info='')[source]#

Exception-safe wrapper for __call__().

Return type:

Union[bool, Exception]

Returns:

An instance of return_type if possible. Otherwise, an Exception instance detailing what went wrong.

async __call__(ctx)[source]#

Handle call():

  • Call it (regardless of whether it is async);

  • Cast returned value to return_type.

Return type:

bool

Returns:

An instance of return_type.

Raises:

TypeError – If call() returned value of incorrect type.

async is_true(ctx, *, info='')[source]#

Same as wrapped_call() but instead of exceptions return False.

Return type:

bool

class ConstCondition(**data)[source]#

Bases: ConstScriptFunc, BaseCondition

root: bool#

Value to return.

AnyCondition#

A type annotation that allows accepting both ConstCondition and BaseCondition while validating ConstCondition if possible.

alias of Union[ConstCondition, BaseCondition][Union[ConstCondition, BaseCondition]]

class BaseResponse(**data)[source]#

Bases: BaseScriptFunc, ABC

Base class for response functions.

These are used in chatsky.core.script.Node.response.

return_type#

alias of Message

abstract async call(ctx)[source]#

Implement this to create a custom function.

Return type:

Union[Message, dict, str]

async wrapped_call(ctx, *, info='')[source]#

Exception-safe wrapper for __call__().

Return type:

Union[Message, Exception]

Returns:

An instance of return_type if possible. Otherwise, an Exception instance detailing what went wrong.

async __call__(ctx)[source]#

Handle call():

  • Call it (regardless of whether it is async);

  • Cast returned value to return_type.

Return type:

Message

Returns:

An instance of return_type.

Raises:

TypeError – If call() returned value of incorrect type.

class ConstResponse(**data)[source]#

Bases: ConstScriptFunc, BaseResponse

root: Message#

Value to return.

AnyResponse#

A type annotation that allows accepting both ConstResponse and BaseResponse while validating ConstResponse if possible.

alias of Union[ConstResponse, BaseResponse][Union[ConstResponse, BaseResponse]]

class BaseDestination(**data)[source]#

Bases: BaseScriptFunc, ABC

Base class for destination functions.

These are used in chatsky.core.transition.Transition.dst.

return_type#

alias of AbsoluteNodeLabel

abstract async call(ctx)[source]#

Implement this to create a custom function.

Return type:

Union[NodeLabel, str, Tuple[str, str], List[str], dict]

async wrapped_call(ctx, *, info='')[source]#

Exception-safe wrapper for __call__().

Return type:

Union[AbsoluteNodeLabel, Exception]

Returns:

An instance of return_type if possible. Otherwise, an Exception instance detailing what went wrong.

async __call__(ctx)[source]#

Handle call():

  • Call it (regardless of whether it is async);

  • Cast returned value to return_type.

Return type:

AbsoluteNodeLabel

Returns:

An instance of return_type.

Raises:

TypeError – If call() returned value of incorrect type.

class ConstDestination(**data)[source]#

Bases: ConstScriptFunc, BaseDestination

root: NodeLabel#

Value to return.

AnyDestination#

A type annotation that allows accepting both ConstDestination and BaseDestination while validating ConstDestination if possible.

alias of Union[ConstDestination, BaseDestination][Union[ConstDestination, BaseDestination]]

class BaseProcessing(**data)[source]#

Bases: BaseScriptFunc, ABC

Base class for processing functions.

These are used in chatsky.core.script.Node.pre_transition and chatsky.core.script.Node.pre_response.

return_type#

alias of None

abstract async call(ctx)[source]#

Implement this to create a custom function.

Return type:

None

async wrapped_call(ctx, *, info='')[source]#

Exception-safe wrapper for __call__().

Return type:

Optional[Exception]

Returns:

An instance of return_type if possible. Otherwise, an Exception instance detailing what went wrong.

async __call__(ctx)[source]#

Handle call():

  • Call it (regardless of whether it is async);

  • Cast returned value to return_type.

Return type:

None

Returns:

An instance of return_type.

Raises:

TypeError – If call() returned value of incorrect type.

class BasePriority(**data)[source]#

Bases: BaseScriptFunc, ABC

Base class for priority functions.

These are used in chatsky.core.transition.Transition.priority.

Has several possible return types:

  • float: Transition successful with the corresponding priority;

  • True or None: Transition successful with the default_priority;

  • False: Transition unsuccessful.

return_type: ClassVar[Union[type, Tuple[type, ...]]] = (<class 'float'>, <class 'NoneType'>, <class 'bool'>)#

Return type of the script function.

abstract async call(ctx)[source]#

Implement this to create a custom function.

Return type:

Union[float, bool, None]

async wrapped_call(ctx, *, info='')[source]#

Exception-safe wrapper for __call__().

Return type:

Union[float, bool, None, Exception]

Returns:

An instance of return_type if possible. Otherwise, an Exception instance detailing what went wrong.

async __call__(ctx)[source]#

Handle call():

  • Call it (regardless of whether it is async);

  • Cast returned value to return_type.

Return type:

Union[float, bool, None]

Returns:

An instance of return_type.

Raises:

TypeError – If call() returned value of incorrect type.

class ConstPriority(**data)[source]#

Bases: ConstScriptFunc, BasePriority

root: Optional[float]#

Value to return.

AnyPriority#

A type annotation that allows accepting both ConstPriority and BasePriority while validating ConstPriority if possible.

alias of Union[ConstPriority, BasePriority][Union[ConstPriority, BasePriority]]