Home > Net >  Python validating an attempt to append to a list attribute
Python validating an attempt to append to a list attribute

Time:09-24

Just learning about properties and setters in python, and seems fair enough when we have a mutable attribute. But what happens when I want to validate a .append() on a list for example? In the below, I can validate the setting of the attribute and it works as expected. But I can bypass its effect by simply appending to get more cookies onto the tray...

class CookieTray:
    def __init__(self):
        self.cookies = []
    
    @property
    def cookies(self):
        return self._cookies
    
    @cookies.setter
    def cookies(self, cookies):
        if len(cookies) > 8:
            raise ValueError("Too many cookies in the tray!")

        self._cookies = cookies

if __name__ == '__main__':
    tray = CookieTray()
    print("Cookies: ", tray.cookies)
    try:
        tray.cookies = [1,1,0,0,0,1,1,0,1] # too many
    except Exception as e:
        print(e)

    tray.cookies = [1,0,1,0,1,0]
    print(tray.cookies)
    tray.cookies.append(0)
    tray.cookies.append(0)
    tray.cookies.append(1) # too many, but can still append
    print(tray.cookies)

Silly example, but I hope it illustrates my question. Should I just be avoiding the setter and making a "setter" method, like add_cookie(self, cookie_type) and then do my validation in there?

CodePudding user response:

You would need to create a ValidatingList class that overrides list.append, something like this:

class ValidatingList(list):
    def append(self, value):
        if len(self) > 8:
             raise ValueError("Too many items in the list")
        else:
            super().append(value)

You then convert your cookies to a ValidatingList:

class CookieTray:
    def __init__(self):
        self.cookies = []

    @property
    def cookies(self):
        return self._cookies

    @cookies.setter
    def cookies(self, cookies):
        if len(cookies) > 8:
            raise ValueError("Too many cookies in the tray!")

        self._cookies = ValidatingList(cookies)

CodePudding user response:

The setter only applies when assigning to the attribute. As you've seen mutating the attribute bypasses this.

To apply the validation to the object being mutated we can use a custom type. Here's an example which just wraps a normal list:

import collections

class SizedList(collections.abc.MutableSequence):

    def __init__(self, maxlen):
        self.maxlen = maxlen
        self._list = []

    def check_length(self):
        if len(self._list) >= self.maxlen:
            raise OverflowError("Max length exceeded")

    def __setitem__(self, i, v):
        self.check_length()
        self._list[i] = v

    def insert(self, i, v):
        self.check_length()
        self._list.insert(i, v)

    def __getitem__(self, i): return self._list[i]
    def __delitem__(self, i): del self._list[i]
    def __len__(self): return len(self._list)
    def __repr__(self): return f"{self._list!r}"

When overriding container types collections.abc can be useful - we can see the abstract methods that must be implemented: __getitem__, __setitem__, __delitem__, __len__, and insert in this case. All of them are delegated to the list object that's being wrapped, with the two that add items having the added length check.

The repr isn't needed, but makes it easier to check the contents - once again just delegating to the wrapped list.

With this you can simply replace the self.cookies = [] line with self.cookies = SizedList(8).

  • Related