Skip to content

Commit d7f5dc5

Browse files
akshaybablooFPA-AkshayGollahallipre-commit-ci[bot]hugovk
authored
Add natural_list (#110)
Co-authored-by: Akshay Raj Gollahalli <akshay.gollahalli@fisherpaykel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 19b43d3 commit d7f5dc5

File tree

7 files changed

+66
-6
lines changed

7 files changed

+66
-6
lines changed

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,8 @@ dmypy.json
152152
# Cython debug symbols
153153
cython_debug/
154154

155-
# PyCharm
156-
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157-
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158-
# and can be added to the global gitignore or merged into this file. For a more nuclear
159-
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160-
#.idea/
155+
# Jetbrains IDEs
156+
.idea/
161157

162158
# hatch-vcs
163159
src/*/_version.py

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Welcome to the humanize API reference.
66
- [Time](time.md)
77
- [Filesize](filesize.md)
88
- [I18n](i18n.md)
9+
- [Lists](lists.md)
910

1011
{%
1112
include-markdown "../README.md"

docs/lists.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Lists
2+
3+
::: humanize.lists

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ nav:
2121
- Time: time.md
2222
- Filesize: filesize.md
2323
- Internationalisation: i18n.md
24+
- Lists: lists.md
2425

2526
plugins:
2627
- search

src/humanize/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from humanize.filesize import naturalsize
66
from humanize.i18n import activate, deactivate, decimal_separator, thousands_separator
7+
from humanize.lists import natural_list
78
from humanize.number import (
89
apnumber,
910
clamp,
@@ -38,6 +39,7 @@
3839
"naturaldate",
3940
"naturalday",
4041
"naturaldelta",
42+
"natural_list",
4143
"naturalsize",
4244
"naturaltime",
4345
"ordinal",

src/humanize/lists.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Lists related humanization."""
2+
3+
from __future__ import annotations
4+
5+
from typing import Any
6+
7+
__all__ = ["natural_list"]
8+
9+
10+
def natural_list(items: list[Any]) -> str:
11+
"""Natural list.
12+
13+
Convert a list of items into a human-readable string with commas and 'and'.
14+
15+
Examples:
16+
>>> natural_list(["one", "two", "three"])
17+
'one, two and three'
18+
>>> natural_list(["one", "two"])
19+
'one and two'
20+
>>> natural_list(["one"])
21+
'one'
22+
23+
Args:
24+
items (list): An iterable of items.
25+
26+
Returns:
27+
str: A string with commas and 'and' in the right places.
28+
"""
29+
if len(items) == 1:
30+
return str(items[0])
31+
elif len(items) == 2:
32+
return f"{str(items[0])} and {str(items[1])}"
33+
else:
34+
return ", ".join(str(item) for item in items[:-1]) + f" and {str(items[-1])}"

tests/test_lists.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
import humanize
6+
7+
8+
@pytest.mark.parametrize(
9+
"test_args, expected",
10+
[
11+
([["1", "2", "3"]], "1, 2 and 3"),
12+
([["one", "two", "three"]], "one, two and three"),
13+
([["one", "two"]], "one and two"),
14+
([["one"]], "one"),
15+
([[""]], ""),
16+
([[1, 2, 3]], "1, 2 and 3"),
17+
([[1, "two"]], "1 and two"),
18+
],
19+
)
20+
def test_natural_list(
21+
test_args: list[str] | list[int] | list[str | int], expected: str
22+
) -> None:
23+
assert humanize.natural_list(*test_args) == expected

0 commit comments

Comments
 (0)