pydecorium.decorator#

class pydecorium.Decorator(*, activated: bool = True, signature_name_format: str = '{name}')[source]#

Base class for decorators.

The subclasses must implement the _wrapper method with the following signature:

def _wrapper(self, func, *args, **kwargs):
    pre_execute()
    outputs = func(*args, **kwargs)
    post_execute()
    return outputs

The subclasses can access several attributes about the function to decorate:

  • function_signature_name: The signature name of the function. The signature name can be set using the signature_name_format attribute.

Parameters:
  • activated (bool, optional) – The activation status of the decorator. Default value is True

  • signature_name_format (str, optional) – The format of the signature name to display. (see set_signature_name_format()) Default value is “{name}”

property activated: bool#

Getter and setter for the activation status of the decorator.

Parameters:

activated (bool) – The activation status of the decorator.

Raises:

TypeError – If the given argument is not a booleen.

check_signature_name_format(signature_name_format: str) bool[source]#

Check if the signature name format is correct.

Parameters:

signature_name_format (str) – The signature name format to check.

Returns:

is_correct – If the signature name format is correct.

Return type:

bool

Raises:

TypeError – If the given argument is not a string.

get_signature_name(func) str[source]#

Get the signature name of the function.

Parameters:

func (function) – The function to get the signature name from.

Returns:

signature_name – The signature name of the function.

Return type:

str

get_signature_name_format() str[source]#

Get the signature name format of the decorator.

Note

The signature name format can be get using the signature_name_format property: decorator.signature_name_format

See also

Returns:

signature_name_format – The format of the signature name to display.

Return type:

str

is_activated() bool[source]#

Returns decorator activation status.

Note

The activation status can also be get using the activated property: decorator.activated

See also

Returns:

activated – If the decorator is activated.

Return type:

bool

is_deactivated() bool[source]#

Returns decorator deactivation status.

Note

The deactivation status can also be get using the activated property: not decorator.activated

See also

Returns:

deactivated – If the decorator is deactivated.

Return type:

bool

set_activated(activated: bool = True) None[source]#

Sets the decorator activation status.

Note

The activation status can also be set using the activated property: decorator.activated = True

See also

Parameters:

activated (bool, optional) – The activation status to apply to the decorator. Default value is True

Raises:

TypeError – If the given argument is not a booleen.

set_deactivated(deactivated: bool = True) None[source]#

Sets the decorator deactivation status.

Note

The deactivation status can also be set using the activated property: decorator.activated = False

See also

Parameters:

deactivated (bool, optional) – The deactivation status to apply to the decorator. Default value is True

Raises:

TypeError – If the given argument is not a booleen.

set_signature_name_format(signature_name_format: str) None[source]#

Sets the signature name format of the decorator.

Note

The signature name format can be set using the signature_name_format property: decorator.signature_name_format = “{name}”

Important

The signature name can be formatted using the following arguments:

  • {name}: The name of the function

  • {module}: The module of the function

  • {qualname}: The qualname of the function

Example of valid signature_name_format:

  • “{name}”: Display the name of the function.

  • “{module}.{qualname}”: Display the module and qualname of the function.

  • “Function {name} from {module}”: Display a custom message with the function name and module.

  • “{name} and {other}”: Display the string “{other}” (use to escape the brackets).

  • “{other {name} other}”: Display the string “{other {name} other}” (use to escape the brackets).

Example of invalid signature_name_format:

  • “{other}”: The format argument “other” is not valid.

  • “{name} and {qualname”: The brackets are not correctly closed.

See also

Parameters:

signature_name_format (str) – The format of the signature name to display.

Raises:
  • ValueError – If the given signature name format is not correct.

  • TypeError – If the given argument is not a string.

property signature_name_format: str#

Getter and setter for the format of the signature name to display.

Parameters:

signature_name_format (str) – The format of the signature name to display.

Raises:
  • ValueError – If the given signature name format is not correct.

  • TypeError – If the given argument is not a string.