Skip to content

FormatDict

exprint.formatter.FormatDict

FormatDict(formatter: Formatter)

Class for formatting dictionary of items.

Source code in exprint/formatter.py
def __init__(self, formatter: Formatter):
    super().__init__(formatter)

_cache instance-attribute

_cache = None

_class_name instance-attribute

_class_name = class_name

_keys instance-attribute

_keys = []

_values instance-attribute

_values = []

formatter instance-attribute

formatter: Formatter = formatter

Formatter class.

item

item(key: Any, value: Any) -> FormatDict

Adds a pair of key-value to the dictionary output.

Parameters:

Name Type Description Default

key

Any

Key to add.

required

value

Any

Value to add.

required

Returns:

Type Description
FormatDict

Itself

Examples:

>>> f = Formatter()
>>> key, value = list(map.items())[0]
>>> f.format_dict().item(key, value)
Source code in exprint/formatter.py
def item(self, key: Any, value: Any) -> FormatDict:
    """
    Adds a pair of key-value to the dictionary output.

    Parameters
    ----------
    key : Any
        Key to add.
    value : Any
        Value to add.

    Returns
    -------
    FormatDict
        Itself

    Examples
    --------

    >>> f = Formatter()
    >>> key, value = list(map.items())[0]
    >>> f.format_dict().item(key, value)
    """
    return self.key(key).value(value)

items

items(values: Iterable[tuple[Any, Any]]) -> FormatDict

Adds all pairs of key-values to the dictionary output.

Parameters:

Name Type Description Default

values

Iterable[tuple[Any, Any]]

Pair of key-values

required

Returns:

Type Description
FormatDict

Itself

Examples:

>>> f = Formatter()
>>> f.format_dict().items(map.items())
Source code in exprint/formatter.py
def items(self, values: Iterable[tuple[Any, Any]]) -> FormatDict:
    """
    Adds all pairs of key-values to the dictionary output.

    Parameters
    ----------
    values : Iterable[tuple[Any, Any]]
        Pair of key-values

    Returns
    -------
    FormatDict
        Itself

    Examples
    --------

    >>> f = Formatter()
    >>> f.format_dict().items(map.items())
    """
    for key, value in values:
        self.item(key, value)
    return self

key

key(key: Any) -> FormatDict

Adds a key to the dictionary output.

Parameters:

Name Type Description Default

key

Any

Key to add.

required

Returns:

Type Description
FormatDict

Itself

Examples:

>>> f = Formatter()
>>> key = list(map.keys())[0]
>>> f.format_dict().key(key)
Source code in exprint/formatter.py
def key(self, key: Any) -> FormatDict:
    """
    Adds a key to the dictionary output.

    Parameters
    ----------
    key : Any
        Key to add.

    Returns
    -------
    FormatDict
        Itself

    Examples
    --------

    >>> f = Formatter()
    >>> key = list(map.keys())[0]
    >>> f.format_dict().key(key)
    """
    return self.key_with(lambda f: f.format_any(key))

key_with

key_with(
    key_fmt: Callable[[Formatter], Format],
) -> FormatDict

Adds a key to the dictionary output. Equivalent to FormatDict.key but formats the value using a function.

Parameters:

Name Type Description Default

key_fmt

Callable[[Formatter], Format]

Key function.

required

Returns:

Type Description
FormatDict

Itself

Examples:

>>> f = Formatter()
>>> key = list(map.keys())[0]
>>> f.format_dict().key_with(lambda f: f.format_any(key))
Source code in exprint/formatter.py
def key_with(self, key_fmt: Callable[[Formatter], Format]) -> FormatDict:
    """
    Adds a key to the dictionary output. Equivalent to `FormatDict.key` but
    formats the value using a function.

    Parameters
    ----------
    key_fmt : Callable[[Formatter], Format]
        Key function.

    Returns
    -------
    FormatDict
        Itself

    Examples
    --------

    >>> f = Formatter()
    >>> key = list(map.keys())[0]
    >>> f.format_dict().key_with(lambda f: f.format_any(key))
    """
    self._keys.append(key_fmt(self.formatter))
    return self

value

value(value: Any) -> FormatDict

Adds a value to the dictionary output.

Parameters:

Name Type Description Default

value

Any

Value to add.

required

Returns:

Type Description
FormatDict

Itself

Examples:

>>> f = Formatter()
>>> value = list(map.values())[0]
>>> f.format_dict().value(value)
Source code in exprint/formatter.py
def value(self, value: Any) -> FormatDict:
    """
    Adds a value to the dictionary output.

    Parameters
    ----------
    value : Any
        Value to add.

    Returns
    -------
    FormatDict
        Itself

    Examples
    --------

    >>> f = Formatter()
    >>> value = list(map.values())[0]
    >>> f.format_dict().value(value)
    """
    return self.value_with(lambda f: f.format_any(value))

value_with

value_with(
    value_fmt: Callable[[Formatter], Format],
) -> FormatDict

Adds a value to the dictionary output. Equivalent to FormatDict.value but formats the value using a function.

Parameters:

Name Type Description Default

value_fmt

Callable[[Formatter], Format]

Value function.

required

Returns:

Type Description
FormatDict

Itself

Examples:

>>> f = Formatter()
>>> value = list(map.values())[0]
>>> f.format_dict().value_with(lambda f: f.format_any(value))
Source code in exprint/formatter.py
def value_with(self, value_fmt: Callable[[Formatter], Format]) -> FormatDict:
    """
    Adds a value to the dictionary output. Equivalent to `FormatDict.value`
    but formats the value using a function.

    Parameters
    ----------
    value_fmt : Callable[[Formatter], Format]
        Value function.

    Returns
    -------
    FormatDict
        Itself

    Examples
    --------

    >>> f = Formatter()
    >>> value = list(map.values())[0]
    >>> f.format_dict().value_with(lambda f: f.format_any(value))
    """
    self._values.append(value_fmt(self.formatter))
    return self