Back to index

plone3  3.1.7
caselessdict.py
Go to the documentation of this file.
00001 # -*- coding: latin-1 -*-
00002 
00003 class CaselessDict(dict):
00004     """
00005     A dictionary that isn't case sensitive, and only use string as keys.
00006 
00007     >>> ncd = CaselessDict(key1='val1', key2='val2')
00008     >>> ncd
00009     CaselessDict({'KEY2': 'val2', 'KEY1': 'val1'})
00010     >>> ncd['key1']
00011     'val1'
00012     >>> ncd['KEY1']
00013     'val1'
00014     >>> ncd['KEY3'] = 'val3'
00015     >>> ncd['key3']
00016     'val3'
00017     >>> ncd.setdefault('key3', 'FOUND')
00018     'val3'
00019     >>> ncd.setdefault('key4', 'NOT FOUND')
00020     'NOT FOUND'
00021     >>> ncd['key4']
00022     'NOT FOUND'
00023     >>> ncd.get('key1')
00024     'val1'
00025     >>> ncd.get('key3', 'NOT FOUND')
00026     'val3'
00027     >>> ncd.get('key4', 'NOT FOUND')
00028     'NOT FOUND'
00029     >>> 'key4' in ncd
00030     True
00031     >>> del ncd['key4']
00032     >>> ncd.has_key('key4')
00033     False
00034     >>> ncd.update({'key5':'val5', 'KEY6':'val6', 'KEY5':'val7'})
00035     >>> ncd['key6']
00036     'val6'
00037     >>> keys = ncd.keys()
00038     >>> keys.sort()
00039     >>> keys
00040     ['KEY1', 'KEY2', 'KEY3', 'KEY5', 'KEY6']
00041     """
00042 
00043     def __init__(self, *args, **kwargs):
00044         "Set keys to upper for initial dict"
00045         dict.__init__(self, *args, **kwargs)
00046         for k,v in self.items():
00047             k_upper = k.upper()
00048             if k != k_upper:
00049                 dict.__delitem__(self, k)
00050                 self[k_upper] = v
00051 
00052     def __getitem__(self, key):
00053         return dict.__getitem__(self, key.upper())
00054 
00055     def __setitem__(self, key, value):
00056         dict.__setitem__(self, key.upper(), value)
00057 
00058     def __delitem__(self, key):
00059         dict.__delitem__(self, key.upper())
00060 
00061     def __contains__(self, item):
00062         return dict.__contains__(self, item.upper())
00063 
00064     def get(self, key, default=None):
00065         return dict.get(self, key.upper(), default)
00066 
00067     def setdefault(self, key, value=None):
00068         return dict.setdefault(self, key.upper(), value)
00069 
00070     def pop(self, key, default=None):
00071         return dict.pop(self, key.upper(), default)
00072 
00073     def popitem(self):
00074         return dict.popitem(self, key.upper())
00075 
00076     def has_key(self, key):
00077         return dict.has_key(self, key.upper())
00078 
00079     def update(self, indict):
00080         """
00081         Multiple keys where key1.upper() == key2.upper() will be lost.
00082         """
00083         for entry in indict:
00084             self[entry] = indict[entry]
00085 
00086     def copy(self):
00087         return CaselessDict(dict.copy(self))
00088 
00089     def clear(self):
00090         dict.clear(self)
00091 
00092     def __repr__(self):
00093         return 'CaselessDict(' + dict.__repr__(self) + ')'