Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: =?UTF-8?Q?Nagy_L=c3=a1szl=c3=b3_Zsolt?= Newsgroups: comp.lang.python Subject: Re: UserList - which methods needs to be overriden? Date: Thu, 9 Jun 2016 15:33:32 +0200 Lines: 55 Message-ID: References: <917ca9c4-aef1-6681-ec70-62de8916117d@shopzeus.com> <7cdb398f-b3f2-9a3c-b444-2229f8496e2f@shopzeus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de vZ5tDiWjrvBWeh66rAI7wgx9g+JDs9Aw5WOpWHGzeZXg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'finally:': 0.05; 'ignored': 0.05; '*args):': 0.09; 'collections': 0.09; 'dict': 0.09; 'invocation': 0.09; 'subclass': 0.09; 'subject:which': 0.09; 'def': 0.13; '(sorry,': 0.16; '__slots__': 0.16; 'clear(self):': 0.16; 'example).': 0.16; 'list):': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'remove(self,': 0.16; 'skip:@ 20': 0.16; 'userdict': 0.16; 'wrappers': 0.16; 'try:': 0.18; 'class,': 0.22; 'pass': 0.22; 'seems': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'yield': 0.27; 'actual': 0.28; 'looks': 0.29; 'types.': 0.29; 'becomes': 0.30; "can't": 0.32; 'class': 0.33; 'list': 0.34; 'basis.': 0.35; 'something': 0.35; 'sometimes': 0.35; 'list,': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'skip:s 50': 0.37; 'detail': 0.38; 'data': 0.39; 'whatever': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'skip:n 10': 0.62; 'accessed': 0.66; 'subject:needs': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shopzeus.com; s=shopzeus_com; t=1465479209; bh=6WqQz8AZBDbDCSeffqDiLM+AZN1Je8MHS10mnUZCuuA=; h=Subject:To:References:From:Date:In-Reply-To:From; b=B+bCghTH+4SmrRJj4USeYwWMVMK0sFGGoKUB3FtX1Z4DmGshKHwXI9pInFCjfkKOe p6OBdfeemkF+xoQ3IWCShlCeF+cL0qof1o5z3heShOAjuXBSQh69E99m4nHfF/duTj 9hQtzsDYDzgfNfMLz6Mu86b2epG2WjKR23dR+6lBzlFJuv7Nuq1+27TtHRFReewFAg n9Fng6R2+6Eoz/ELoqkr/0R+I9gBTTDPuwFOVoFY/NtYjW+TUenJK8wsqCMHmfot0o BKE0O5sTUr+loGT2uY3ObPTikbykwpvP2FlA60JpNA3CD+4cl/X0JAzgO3PnqTiH21 YopZw4jmc6z3A== In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <7cdb398f-b3f2-9a3c-b444-2229f8496e2f@shopzeus.com> X-Mailman-Original-References: <917ca9c4-aef1-6681-ec70-62de8916117d@shopzeus.com> Xref: csiph.com comp.lang.python:109746 > Using the built-in list or dict is problematic because sometimes the=20 > subclass methods are ignored when the internal data is accessed (sorry,= I=20 > don't have an example). Are you suggesting that I should use UserList and UserDict instead of list and dict? What is the truth? Was UserDict left in collections for backward compatibility, or not? > > How detailed is your change notification? If it's always the same metho= d=20 > invocation you may be able to create wrappers like > > def whatever(self, *args): > try: > return super().whatever(*args) > finally: > self.changed() > > automatically, and the base class, be it list or UserList or whatever=20 > becomes a detail that can be chosen on a case by case basis. My actual solution looks like this: class ObservableCollection(Observable): @contextmanager def notify(self): self.notify_observers(EVT_BEFORE_COLLECTION_CHANGED) yield self.notify_observers(EVT_AFTER_COLLECTION_CHANGED) class ObservableList(ObservableCollection, list): __slots__ =3D [] def remove(self, value): with self.notify(): super().remove(value) def clear(self): with self.notify(): super().clear() def pop(self): with self.notify(): return super().pop() It seems to be working with the built in list, dict and set types. There must be a better way! Something like this: @wrap_notify('remove', 'clear', 'append', 'insert', 'sort'): class ObservableList(ObservableCollection, list): pass I just can't find out the right syntax.