Skip to content Skip to sidebar Skip to footer

A Case Insensitive String Class In Python

I need to perform case insensitive string comparisons in python in sets and dictionary keys. Now, to create sets and dict subclasses that are case insensitive proves surprisingly t

Solution 1:

In your demo you are using 'a' to look stuff up in your set. It wouldn't work if you tried to use 'A', because 'A' has a different hash. Also 'A' in d.keys() would be true, but 'A' in d would be false. You've essentially created a type that violates the normal contract of all hashes, by claiming to be equal to objects that have different hashes.

You could combine this answer with the answers about creating specialised dicts, and have a dict that converted any possible key into CIstr before trying to look it up. Then all your CIstr conversions could be hidden away inside the dictionary class.

E.g.

class CaseInsensitiveDict(dict):
    def __setitem__(self, key, value):
        super(CaseInsensitiveDict, self).__setitem__(convert_to_cistr(key), value)
    def __getitem__(self, key):
        return super(CaseInsensitiveDict, self).__getitem__(convert_to_cistr(key))
    # __init__, __contains__ etc.

(Based on https://stackoverflow.com/a/2082169/3890632)


Solution 2:

The code mostly looks fine. I would eliminate the short-cut's in __ge__, __le__, and __ne__ and expand them to call lower() directly.

The short-cut looks like what is done in `functools.total_ordering() but it just slows down the code and makes it harder to test cross-type comparisons which are tricky to get right when the methods are interdependent.


Post a Comment for "A Case Insensitive String Class In Python"