caseless dict - questions

Discussion in 'Python' started by Phoe6, Jul 5, 2008.

  1. Phoe6

    Phoe6 Guest

    I have a requirement for using caseless dict. I searched the web for
    many different implementations and found one snippet which was
    implemented in minimal and useful way.

    #############
    import UserDict

    class CaseInsensitiveDict(dict, UserDict.DictMixin):
    def __init__(self, *args, **kwargs):
    self.orig = {}
    super(CaseInsensitiveDict, self).__init__(*args, **kwargs)
    def items(self):
    keys = dict.keys(self)
    values = dict.values(self)
    return [(self.orig[k],v) for k in keys for v in values]
    def __setitem__(self, k, v):
    hash_val = hash(k.lower())
    self.orig[hash_val] = k
    dict.__setitem__(self, hash_val, v)
    def __getitem__(self, k):
    return dict.__getitem__(self, hash(k.lower()))


    obj = CaseInsensitiveDict()
    obj['Name'] = 'senthil'
    print obj
    print obj.items()

    obj1 = {}
    obj1['Name'] = 'senthil'
    print obj1
    print obj1.items()
    ###########
    [ors@goofy python]$ python cid1.py
    {15034981: 'senthil'}
    [('Name', 'senthil')]
    {'Name': 'senthil'}
    [('Name', 'senthil')]

    ---
    The difference between the Caselessdict and {} is that when called as
    the object, the Caselessdict() is giving me the internal
    representation.
    obj = CaseInsensitiveDict()
    obj['Name'] = 'senthil'
    print obj
    gives: {15034981: 'senthil'}

    obj1 = {}
    obj1['Name'] = 'senthil'
    print obj1
    Correctly gives {'Name': 'senthil'}

    What changes should I make to CaseInsensitiveDict ( written above), so
    that its instance gives the actual dictionary instead of its internal
    representation.
    Constructing a dictionary and returning from __init__ method did not
    work.

    TIA,
    Senthil
    Phoe6, Jul 5, 2008
    #1
    1. Advertising

  2. Phoe6

    Jeff Guest

    Use the __str__ and __unicode__ methods to control the printed
    representation of a class.
    Jeff, Jul 8, 2008
    #2
    1. Advertising

  3. Phoe6

    oj Guest

    On Jul 5, 1:57 am, Phoe6 <> wrote:
    > I have a requirement for using caseless dict. I searched the web for
    > many different implementations and found one snippet which was
    > implemented in minimal and useful way.
    >
    > #############
    > import UserDict
    >
    > class CaseInsensitiveDict(dict, UserDict.DictMixin):
    >     def __init__(self, *args, **kwargs):
    >         self.orig = {}
    >         super(CaseInsensitiveDict, self).__init__(*args, **kwargs)
    >     def items(self):
    >         keys = dict.keys(self)
    >         values = dict.values(self)
    >         return [(self.orig[k],v) for k in keys for v in values]
    >     def __setitem__(self, k, v):
    >         hash_val = hash(k.lower())
    >         self.orig[hash_val] = k
    >         dict.__setitem__(self, hash_val, v)
    >     def __getitem__(self, k):
    >         return dict.__getitem__(self, hash(k.lower()))
    >
    > obj = CaseInsensitiveDict()
    > obj['Name'] = 'senthil'
    > print obj
    > print obj.items()
    >
    > obj1 = {}
    > obj1['Name'] = 'senthil'
    > print obj1
    > print obj1.items()
    > ###########
    > [ors@goofy python]$ python cid1.py
    > {15034981: 'senthil'}
    > [('Name', 'senthil')]
    > {'Name': 'senthil'}
    > [('Name', 'senthil')]
    >
    > ---
    > The difference between the Caselessdict and {} is that when called as
    > the object, the Caselessdict() is giving me the internal
    > representation.
    > obj = CaseInsensitiveDict()
    > obj['Name'] = 'senthil'
    > print obj
    > gives: {15034981: 'senthil'}
    >
    > obj1 = {}
    > obj1['Name'] = 'senthil'
    > print obj1
    > Correctly gives {'Name': 'senthil'}
    >
    > What changes should I make to CaseInsensitiveDict ( written above), so
    > that its instance gives the actual dictionary instead of its internal
    > representation.
    > Constructing a dictionary and returning from __init__ method did not
    > work.
    >
    > TIA,
    > Senthil


    What I think you need to do, is define a __repr__(self) method (see
    http://docs.python.org/ref/customization.html)

    Something like:

    def __repr__(self):
    return dict(self.items())

    I /think/ will work. I haven't tested it though. This isn't exactly
    what repr is supposed to do - evaling it won't give you the correct
    object back. Defining __str__ might be a better approach.

    -Oli
    oj, Jul 8, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    412
    Skip Montanaro
    Aug 15, 2003
  2. Fuzzyman
    Replies:
    0
    Views:
    343
    Fuzzyman
    Mar 10, 2005
  3. Alexander Kozlovsky

    dict!ident as equivalent of dict["ident"]

    Alexander Kozlovsky, May 21, 2006, in forum: Python
    Replies:
    5
    Views:
    357
    Alexander Kozlovsky
    May 22, 2006
  4. Drew
    Replies:
    19
    Views:
    1,345
    Duncan Booth
    Mar 15, 2007
  5. Stef Mientki

    caseless dictionary howto ?

    Stef Mientki, Jun 19, 2007, in forum: Python
    Replies:
    12
    Views:
    517
    Gabriel Genellina
    Jun 20, 2007
Loading...

Share This Page