Skip to content

Commit

Permalink
Add conjunction to comma_join & fix islist (#4184)
Browse files Browse the repository at this point in the history
* Add ability to change the conjunction used by comma_join.
* Fix islist bug which caused it to incorrectly treat classes with __iter__ as lists (e.g. islist(dict)).
  • Loading branch information
kenodegard authored Jul 24, 2023
1 parent 25c695e commit ef382e4
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions conda_build/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
)
from pathlib import Path
from threading import Thread
from typing import Iterable

import libarchive

Expand Down Expand Up @@ -941,7 +942,7 @@ def file_info(path):
}


def comma_join(items):
def comma_join(items: Iterable[str], conjunction: str = "and") -> str:
"""
Like ', '.join(items) but with and
Expand All @@ -954,11 +955,10 @@ def comma_join(items):
>>> comma_join(['a', 'b', 'c'])
'a, b, and c'
"""
return (
" and ".join(items)
if len(items) <= 2
else ", ".join(items[:-1]) + ", and " + items[-1]
)
items = tuple(items)
if len(items) <= 2:
return f"{items[0]} {conjunction} {items[1]}"
return f"{', '.join(items[:-1])}, {conjunction} {items[-1]}"


def safe_print_unicode(*args, **kwargs):
Expand Down Expand Up @@ -1268,7 +1268,7 @@ def islist(arg, uniform=False, include_dict=True):
:return: Whether `arg` is a `list`
:rtype: bool
"""
if isinstance(arg, str) or not hasattr(arg, "__iter__"):
if isinstance(arg, str) or not isinstance(arg, Iterable):
# str and non-iterables are not lists
return False
elif not include_dict and isinstance(arg, dict):
Expand All @@ -1279,6 +1279,7 @@ def islist(arg, uniform=False, include_dict=True):
return True

# NOTE: not checking for Falsy arg since arg may be a generator
# WARNING: if uniform != False and arg is a generator then arg will be consumed

if uniform is True:
arg = iter(arg)
Expand Down

0 comments on commit ef382e4

Please sign in to comment.