Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[strutils] implements natural_enumerate #359

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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.

None yet

1 participant