case insensitive dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Henry

    #1

    case insensitive dictionary

    I believe the standard dictionary should be amened to allow the use of
    case insensitive keys - as an option. I found some work done by others
    to do that at:



    but the problem with that approach is that they lowercase the keys
    immediately when you create the dictionary and so the true identity of
    the key is lost.

    Of course, I can subclass it and save a copy of the "real" key but
    that's kind of messcy.

    In other words:

    If I have:

    pets=caselessDi ct()

    pets["Cat"] = 3
    pets["Dog"] = 2

    I would like to see:

    pets["cat"] prints 3
    pets["DOG"] prints 2

    but

    print pets.keys()

    should print:

    "Cat", "Dog"

    not:

    "cat", "dog"

  • Rob Williscroft

    #2
    Re: case insensitive dictionary

    John Henry wrote in news:1164494606 .514366.124810
    @l39g2000cwd.go oglegroups.com in comp.lang.pytho n:
    I believe the standard dictionary should be amened to allow the use of
    case insensitive keys - as an option.
    class idict( dict ):

    class __istr( str ):

    def __eq__( self, other ):
    return self.lower() == other.lower()

    def __hash__( self ):
    return self.lower().__ hash__()

    def __setitem__( self, k, v ):
    dict.__setitem_ _( self, idict.__istr( k ), v )

    d = idict( a = 1, b = 2 )
    d['A'] = 3

    print d

    Rob.
    --

    Comment

    • vbgunz

      #3
      Re: case insensitive dictionary


      John Henry wrote:
      I believe the standard dictionary should be amened to allow the use of
      case insensitive keys - as an option. I found some work done by others
      to do that at:
      >

      >
      but the problem with that approach is that they lowercase the keys
      immediately when you create the dictionary and so the true identity of
      the key is lost.
      >
      Of course, I can subclass it and save a copy of the "real" key but
      that's kind of messcy.
      >
      In other words:
      >
      If I have:
      >
      pets=caselessDi ct()
      >
      pets["Cat"] = 3
      pets["Dog"] = 2
      >
      I would like to see:
      >
      pets["cat"] prints 3
      pets["DOG"] prints 2
      >
      but
      >
      print pets.keys()
      >
      should print:
      >
      "Cat", "Dog"
      >
      not:
      >
      "cat", "dog"
      You can try to title-case the list returned with keys() like so:
      print [x.title() for x in pets.keys()]

      Not a perfect solution but you get what you want... just an idea :)

      Comment

      • tomasz.kulawik.groups.1@gmail.com

        #4
        Re: case insensitive dictionary

        John Henry napisal(a):
        I believe the standard dictionary should be amened to allow the use of
        case insensitive keys - as an option. I found some work done by others
        to do that at:
        >

        >
        but the problem with that approach is that they lowercase the keys
        immediately when you create the dictionary and so the true identity of
        the key is lost.
        >
        Of course, I can subclass it and save a copy of the "real" key but
        that's kind of messcy.
        >
        In other words:
        >
        If I have:
        >
        pets=caselessDi ct()
        >
        pets["Cat"] = 3
        pets["Dog"] = 2
        >
        I would like to see:
        >
        pets["cat"] prints 3
        pets["DOG"] prints 2
        >
        but
        >
        print pets.keys()
        >
        should print:
        >
        "Cat", "Dog"
        >
        not:
        >
        "cat", "dog"
        How about:
        >>class Dictstr(str):
        .... def __hash__(self):
        .... return str.__hash__(se lf.lower())
        .... def __eq__(self,oth er):
        .... return self.lower()==o ther.lower()
        ....
        >>class dictt(dict):
        .... def __setitem__(sel f, key, value):
        .... if isinstance(key, str):
        .... dict.__setitem_ _(self, Dictstr(key), value)
        .... else:
        .... dict.__setitem_ _(self, key, value)
        .... def __getitem__(sel f, key):
        .... if isinstance(key, str):
        .... return dict.__getitem_ _(self, Dictstr(key))
        .... else:
        .... return dict.__getitem_ _(self, key)
        ....
        >>d=dictt()
        >>d[1]=1
        >>d['Cat']='Cat'
        >>d['Dog']='Dog'
        >>d['dOg']
        'Dog'
        >>d.keys()
        [1, 'Dog', 'Cat']

        Note that you would need to redefine also __init__, __contains__ and
        other methods.

        Comment

        • John Henry

          #5
          Re: case insensitive dictionary

          I don't think that's sufficient. See how many methods the author of
          http://aspn.activestate.com/ASPN/Coo.../Recipe/283455 had to
          redefine.

          Rob Williscroft wrote:
          John Henry wrote in news:1164494606 .514366.124810
          @l39g2000cwd.go oglegroups.com in comp.lang.pytho n:
          >
          I believe the standard dictionary should be amened to allow the use of
          case insensitive keys - as an option.
          >
          class idict( dict ):
          >
          class __istr( str ):
          >
          def __eq__( self, other ):
          return self.lower() == other.lower()
          >
          def __hash__( self ):
          return self.lower().__ hash__()
          >
          def __setitem__( self, k, v ):
          dict.__setitem_ _( self, idict.__istr( k ), v )
          >
          d = idict( a = 1, b = 2 )
          d['A'] = 3
          >
          print d
          >
          Rob.
          --
          http://www.victim-prime.dsl.pipex.com/

          Comment

          • J. Clifford Dyer

            #6
            Re: case insensitive dictionary

            John Henry wrote:
            print pets.keys()
            >
            should print:
            >
            "Cat", "Dog"
            If you do:

            Pypets['Cat'] = 2
            Pypets['Dog'] = 3
            Pypets['DOG'] = 4
            Pypets['cat'] += 5
            Pypets.keys()

            What should the result be?

            "['Cat', 'Dog']" or "['cat', 'DOG']"?


            That is to say, if you use a new case in redefining the values of your
            case insensitive dictionary, does the key take on the new case, or will
            it always and forever be the case originally given to it?

            Cheers,
            Cliff

            Comment

            • Fuzzyman

              #7
              Re: case insensitive dictionary


              John Henry wrote:
              I believe the standard dictionary should be amened to allow the use of
              case insensitive keys - as an option. I found some work done by others
              to do that at:
              >

              >
              but the problem with that approach is that they lowercase the keys
              immediately when you create the dictionary and so the true identity of
              the key is lost.
              A later version of this class does give you access to the original case
              :

              http://www.voidspace.org.uk/python/a...shtml#caseless

              Fuzzyman
              http://www.voidspace.org.uk/python/index.shtml

              Comment

              • John Henry

                #8
                Re: case insensitive dictionary

                I believe that if you redefine the value, the key should not change.
                So, yes, I would expect that they value of the key to remain as they
                were.


                J. Clifford Dyer wrote:
                John Henry wrote:
                print pets.keys()

                should print:

                "Cat", "Dog"
                >
                If you do:
                >
                Pypets['Cat'] = 2
                Pypets['Dog'] = 3
                Pypets['DOG'] = 4
                Pypets['cat'] += 5
                Pypets.keys()
                >
                What should the result be?
                >
                "['Cat', 'Dog']" or "['cat', 'DOG']"?
                >
                >
                That is to say, if you use a new case in redefining the values of your
                case insensitive dictionary, does the key take on the new case, or will
                it always and forever be the case originally given to it?
                >
                Cheers,
                Cliff

                Comment

                Working...