variable#

Description#

This plugin allows you to manipulate context variables that can be accessed across your tasks. It requires the platypush.plugins.db and platypush.plugins.redis plugins to be enabled, as the variables will be stored either persisted on a local database or on the local Redis instance.

Configuration#

variable:
    # No configuration required

Actions#

Module reference#

class platypush.plugins.variable.VariablePlugin(*_, **__)[source]#

Bases: Plugin, EntityManager

This plugin allows you to manipulate context variables that can be accessed across your tasks. It requires the platypush.plugins.db and platypush.plugins.redis plugins to be enabled, as the variables will be stored either persisted on a local database or on the local Redis instance.

__init__(**kwargs)[source]#
delete(name: str)[source]#

Delete a variable from the database.

Unlike unset(), this method actually deletes the record from the database instead of setting it to null.

Parameters:

name – Name of the variable to remove.

expire(name: str, expire: int)[source]#

Set a variable expiration on Redis

Parameters:
  • name – Variable name

  • expire – Expiration time in seconds

get(name: str | None = None, default_value=None)[source]#

Get the value of a variable by name from the local db.

Parameters:
  • name – Variable name. If not specified, all the stored variables will be returned.

  • default_value – What will be returned if the variable is not defined (default: None)

Returns:

A map in the format {"<name>":"<value>"}

mget(name: str)[source]#

Get the value of a variable by name from Redis.

Parameters:

name – Variable name

Returns:

A map in the format {"<name>":"<value>"}

mset(**kwargs)[source]#

Set a variable or a set of variables on Redis.

Parameters:

kwargs – Key-value list of variables to set (e.g. foo='bar', answer=42)

Returns:

A map with the set variables

munset(name: str)[source]#

Unset a Redis variable by name if it’s set

Parameters:

name – Name of the variable to remove

publish_entities(entities: Collection[Any] | None, callback: Callable[[Entity], Any] | None = None, **kwargs) Collection[Entity]#

Publishes a list of entities. The downstream consumers include:

It also accepts an optional callback that will be called when each of the entities in the set is flushed to the database.

You usually don’t need to override this class (but you may want to extend transform_entities() instead if your extension doesn’t natively handle Entity objects).

set(**kwargs)[source]#

Set a variable or a set of variables on the local db.

Parameters:

kwargs – Key-value list of variables to set (e.g. foo='bar', answer=42)

status(*_, **__)[source]#

All derived classes should implement this method.

At the very least, this method should refresh the current state of the integration’s entities and call publish_entities().

It should also return the current state of the entities as a list of serialized entities, if possible.

transform_entities(entities: dict | Iterable) Collection[Variable][source]#

This method takes a list of entities in any (plugin-specific) format and converts them into a standardized collection of Entity objects. Since this method is called by publish_entities() before entity updates are published, you may usually want to extend it to pre-process the entities managed by your extension into the standard format before they are stored and published to all the consumers.

unset(name: str)[source]#

Unset a variable by name if it’s set on the local db.

Unlike delete(), this method only sets the record to null instead of removing it from the database.

Parameters:

name – Name of the variable to remove.

platypush.plugins.variable.ensure_initialized(f: Callable[[...], Any])[source]#

Ensures that the entities engine has been initialized before reading/writing the db.

It also performs lazy initialization of the variables in the local cache.