Q: List of given type

Zaur Shibzoukhov szport at fromru.com
Sat Mar 29 11:23:42 EST 2003


I am thinking on solution of the following problem:
"Create and support a list so that all objects in list has a given type".

This is important (for me) to prevent any checkings of list item's type in my extension module that only uses list items.

I write a module "listof.py".
But I think that there exists a better and more fundamental solution of this problem.

Anybody know?

Zaur

------------------------------------------------------------------------------------

#
# listof.py
#


try:
    # use implementation with C API if available
    from _listof import _newlistof
except:
    def _newlistof(sourcelist, itemtype, *args, **kw):
        """_newlistof(sourcelist, itemtype, *args, **kw)
    
        sourcelist - source list
        itemtype - a given type of items
        args and kw - for convertion to the given type
    
        This function create a list copy of sourcelist so that all elements of them has the given __itemtype__.
        """
        newlist = []
        append = newlist.append
        for o in sourcelist:
            if type(o) == itemtype:
                append(o)
            else:
                append(itemtype(o, *args, **kw))
        return newlist


class listof(list):
    """
    Provides a list of objects of a given type with automatic type convertion of new added or assigned items
    """
    #
    __slots__ = ['__itemtype__', '__args__', '__kw__']
    #
    def __init__(self, sourcelist, itemtype, *args, **kw):
        if type(itemtype) != type:
            raise TypeError
        self.__itemtype__ = itemtype
        self.__args__ = args
        self.__kw__ = kw
        list.extend(self, _newlistof(sourcelist, itemtype, *args, **kw))
    #
    def append(self, o):
        if type(o) == self.__itemtype__:
            list.append(self, o)
        else:
            list.append(self, self.__itemtype__(o, *self.__args__, **self.__kw__))
    #
    def extend(self, sourcelist):
        list.extend(self, _newlistof(sourcelist, self.__itemtype__, *self.__args__, **self.__kw__))
    #
    def __setitem__(self, i, o):
        if type(o) == self.__itemtype__:
            list.__setitem__(self, i, o)
        else:
            list.__setitem__(self, i, self.__itemtype__(o, *self.__args__, **self.__kw__))
    #
    def __setslice__(self, i, j, otherlist):
        list.__setslice__(self, i, j, _newlistof(otherlist, self.__itemtype__, *self.__args__, **self.__kw__))
    #
    def insert(self, i, o):
        if type(o) == self.__itemtype__:
            list.insert(self, i, o)
        else:
            list.insert(self, i, self.__itemtype__(o, *self.__args__, **self.__kw__))
    #
    def __add__(self, other):
        return list.__add__(self, _newlistof(other, self.__itemtype__, *self.__args__, **self.__kw__))
    #
    def itemtype(self):
        """
        obj.itemtype()

        Return the type of list items.
        """
        return self.__itemtype__

#
# eof
#





More information about the Python-list mailing list