Skip to content

format

formatter

formatter(obj, format='str')

format an object for printing to the screen

See FORMAT_OPTIONS for available formats. If format is not specified the str representation of the object is returned.

Source code in arrakis/format.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def formatter(obj: Any, format: str = "str"):  # noqa: A002
    """format an object for printing to the screen

    See FORMAT_OPTIONS for available formats.  If format is not
    specified the str representation of the object is returned.

    """
    if format == "str":
        return str(obj)
    if format == "repr":
        return repr(obj)
    if format == "pprint":
        return pformat(obj)
    if format == "json":
        return json.dumps(obj, default=_obj_encode)
    if format == "json-pp":
        return json.dumps(obj, default=_obj_encode, indent=4)
    if format == "json-abrv":
        return json.dumps(
            obj,
            default=partial(_obj_encode, abrv=True),
            indent=4,
        )
    msg = f"Unknown format: {format}"
    raise ValueError(msg)