lenskit.data.repr#

Utility functions for implementing __str__ and __repr__ methods with consistent syntax.

Classes#

HasObjectRepr

Base class for protocol classes.

ReprWriter

ReprIndenter

ObjectRepr

Functions#

object_repr(tag, *words[, comment])

Construct string "object" representations.

Module Contents#

class lenskit.data.repr.HasObjectRepr#

Bases: Protocol

Base class for protocol classes.

Protocol classes are defined as:

class Proto(Protocol):
    def meth(self) -> int:
        ...

Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).

For example:

class C:
    def meth(self) -> int:
        return 0

def func(x: Proto) -> int:
    return x.meth()

func(C())  # Passes static type check

See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:

class GenProto[T](Protocol):
    def meth(self) -> T:
        ...
class lenskit.data.repr.ReprWriter#
out: io.StringIO#
current_indent: int = 0#
newline: bool = True#
write(text)#
Parameters:

text (str)

writeln(text='')#
Parameters:

text (str)

string()#
Return type:

str

indent(*, size=2)#
Parameters:

size (int)

class lenskit.data.repr.ReprIndenter(writer, size)#
Parameters:
writer: ReprWriter#
size: int#
__enter__()#
__exit__(exc_type, exc_val, exc_tb)#
Parameters:
class lenskit.data.repr.ObjectRepr#
tag: str#
words: collections.abc.Sequence[str] = ()#
comment: str | None = None#
attrs: collections.abc.Mapping[str, Any] | None = None#
write(writer)#
Parameters:

writer (ReprWriter)

string()#
Return type:

str

lenskit.data.repr.object_repr(tag, *words, comment=None, **attrs)#

Construct string “object” representations.

Parameters:
  • tag (str)

  • words (str)

  • comment (str | None)

  • attrs (Any)

Return type:

ObjectRepr