Skip to content

Commit

Permalink
Resurrected support for byte strings as list values
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
  • Loading branch information
andy-maier committed Feb 26, 2023
1 parent 22b4ba2 commit 14eff32
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 50 deletions.
8 changes: 5 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ methods added in Python 3.3 to the built-in ``list`` class.
.. _list class of Python 3.8: https://docs.python.org/3.8/library/stdtypes.html#list
.. _NocaseList: https://nocaselist.readthedocs.io/en/stable/reference.html#nocaselist.NocaseList

The case-insensitivity is achieved by matching any list values as their
The case-insensitivity is achieved by matching any key values as their
casefolded values. By default, the casefolding is performed with
`str.casefold()`_ on Python 3 and with `str.lower()`_ on Python 2.
`str.casefold()`_ for unicode string keys and with `bytes.lower()`_ for byte
string keys.
The default casefolding can be overridden with a user-defined casefold method.

.. _str.casefold(): https://docs.python.org/3/library/stdtypes.html#str.casefold
.. _str.lower(): https://docs.python.org/2/library/stdtypes.html#str.lower
.. _bytes.lower(): https://docs.python.org/3/library/stdtypes.html#bytes.lower


Installation
------------
Expand Down
4 changes: 4 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Released: not yet

**Enhancements:**

* Resurrected support for byte strings as list values in the default
implementation of the casefold method. The list can now contains unicode
strings and byte strings.

**Cleanup:**

**Known issues:**
Expand Down
7 changes: 6 additions & 1 deletion docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ for being case-insensitive, of course). This includes the ``clear()`` and

The case-insensitivity is achieved by matching any key values as their
casefolded values. By default, the casefolding is performed with
:meth:`py:str.casefold` on Python 3 and with :meth:`py2:str.lower` on Python 2.
:meth:`py:str.casefold` for unicode string keys and with :meth:`py:bytes.lower`
for byte string keys.

The :meth:`py:str.casefold` method implements the casefolding
algorithm described in :term:`Default Case Folding in The Unicode Standard`.

The default casefolding can be overridden with a user-defined casefold method.


Expand Down
18 changes: 9 additions & 9 deletions nocaselist/_nocaselist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import sys
import os
import six

__all__ = ['NocaseList']

Expand Down Expand Up @@ -114,27 +113,28 @@ def __casefold__(value):
It returns a case-insensitive form of the input value by calling a
"casefold method" on the value. The input value will not be `None`.
The casefold method called by this method is :meth:`py:str.casefold`
on Python 3 and :meth:`py2:str.lower` on Python 2.
The casefold method called by this method is :meth:`py2:str.lower` on
Python 2, and on Python 3 it is :meth:`py:str.casefold`, falling back
to :meth:`py:bytes.lower` if it does not exist.
This method can be overridden by users in order to change the
case-insensitive behavior of the class.
See :ref:`Overriding the default casefold method` for details.
Parameters:
value (str or unicode): Input value, as a unicode string or in
Python 2 also as a byte string. Will not be `None`.
value (str or unicode or bytes): Input value. Will not be `None`.
Returns:
str or unicode: Case-insensitive form of the input value, as a
unicode string or in Python 2 also as a byte string.
str or unicode or bytes: Case-insensitive form of the input value.
Raises:
AttributeError: The value does not have the casefold method.
"""
if six.PY2:
try:
return value.casefold()
except AttributeError:
# Either Python 2, or Python 3 and a byte string key
return value.lower()
return value.casefold()

def __getstate__(self):
"""
Expand Down
Loading

0 comments on commit 14eff32

Please sign in to comment.