Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repr_sequence #287

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "coola"
version = "0.0.25a2"
version = "0.0.25a3"
description = "A library to check if two complex/nested objects are equal or not"
readme = "README.md"
authors = ["Thibaut Durand <[email protected]>"]
Expand Down
31 changes: 30 additions & 1 deletion src/coola/utils/format.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

__all__ = ["repr_indent", "str_indent", "str_mapping", "str_sequence"]
__all__ = ["repr_indent", "repr_indent", "str_indent", "str_mapping", "str_sequence"]

from collections.abc import Mapping, Sequence
from typing import Any
Expand Down Expand Up @@ -40,6 +40,35 @@ def repr_indent(original: Any, num_spaces: int = 2) -> str:
return str_indent(original, num_spaces)


def repr_sequence(sequence: Sequence, num_spaces: int = 2) -> str:
r"""Computes a string representation of a sequence.

Args:
----
sequence (``Sequence``): Specifies the sequence.
num_spaces (int, optional): Specifies the number of spaces
used for the indentation. Default: ``2``.

Returns:
-------
str: The string representation of the sequence.

Example usage:

.. code-block:: pycon

>>> from coola.utils.format import repr_indent
>>> print(repr_sequence(["abc", "something\nelse"]))
(0): abc
(1): something
else
"""
lines = []
for i, item in enumerate(sequence):
lines.append(f"({i}): {repr_indent(item, num_spaces=num_spaces)}")
return "\n".join(lines)


def str_indent(original: Any, num_spaces: int = 2) -> str:
r"""Add indentations if the original string is a multi-lines string.

Expand Down
29 changes: 28 additions & 1 deletion tests/unit/utils/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

from pytest import raises

from coola.utils.format import repr_indent, str_indent, str_mapping, str_sequence
from coola.utils.format import (
repr_indent,
repr_sequence,
str_indent,
str_mapping,
str_sequence,
)

#################################
# Tests for repr_indent #
Expand Down Expand Up @@ -38,6 +44,27 @@ def test_repr_indent_not_a_repring() -> None:
assert repr_indent(123) == "123"


###################################
# Tests for repr_sequence #
###################################


def test_repr_sequence_empty() -> None:
assert repr_sequence([]) == ""


def test_repr_sequence_1_item() -> None:
assert repr_sequence(["abc"]) == "(0): abc"


def test_repr_sequence_2_items() -> None:
assert repr_sequence(["abc", 123]) == "(0): abc\n(1): 123"


def test_repr_sequence_2_items_multiple_line() -> None:
assert repr_sequence(["abc", "something\nelse"]) == "(0): abc\n(1): something\n else"


################################
# Tests for str_indent #
################################
Expand Down