Skip to content

Commit

Permalink
Introduce oxford comma function
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Dec 13, 2024
1 parent 236e4b4 commit df3e74b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def collection(self) -> CollectionData | None:
LOG.warning(
"The detected galaxy.yml file (%s) is incomplete, missing %s",
galaxy_file,
missing_keys,
util.oxford_comma(missing_keys),
)
return None

Expand Down
19 changes: 19 additions & 0 deletions src/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,22 @@ def print_as_yaml(data: object) -> None:
# https://github.com/Textualize/rich/discussions/990#discussioncomment-342217
result = Syntax(code=safe_dump(data), lexer="yaml", background_color="default")
console.print(result)


def oxford_comma(listed: Iterable[bool | str | Path], condition: str = "and") -> str:
"""Format a list into a sentence.
:param listed: List of string entries to modify
:param condition: String to splice into string, usually 'and'
:returns: Modified string
"""
match [f"'{entry!s}'" for entry in listed]:
case []:
return ""
case [one]:
return one
case [one, two]:
return f"{one} {condition} {two}"

listed = list(listed)
return f"{', '.join(str(x) for x in listed[:-1])}, {condition} {listed[-1]}"

0 comments on commit df3e74b

Please sign in to comment.