Skip to content

FormatClass

exprint.formatter.FormatClass

FormatClass(class_name: str, formatter: Formatter)

Class for formatting classes.

Parameters:

Name Type Description Default

class_name

str

Class name

required
Source code in exprint/formatter.py
def __init__(self, class_name: str, formatter: Formatter):
    super().__init__(formatter, formatter.format_value().value(class_name))

_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.

field

field(name: str, value: Any) -> FormatClass

Adds a field to the class output.

Parameters:

Name Type Description Default

name

str

Name of the field.

required

value

Any

Value of the field.

required

Returns:

Type Description
FormatClass

Itself

Examples:

>>> f = Formatter()
>>> f.format_class("MyClass").field("foo", obj.foo)
Source code in exprint/formatter.py
def field(self, name: str, value: Any) -> FormatClass:
    """
    Adds a field to the class output.

    Parameters
    ----------
    name : str
        Name of the field.
    value : Any
        Value of the field.

    Returns
    -------
    FormatClass
        Itself

    Examples
    --------

    >>> f = Formatter()
    >>> f.format_class("MyClass").field("foo", obj.foo)
    """
    return self.field_with(name, lambda f: f.format_any(value))

field_with

field_with(
    name: str, value_fmt: Callable[[Formatter], Format]
) -> FormatClass

Adds a field to the class output. Equivalent to FormatClass.field but formats the value using a function.

Parameters:

Name Type Description Default

name

str

Name of the field.

required

value_fmt

Callable[[Formatter], Format]

Value function.

required

Returns:

Type Description
FormatClass

Itself

Examples:

>>> f = Formatter()
>>> f.format_class("MyClass").field_with("foo", lambda f: f.format_any(obj.foo))
Source code in exprint/formatter.py
def field_with(
    self, name: str, value_fmt: Callable[[Formatter], Format]
) -> FormatClass:
    """
    Adds a field to the class output. Equivalent to `FormatClass.field` but
    formats the value using a function.

    Parameters
    ----------
    name : str
        Name of the field.
    value_fmt : Callable[[Formatter], Format]
        Value function.

    Returns
    -------
    FormatClass
        Itself

    Examples
    --------

    >>> f = Formatter()
    >>> f.format_class("MyClass").field_with("foo", lambda f: f.format_any(obj.foo))
    """
    self._keys.append(self.formatter.format_value().value(name))
    self._values.append(value_fmt(self.formatter))
    return self