Skip to content

Commit

Permalink
Added conjunction to comma_join, fixed islist
Browse files Browse the repository at this point in the history
Added ability to change the conjunction used by comma_join. Fixed islist bug which caused it to incorrectly treat classes with __iter__ as lists (e.g. islist(dict)).
  • Loading branch information
kenodegard committed Jan 16, 2021
1 parent d2048ea commit cfca25c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions conda_build/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def getter(index):
return operator.itemgetter(index)


def comma_join(items):
def comma_join(items, conjunction="and"):
"""
Like ', '.join(items) but with and
Expand All @@ -925,7 +925,9 @@ 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]
if len(items) <= 2:
return ' {} '.format(conjunction).join(items)
return ', '.join(items[:-1]) + ', {} '.format(conjunction) + items[-1]


def safe_print_unicode(*args, **kwargs):
Expand Down Expand Up @@ -1224,13 +1226,21 @@ def islist(arg, uniform=False, include_dict=True):
:return: Whether `arg` is a `list`
:rtype: bool
"""
if isinstance(arg, string_types) or not hasattr(arg, '__iter__'):
# str and non-iterables are not lists
if isinstance(arg, string_types):
# strs are not lists
return False
elif not include_dict and isinstance(arg, dict):

try:
iter(arg)
except TypeError:
# non-iterables are not lists
return False

if not include_dict and isinstance(arg, dict):
# do not treat dict as a list
return False
elif not uniform:

if not uniform:
# short circuit for non-uniformity
return True

Expand Down

0 comments on commit cfca25c

Please sign in to comment.