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 4155a68
Show file tree
Hide file tree
Showing 2 changed files with 23 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(

Check warning on line 281 in src/molecule/config.py

View check run for this annotation

Codecov / codecov/patch

src/molecule/config.py#L281

Added line #L281 was not covered by tests
"The detected galaxy.yml file (%s) is incomplete, missing %s",
galaxy_file,
missing_keys,
util.oxford_comma(missing_keys),
)
return None

Check warning on line 286 in src/molecule/config.py

View check run for this annotation

Codecov / codecov/patch

src/molecule/config.py#L286

Added line #L286 was not covered by tests

Expand Down
22 changes: 22 additions & 0 deletions src/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,25 @@ 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.
Args:
listed: List of string entries to modify
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}"
case [*front, back]:
return f"{', '.join(s for s in front)}, {condition} {back}"
return ""

Check warning on line 621 in src/molecule/util.py

View check run for this annotation

Codecov / codecov/patch

src/molecule/util.py#L612-L621

Added lines #L612 - L621 were not covered by tests

0 comments on commit 4155a68

Please sign in to comment.