Skip to content

Conversation

@mgaitan
Copy link
Contributor

@mgaitan mgaitan commented Dec 5, 2023

Return a string with a natural enumeration of the given items.

It's based on my contribution to IPython
https://github.com/ipython/ipython/blob/3ced24d6b07c3d66bbae5bef24fa59af25606512/IPython/utils/text.py#L729

which is an extended version of Django's get_text_list()`
https://github.com/django/django/blob/0ee2b8c326d47387bacb713a3ab369fa9a7a22ee/django/utils/text.py#L270

    >>> natural_enumerate(['a', 'b', 'c', 'd'])
    'a, b, c and d'
    >>> natural_enumerate(['a', 'b', 'c'], ' or ')
    'a, b or c'
    >>> natural_enumerate(['a', 'b', 'c'], ', ')
    'a, b, c'
    >>> natural_enumerate(['a', 'b'], ' or ')
    'a or b'
    >>> natural_enumerate(['a'])
    'a'
    >>> natural_enumerate([])
    ''
    >>> natural_enumerate(['a', 'b'], wrap_item_with="`")
    '`a` and `b`'
    >>> natural_enumerate(['a', 'b', 'c', 'd'], " = ", sep=" + ")
    'a + b + c = d'

I'm open to find a better name

@mgaitan
Copy link
Contributor Author

mgaitan commented Dec 5, 2023

I refuse to make this code 2.7 compatible. #339 should be implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant