Skip to content Skip to sidebar Skip to footer

Subclass Python List To Validate New Items

I want a python list which represents itself externally as an average of its internal list items, but otherwise behaves as a list. It should raise a TypeError if an item is added t

Solution 1:

Inherit from MutableSequence and implement the methods it requires as well as any others that fall outside of the scope of Sequences alone -- like the operators here. This will allow you to change the operator manipulations for list-like capabilities while automatically generating iterators and contains capabilities.

If you want to check for slices btw you need to do isinstance(key, slice) in your __getitem__ (and/or __setitem__) methods. Note that a single index like myList[0] is not a slice request, but a single index and myList[:0] is an actual slice request.

Solution 2:

The array.array class will take care of the float part:

classAverageList(array.array):
    def__new__(cls, *args, **kw):
        return array.array.__new__(cls, 'd')
    def__init__(self, values=()):
        self.extend(values)
    def__repr__(self):
        ifnotlen(self): return'Empty'returnrepr(math.fsum(self)/len(self))

And some tests:

>>>s = AverageList([1,2])>>>s
1.5
>>>s.append(9)>>>s
4.0
>>>s.extend('lol')
Traceback (most recent call last):
  File "<pyshell#117>", line 1, in <module>
    s.extend('lol')
TypeError: a float is required

Solution 3:

Actually the best answer may be: don't.

Checking all objects as they get added to the list will be computationally expensive. What do you gain by doing those checks? It seems to me that you gain very little, and I'd recommend against implementing it.

Python doesn't check types, and so trying to have a little bit of type checking for one object really doesn't make a lot of sense.

Solution 4:

There are 7 methods of the list class that add elements to the list and would have to be checked. Here's one compact implementation:

defcheck_float(x):
    try:
        f = float(x)
    except:
        raise TypeError("Cannot add %s to AverageList" % str(x))

defmodify_method(f, which_arg=0, takes_list=False):
    defnew_f(*args):
        if takes_list:
            map(check_float, args[which_arg + 1])
        else:
            check_float(args[which_arg + 1])
        return f(*args)
    return new_f

classAverageList(list):
    def__check_float(self, x):
        try:
            f = float(x)
        except:
            raise TypeError("Cannot add %s to AverageList" % str(x))

    append = modify_method(list.append)
    extend = modify_method(list.extend, takes_list=True)
    insert = modify_method(list.insert, 1)
    __add__ = modify_method(list.__add__, takes_list=True)
    __iadd__ = modify_method(list.__iadd__, takes_list=True)
    __setitem__ = modify_method(list.__setitem__, 1)
    __setslice__ = modify_method(list.__setslice__, 2, takes_list=True)

Solution 5:

The general approach would be to create your own class inheriting vom list and overwriting the specific methods like append, extend etc. This will probably also include magic methods of the Python list (see this article for details: http://www.rafekettler.com/magicmethods.html#sequence).

For validation, you will need to overwrite __setitem__(self, key, value)

Post a Comment for "Subclass Python List To Validate New Items"