Skip to content

Commit 4155a68

Browse files
committed
Introduce oxford comma function
1 parent 236e4b4 commit 4155a68

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/molecule/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def collection(self) -> CollectionData | None:
281281
LOG.warning(
282282
"The detected galaxy.yml file (%s) is incomplete, missing %s",
283283
galaxy_file,
284-
missing_keys,
284+
util.oxford_comma(missing_keys),
285285
)
286286
return None
287287

src/molecule/util.py

+22
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,25 @@ def print_as_yaml(data: object) -> None:
597597
# https://github.com/Textualize/rich/discussions/990#discussioncomment-342217
598598
result = Syntax(code=safe_dump(data), lexer="yaml", background_color="default")
599599
console.print(result)
600+
601+
602+
def oxford_comma(listed: Iterable[bool | str | Path], condition: str = "and") -> str:
603+
"""Format a list into a sentence.
604+
605+
Args:
606+
listed: List of string entries to modify
607+
condition: String to splice into string, usually 'and'
608+
609+
Returns:
610+
Modified string
611+
"""
612+
match [f"'{entry!s}'" for entry in listed]:
613+
case []:
614+
return ""
615+
case [one]:
616+
return one
617+
case [one, two]:
618+
return f"{one} {condition} {two}"
619+
case [*front, back]:
620+
return f"{', '.join(s for s in front)}, {condition} {back}"
621+
return ""

0 commit comments

Comments
 (0)