[Tutor] ignoring diacritical signs

Albert-Jan Roskam fomcl at yahoo.com
Mon Dec 2 15:11:04 CET 2013


Hi,

I created the code below because I want to compare two fields while ignoring the diacritical signs. I thought it'd be cool to overload __eq__ for this. Is this a good approach, or have I been fixated too much on using the __eq__ special method? 


# -*- coding: utf-8 -*-

class Equalize(object):
    """Compare strings while ignoring diacritical signs and optionally casing"""

    def __init__(self, frms=u"é", tos=u"e", ignorecase=False):
        self.mapping = {ord(frm): ord(to) for frm, to in zip(frms, tos)}
        self.ignorecase = ignorecase

    def __call__(self, value):
        if self.ignorecase:
            value = value.lower()
        #if max(map(ord, list(value))) <= 128:
        #    return value
        return value.translate(self.mapping)

    def __eq__(self, other):
        if self == other:
            return True
        return False

if __name__ == "__main__":
    eq = Equalize(ignorecase=True)
    value_a, value_b = u"énorm", u"enorm"
    if value_a != value_b:
        print eq(value_a) == eq(value_b)


# alternative
frms, tos = u"é", u"e"
mapping = {ord(frm): ord(to) for frm, to in zip(frms, tos)}
value_a, value_b = u"énorm", u"enorm"
if value_a != value_b:
    value_a.translate(mapping) == value_b.translate(mapping)

Regards,

Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a 

fresh water system, and public health, what have the Romans ever done for us?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Tutor mailing list