Skip to content

Variable

Class for handling Variable substitutions.

Variable

Bases: Substitution

Environment Variable class capable of substituting itself into strings.

Derived from the Substitution EnvObject class which requires that a substitution be able to inject itself into data.

Source code in maestrowf/datastructures/environment/variable.py
class Variable(Substitution):
    """
    Environment Variable class capable of substituting itself into strings.

    Derived from the Substitution EnvObject class which requires that a
    substitution be able to inject itself into data.
    """

    def __init__(self, name, value, token='$'):
        """
        Initialize the Variable class.

        The Variable represents substrings that can be present within String
        data that are meant to be replaced. The general format that such items
        take is generally expressed as '<token>(<name>)', and will be replaced
        with the value specified.

        :params name: String name that refers to a Variable instance.
        :params value: The value to substitute for the Variable instance.
        :params token: String of expected character(s) that appear at the
            beginning of a substring representing the variable.
        """
        self.name = name
        self.value = value
        self.token = token

        if not self._verify():
            msg = "Variable initialized without complete settings. Set " \
                           "required [name, value] before calling methods."
            logger.exception(msg)
            raise ValueError(msg)

    def get_var(self):
        """
        Get the variable representation of the variable's name.

        :returns: String of the Variable's name in token form.
        """
        return "{}({})".format(self.token, self.name)

    def substitute(self, data):
        """
        Substitute the variable's value for its notation.

        :param data: String to substitute variable into.
        :returns: String with the variable's name replaced with its value.
        """
        self._verification("Attempting to substitute a variable that is not"
                           " complete.")
        logger.debug("%s: %s", self.get_var(),
                     data.replace(self.get_var(), str(self.value)))
        return data.replace(self.get_var(), str(self.value))

    def _verify(self):
        """
        Verify that the necessary Variable fields are populated.

        :returns: True if Variable is valid, False otherwise.
        """
        _valid = bool(self.name) and self.value is not None
        return _valid

    def __str__(self):
        """
        Generate the string representation of the objects.

        :returns: A string with the token form of the variable.
        """
        return self.get_var()

__init__(name, value, token='$')

Initialize the Variable class.

The Variable represents substrings that can be present within String data that are meant to be replaced. The general format that such items take is generally expressed as '()', and will be replaced with the value specified.

Parameters:

Name Type Description Default
name

String name that refers to a Variable instance.

required
value

The value to substitute for the Variable instance.

required
token

String of expected character(s) that appear at the beginning of a substring representing the variable.

'$'
Source code in maestrowf/datastructures/environment/variable.py
def __init__(self, name, value, token='$'):
    """
    Initialize the Variable class.

    The Variable represents substrings that can be present within String
    data that are meant to be replaced. The general format that such items
    take is generally expressed as '<token>(<name>)', and will be replaced
    with the value specified.

    :params name: String name that refers to a Variable instance.
    :params value: The value to substitute for the Variable instance.
    :params token: String of expected character(s) that appear at the
        beginning of a substring representing the variable.
    """
    self.name = name
    self.value = value
    self.token = token

    if not self._verify():
        msg = "Variable initialized without complete settings. Set " \
                       "required [name, value] before calling methods."
        logger.exception(msg)
        raise ValueError(msg)

__str__()

Generate the string representation of the objects.

Returns:

Type Description

A string with the token form of the variable.

Source code in maestrowf/datastructures/environment/variable.py
def __str__(self):
    """
    Generate the string representation of the objects.

    :returns: A string with the token form of the variable.
    """
    return self.get_var()

get_var()

Get the variable representation of the variable's name.

Returns:

Type Description

String of the Variable's name in token form.

Source code in maestrowf/datastructures/environment/variable.py
def get_var(self):
    """
    Get the variable representation of the variable's name.

    :returns: String of the Variable's name in token form.
    """
    return "{}({})".format(self.token, self.name)

substitute(data)

Substitute the variable's value for its notation.

Parameters:

Name Type Description Default
data

String to substitute variable into.

required

Returns:

Type Description

String with the variable's name replaced with its value.

Source code in maestrowf/datastructures/environment/variable.py
def substitute(self, data):
    """
    Substitute the variable's value for its notation.

    :param data: String to substitute variable into.
    :returns: String with the variable's name replaced with its value.
    """
    self._verification("Attempting to substitute a variable that is not"
                       " complete.")
    logger.debug("%s: %s", self.get_var(),
                 data.replace(self.get_var(), str(self.value)))
    return data.replace(self.get_var(), str(self.value))