source: TI12-security/trunk/python/ndg.security.common/ndg/security/common/utils/__init__.py @ 5589

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.common/ndg/security/common/utils/__init__.py@5589
Revision 5589, 2.5 KB checked in by pjkersha, 11 years ago (diff)

ndg.security.test.unit.saml.test_saml: Working unit test for Attribute Query Response

Line 
1"""Utilities package for NDG Security
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "02/04/09"
7__copyright__ = ""
8__license__ = "BSD - see LICENSE file in top-level directory"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__revision__ = '$Id: $'
11
12class UniqList(list):
13    """Extended version of list type to enable a list with unique items.
14    If an item is added that is already present then it is silently omitted
15    from the list
16    """
17    def extend(self, iter):
18        return super(UniqList, self).extend([i for i in iter if i not in self])
19       
20    def __iadd__(self, iter):
21        return super(UniqList, self).__iadd__([i for i in iter 
22                                               if i not in self])
23         
24    def append(self, item):
25        for i in self:
26            if i == item:
27                return None
28           
29        return super(UniqList, self).append(item)
30
31
32class TypedList(list):
33    """Extend list type to enabled only items of a given type.  Supports
34    any type where the array type in the Standard Library is restricted to
35    only limited set of primitive types
36    """
37   
38    def __init__(self, elementType, *arg, **kw):
39        """
40        @type elementType: type/tuple
41        @param elementType: object type or types which the list is allowed to
42        contain.  If more than one type, pass as a tuple
43        """
44        self.__elementType = elementType
45        super(TypedList, self).__init__(*arg, **kw)
46   
47    def _getElementType(self):
48        return self.__elementType
49   
50    elementType = property(fget=_getElementType, 
51                           doc="The allowed type or types for list elements")
52     
53    def extend(self, iter):
54        for i in iter:
55            if not isinstance(i, self.__elementType):
56                raise TypeError("List items must be of type %s" % 
57                                (self.__elementType,))
58               
59        return super(TypedList, self).extend(iter)
60       
61    def __iadd__(self, iter):
62        for i in iter:
63            if not isinstance(i, self.__elementType):
64                raise TypeError("List items must be of type %s" % 
65                                (self.__elementType,))
66                   
67        return super(TypedList, self).__iadd__(iter)
68         
69    def append(self, item):
70        if not isinstance(item, self.__elementType):
71                raise TypeError("List items must be of type %s" % 
72                                (self.__elementType,))
73   
74        return super(TypedList, self).append(item)
Note: See TracBrowser for help on using the repository browser.