X Tutup
The Wayback Machine - https://web.archive.org/web/20251004111223/https://github.com/python/cpython/pull/118017
Skip to content

Conversation

ani0075saha
Copy link

@ani0075saha ani0075saha commented Apr 18, 2024


馃摎 Documentation preview 馃摎: https://cpython-previews--118017.org.readthedocs.build/

Update the comparison functions code snippet to be self-contained. Use the names list from a previous example instead of words.
Update comparison functions code snippet to be self-contained. Use names list from previous example instead of words.
@ghost
Copy link

ghost commented Apr 18, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news awaiting review labels Apr 18, 2024
@encukou
Copy link
Member

encukou commented Apr 18, 2024

IMO, it would be better to find a different example here:

  • an equivalent key function locale.strxfrm, already exists. There's no need to use cmp_to_key(strcoll).
  • the result is locale-dependent; it might be different (and the doctest might fail) if locale.setlocale() is called beforehand.

Comment on lines +273 to +280
..doctest::

>>> from functools import cmp_to_key
>>> from locale import strcoll

>>> names = 'Zo毛 脜bj酶rn N煤帽ez 脡lana Zeke Abe Nubia Eloise'.split()
>>> sorted(names, key=cmp_to_key(strcoll)) # locale-aware sort order
['Abe', 'Eloise', 'Nubia', 'N煤帽ez', 'Zeke', 'Zo毛', '脜bj酶rn', '脡lana']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
..doctest::
>>> from functools import cmp_to_key
>>> from locale import strcoll
>>> names = 'Zo毛 脜bj酶rn N煤帽ez 脡lana Zeke Abe Nubia Eloise'.split()
>>> sorted(names, key=cmp_to_key(strcoll)) # locale-aware sort order
['Abe', 'Eloise', 'Nubia', 'N煤帽ez', 'Zeke', 'Zo毛', '脜bj酶rn', '脡lana']
.. code-block::
>>> from functools import cmp_to_key
>>> from locale import strcoll
>>> names = 'Zo毛 脜bj酶rn N煤帽ez 脡lana Zeke Abe Nubia Eloise'.split()
>>> sorted(names, key=cmp_to_key(strcoll)) # locale-aware sort order
['Abe', 'Eloise', 'Nubia', 'N煤帽ez', 'Zeke', 'Zo毛', '脜bj酶rn', '脡lana']

To avoid the doctest issues.

Comment on lines +273 to +280
..doctest::

>>> from functools import cmp_to_key
>>> from locale import strcoll

>>> names = 'Zo毛 脜bj酶rn N煤帽ez 脡lana Zeke Abe Nubia Eloise'.split()
>>> sorted(names, key=cmp_to_key(strcoll)) # locale-aware sort order
['Abe', 'Eloise', 'Nubia', 'N煤帽ez', 'Zeke', 'Zo毛', '脜bj酶rn', '脡lana']
Copy link
Member

@StanFromIreland StanFromIreland Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
..doctest::
>>> from functools import cmp_to_key
>>> from locale import strcoll
>>> names = 'Zo毛 脜bj酶rn N煤帽ez 脡lana Zeke Abe Nubia Eloise'.split()
>>> sorted(names, key=cmp_to_key(strcoll)) # locale-aware sort order
['Abe', 'Eloise', 'Nubia', 'N煤帽ez', 'Zeke', 'Zo毛', '脜bj酶rn', '脡lana']
.. code-block::
>>> from functools import cmp_to_key
>>> def compare_abs(x, y):
... """Sort by absolute value, prioritizing negatives"""
... if abs(x) != abs(y):
... return (abs(x) > abs(y)) - (abs(x) < abs(y))
... return (x > y) - (x < y)
>>> numbers = [5, -1, 2, -5, 3, -2, 1]
>>> sorted(numbers, key=cmp_to_key(compare_abs))
[-1, 1, -2, 2, 3, -5, 5]

As for a different example, @encukou WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

3 participants
X Tutup