diff --git a/ignite/utils.py b/ignite/utils.py index 0de0f5737100..7961130108e8 100644 --- a/ignite/utils.py +++ b/ignite/utils.py @@ -128,21 +128,54 @@ def flatten_to_str(data: Any) -> List[str]: structure. Examples: - >>> flatten_to_str(42) - ['42.0000'] + Use the function to flatten and format various data structures. - >>> flatten_to_str([1, 2.34567, 3]) - ['1.0000, 2.3457, 3.0000'] + .. testsetup:: * - >>> flatten_to_str({'a': 1, 'b': [2, 3], 'c': None}) - ['a: 1.0000', 'b: 2.0000, 3.0000', 'c: '] + from ignite.utils import flatten_to_str + import torch - >>> flatten_to_str(torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])) - ['[1.0000, 2.0000, 3.0000, 4.0000, 5.0000, 6.0000, 7.0000, 8.0000, 9.0000, 10.0000, ...]'] + .. testcode:: - >>> flatten_to_str({'tensor': torch.tensor([[1, 2], [3, 4]])}) - ['tensor: Shape: torch.Size([2, 2])'] + flatten_to_str(42) + + .. testoutput:: + + ['42.0000'] + + .. testcode:: + + flatten_to_str([1, 2.34567, 3]) + + .. testoutput:: + + ['1.0000, 2.3457, 3.0000'] + + .. testcode:: + + flatten_to_str({'a': 1, 'b': [2, 3], 'c': None}) + + .. testoutput:: + + ['a: 1.0000', 'b: 2.0000, 3.0000', 'c: '] + + .. testcode:: + + flatten_to_str(torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])) + + .. testoutput:: + + ['[1.0000, 2.0000, 3.0000, 4.0000, 5.0000, 6.0000, 7.0000, 8.0000, 9.0000, 10.0000, ...]'] + + .. testcode:: + + flatten_to_str({'tensor': torch.tensor([[1, 2], [3, 4]])}) + + .. testoutput:: + + ['tensor: Shape: torch.Size([2, 2])'] """ + formatted_items: List[str] = [] def format_item(item: Any, prefix: str = "") -> Optional[str]: