Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109748

Re: UserList - which methods needs to be overriden?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Nagy László Zsolt <gandalf@shopzeus.com>
Newsgroups comp.lang.python
Subject Re: UserList - which methods needs to be overriden?
Date Thu, 9 Jun 2016 15:55:16 +0200
Lines 73
Message-ID <mailman.115.1465480515.2306.python-list@python.org> (permalink)
References <b245d88e-9b90-bb90-bd2a-13ee82f7a5c8@shopzeus.com> <njbdhk$pv2$1@ger.gmane.org> <917ca9c4-aef1-6681-ec70-62de8916117d@shopzeus.com> <njbp5d$mn9$1@ger.gmane.org> <7cdb398f-b3f2-9a3c-b444-2229f8496e2f@shopzeus.com> <13f5d47e-39e2-76ce-e0c6-195a4bd25e7b@shopzeus.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de dVlgXOnEHwsG8wnPZy2BkwoGmEB31S3E+qlHGUhba3RA==
Return-Path <gandalf@shopzeus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'finally:': 0.05; 'one?': 0.05; '*args,': 0.07; 'wrapped': 0.07; 'subject:which': 0.09; 'def': 0.13; '__slots__': 0.16; 'cls': 0.16; 'contextlib': 0.16; 'list):': 0.16; 'nice:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:@ 20': 0.16; 'duplicate': 0.18; 'working.': 0.18; 'decorator': 0.22; 'pass': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'yield': 0.27; "skip:' 10": 0.28; '**kwargs):': 0.29; "i'm": 0.30; "can't": 0.32; 'class': 0.33; 'gets': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'skip:o 20': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'body': 0.61; 'charset:windows-1252': 0.62; '100%': 0.72; 'd[1]': 0.84; 'decorator.': 0.84; 'dict,': 0.84; 'horrible': 0.84; 'subject:needs': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=shopzeus.com; s=shopzeus_com; t=1465480513; bh=1w3ay1yvZ5pBwbRe8FHluO4zrUf40HxYaGj3XRwkSeU=; h=Subject:To:References:From:Date:In-Reply-To:From; b=Qn0dgc+sIyZtK7vV+IWBU/QMzYo4Hy5wx+2opmiSEnkAgS308irWqeuUcvyfcH6wq AccG9rrrZ87tPx2ZSQQL0JgCzB6kudqMQdLNXcgxLN44jPcwEy9/1wITOW3Zswtt1P 6XXEakbMWzAXWn76lLkCO209+rlw02PJo9kdrzBV9+3x+p10/CDWW+cnoI3o9eamjT YTQA1WlPnjdmdyAXB+oLi+o+X3T18ARyX1Oz3SqVTkB9X5glilOw75a+3VLpCjJg6Y JAFSCfbDRNyh7JJIAgG55TfGjv2nJlLqPtJjtgUiMSdyThNtvANs068rccSN0Gmf6R BbbQTySve6t7A==
In-Reply-To <7cdb398f-b3f2-9a3c-b444-2229f8496e2f@shopzeus.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <13f5d47e-39e2-76ce-e0c6-195a4bd25e7b@shopzeus.com>
X-Mailman-Original-References <b245d88e-9b90-bb90-bd2a-13ee82f7a5c8@shopzeus.com> <njbdhk$pv2$1@ger.gmane.org> <917ca9c4-aef1-6681-ec70-62de8916117d@shopzeus.com> <njbp5d$mn9$1@ger.gmane.org> <7cdb398f-b3f2-9a3c-b444-2229f8496e2f@shopzeus.com>
Xref csiph.com comp.lang.python:109748

Show key headers only | View raw


> @wrap_notify('remove', 'clear', 'append', 'insert', 'sort'):
> class ObservableList(ObservableCollection, list):
>     pass
>
> I just can't find out the right syntax.
>
>
All right, this is working.

from contextlib import contextmanager

class ObservableCollection:
    @contextmanager
    def notify(cls):
        print("notify before change")
        yield
        print("notify after change")


def wrap_notify(cls, basecls, method_names):
    for method_name in method_names:
        orig = getattr(basecls, method_name)
        def modified(self, *args, **kwargs):
            with self.notify(): return orig(self,*args,**kwargs)
        setattr(cls, method_name, modified)
    return cls




class ObservableDict(ObservableCollection, dict):
    __slots__ = []
wrap_notify(ObservableDict, dict, ['update', '__delitem__',
'__setitem__']) # many others...

class ObservableList(ObservableCollection, list):
    __slots__ = []
wrap_notify(ObservableList, list, ['append', 'pop']) # many others...


d =  ObservableDict()
d[1] = 2
print(d)


But I'm still not 100% satisfied. wrap_notify is not a class decorator.
Is there a way to make a class decorator that gets the class as its
first parameter? It would be very nice:

def wrap_notify(cls, method_names):
    class Wrapped(cls):
        pass
    for method_name in method_names:
        orig = getattr(Wrapped, method_name)
        def modified(self, *args, **kwargs):
            with self.notify(): return orig(self,*args,**kwargs)
        setattr(cls, method_name, modified)
    return Wrapped


@wrap_notify(['update', '__delitem__', '__setitem__']) # many others...
class ObservableDict(ObservableCollection, dict):
    __slots__ = []

@wrap_notify(['append', 'pop']) # many others...
class ObservableList(ObservableCollection, list):
    __slots__ = []

And finally: is this Pythonic, or a horrible mess? Would it be better to
duplicate the body of each method one by one?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: UserList - which methods needs to be overriden? Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-09 15:55 +0200

csiph-web