Skip to content

Commit

Permalink
use doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
leej3 committed Jun 19, 2024
1 parent 29004ae commit 466ea8e
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions ignite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit 466ea8e

Please sign in to comment.