-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Akshay Raj Gollahalli <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <[email protected]>
- Loading branch information
1 parent
19b43d3
commit d7f5dc5
Showing
7 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Lists | ||
|
||
::: humanize.lists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Lists related humanization.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
__all__ = ["natural_list"] | ||
|
||
|
||
def natural_list(items: list[Any]) -> str: | ||
"""Natural list. | ||
Convert a list of items into a human-readable string with commas and 'and'. | ||
Examples: | ||
>>> natural_list(["one", "two", "three"]) | ||
'one, two and three' | ||
>>> natural_list(["one", "two"]) | ||
'one and two' | ||
>>> natural_list(["one"]) | ||
'one' | ||
Args: | ||
items (list): An iterable of items. | ||
Returns: | ||
str: A string with commas and 'and' in the right places. | ||
""" | ||
if len(items) == 1: | ||
return str(items[0]) | ||
elif len(items) == 2: | ||
return f"{str(items[0])} and {str(items[1])}" | ||
else: | ||
return ", ".join(str(item) for item in items[:-1]) + f" and {str(items[-1])}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
import humanize | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_args, expected", | ||
[ | ||
([["1", "2", "3"]], "1, 2 and 3"), | ||
([["one", "two", "three"]], "one, two and three"), | ||
([["one", "two"]], "one and two"), | ||
([["one"]], "one"), | ||
([[""]], ""), | ||
([[1, 2, 3]], "1, 2 and 3"), | ||
([[1, "two"]], "1 and two"), | ||
], | ||
) | ||
def test_natural_list( | ||
test_args: list[str] | list[int] | list[str | int], expected: str | ||
) -> None: | ||
assert humanize.natural_list(*test_args) == expected |