substitution of a method by a callable object
Can I substitute a method of a class by a callable object (not a function)? I can very easy insert my function in a class as a method, but an object - can't. I have the following: class Foo(object): pass class Obj(object): def __call__(self, obj_self): print 'Obj' def func(self): print 'func' f = Foo() Foo.meth = func f.meth() # all goes OK Foo.meth = Obj() f.meth() # I get TypeError: __call__() takes exactly 2 arguments (1 given) -- http://mail.python.org/mailman/listinfo/python-list
Re: substitution of a method by a callable object
On 22 окт, 20:46, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > netimen a écrit : > > > Can I substitute a method of a class by a callable object (not a > > function)? I can very easy insert my function in a class as a method, > > but an object - can't. > > functions implement the descriptor protocol so when looked up as class > attributes, the lookup invoke their __get__ method, which in turn > returns a method object (which is a thin wrapper around the function, > the class and the instance). > > You can either build the method manually (as Georges explained), or make > your own Obj class a proper descriptor: > > from types import MethodType > > class Obj(object): > __name__ = "Obj" # for Method.__repr_ > > def __call__(self, obj_self): > print 'Obj' > > def __get__(self, instance, cls): > return MethodType(self, instance, cls) > > HTH thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: substitution of a method by a callable object
On 23 окт, 00:34, Terry Reedy <[EMAIL PROTECTED]> wrote: > George Sakkis wrote: > > On Oct 22, 12:13 pm, netimen <[EMAIL PROTECTED]> wrote: > > >> Can I substitute a method of a class by a callable object (not a > >> function)? I can very easy insert my function in a class as a method, > >> but an object - can't. > > >> I have the following: > > >> class Foo(object): > >> pass > > >> class Obj(object): > >> def __call__(self, obj_self): > >> print 'Obj' > > >> def func(self): > >> print 'func' > > >> f = Foo() > >> Foo.meth = func > >> f.meth() # all goes OK > >> Foo.meth = Obj() > >> f.meth() # I get TypeError: __call__() takes exactly 2 arguments (1 > >> given) > > > You have to wrap it as an (unbound) instance method explicitly: > > Nope. As the error message says, the method was called with nothing > provided to be bound to the extraneous parameter obj_self. Either > provide an arg, such as with f.meth(1), *or* delete obj_self and 'Obj' > is printed, with both 2.5 and 3.0. OK, I have implemented Bruno Desthuilliers example. But there is another question: can I having a method determine if it is an instance of given class. So: class Obj(object): __name__ = "Obj" # for Method.__repr_ def __call__(self, obj_self): print 'Obj' def __get__(self, instance, cls): return MethodType(self, instance, cls) class Foo(object): pass Foo.meth = Obj() ## in some another place of code if isinstance(Foo.meth, Obj): # doesn't work because type(Foo.meth) is now 'instancemethod' ... Can I determine that? -- http://mail.python.org/mailman/listinfo/python-list
Re: substitution of a method by a callable object
On 23 окт, 00:26, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > netimen a écrit : > (snip) > > > OK, I have implemented Bruno Desthuilliers example. But there is > > another question: can I having a method determine if it is an instance > > of given class. So: > > As the name imply, a method is usually, well, an instance of type > 'method' !-) > > > > > class Obj(object): > (snip) > > > class Foo(object): > > pass > > > Foo.meth = Obj() > > > ## in some another place of code > > if isinstance(Foo.meth, Obj): # doesn't work because type(Foo.meth) is > > now 'instancemethod' > > Indeed. I suppose what you want is to know if the callable wrapped by > the method is an instance of Obj ? > > > > > Can I determine that? > > Yeps. Test the im_func attribute of the method: > > if isinstance(Foo.meth.im_func, Obj): print "yadda" > > But you'd better put this in a try/except block, because a callable > attribute of a class is not necessarily a method - and so it may not > have an im_func attribute. > > Also, may I ask why you want to check this ? Not that it's necessarily > wrong, but real use case for typechecking are pretty rare. Thanks! I have wasted much time playing with different Foo.meth trying to get to Obj through them. But im_func was one of the fields I haven't checked ) Indeed, I have managed already without that. The task was to substitute __str__ method by a complex formatter. It called a low- level formatter, a function wich returned simple str(self). That function could be called with objects with substituted formatters and with simple objects. So to avoid recursion I wanted to check wether the formatter of my object was substituted. -- http://mail.python.org/mailman/listinfo/python-list
substitution __str__ method of an instance
I couldn't substitute __str__ method of an instance. Though I managed to substitute ordinary method of an instance: from types import MethodType class Foo(object): pass class Printer(object): def __call__(self, obj_self): return 'printed' f = Foo() f.printer = MethodType(Printer(), f, Foo) print f.printer() # works fine - I get: 'printed' print f # get: <__main__.Foo object at 0x00D69C10> f.__str__ = MethodType(Printer(), f, Foo) print f # still get: <__main__.Foo object at 0x00D69C10>. Why? Foo.__str__ = MethodType(Printer(), None, Foo) print f # works fine - I get: 'printed' How can I substitute __str__ method of an instance? -- http://mail.python.org/mailman/listinfo/python-list
brackets content regular expression
I have a text containing brackets (or what is the correct term for '>'?). I'd like to match text in the uppermost level of brackets. So, I have sth like: ' 123 < 1 aaa < t bbb < a ff > > 2 > b'. How to match text between the uppermost brackets ( 1 aaa < t bbb < a ff > > 2 )? P.S. sorry for my english. -- http://mail.python.org/mailman/listinfo/python-list
Re: brackets content regular expression
Thank's but if i have several top-level groups and want them match one
by one:
text = "a < b < с > d > here starts a new group: < e < f > g >"
I want to match first " b < с > d " and then " e < f > g " but not "
b < с > d > here starts a new group: < e < f > g "
On 31 окт, 20:53, Matimus <[EMAIL PROTECTED]> wrote:
> On Oct 31, 10:25 am, netimen <[EMAIL PROTECTED]> wrote:
>
> > I have a text containing brackets (or what is the correct term for
> > '>'?). I'd like to match text in the uppermost level of brackets.
>
> > So, I have sth like: ' 123 < 1 aaa < t bbb < a ff > > 2 >
> > b'. How to match text between the uppermost brackets ( 1 aaa < t
> > bbb < a ff > > 2 )?
>
> > P.S. sorry for my english.
>
> I think most people call them "angle brackets". Anyway it should be
> easy to just match the outer most brackets:
>
> >>> import re
> >>> text = " 123 < 1 aaa < t bbb < a ff > > 2 >"
> >>> r = re.compile("<(.+)>")
> >>> m = r.search(text)
> >>> m.group(1)
>
> ' 1 aaa < t bbb < a ff > > 2 '
>
> In this case the regular expression is automatically greedy, matching
> the largest area possible. Note however that it won't work if you have
> something like this: " ".
>
> Matt
--
http://mail.python.org/mailman/listinfo/python-list
Re: brackets content regular expression
there may be different levels of nesting:
"a < b < Ó > d > here starts a new group: < 1 < e < f > g > 2 >
another group: < 3 >"
On 31 окт, 21:57, netimen <[EMAIL PROTECTED]> wrote:
> Thank's but if i have several top-level groups and want them match one
> by one:
>
> text = "a < b < Ó > d > here starts a new group: < e < f > g >"
>
> I want to match first " b < Ó > d " and then " e < f > g " but not "
> b < Ó > d > here starts a new group: < e < f > g "
> On 31 ÏËÔ, 20:53, Matimus <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Oct 31, 10:25šam, netimen <[EMAIL PROTECTED]> wrote:
>
> > > I have a text containing brackets (or what is the correct term for
> > > '>'?). I'd like to match text in the uppermost level of brackets.
>
> > > So, I have sth like: ' 123 < 1 aaa < t bbb < a ff > > 2 >
> > > b'. How to match text between the uppermost brackets ( 1 aaa < t
> > > bbb < a ff > > 2 )?
>
> > > P.S. sorry for my english.
>
> > I think most people call them "angle brackets". Anyway it should be
> > easy to just match the outer most brackets:
>
> > >>> import re
> > >>> text = " 123 < 1 aaa < t bbb < a ff > > 2 >"
> > >>> r = re.compile("<(.+)>")
> > >>> m = r.search(text)
> > >>> m.group(1)
>
> > ' 1 aaa < t bbb < a ff > > 2 '
>
> > In this case the regular expression is automatically greedy, matching
> > the largest area possible. Note however that it won't work if you have
> > something like this: " ".
>
> > Matt
--
http://mail.python.org/mailman/listinfo/python-list
Re: brackets content regular expression
Yeah, I know it's quite simple to do manually. I was just interested
if it could be done by regular expressions. Thank you anyway.
On 1 нояб, 00:36, Matimus <[EMAIL PROTECTED]> wrote:
> On Oct 31, 11:57 am, netimen <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Thank's but if i have several top-level groups and want them match one
> > by one:
>
> > text = "a < b < Ó > d > here starts a new group: < e < f > g >"
>
> > I want to match first " b < Ó > d " and then " e < f > g " but not "
> > b < Ó > d > here starts a new group: < e < f > g "
> > On 31 ÏËÔ, 20:53, Matimus <[EMAIL PROTECTED]> wrote:
>
> > > On Oct 31, 10:25šam, netimen <[EMAIL PROTECTED]> wrote:
>
> > > > I have a text containing brackets (or what is the correct term for
> > > > '>'?). I'd like to match text in the uppermost level of brackets.
>
> > > > So, I have sth like: ' 123 < 1 aaa < t bbb < a ff > > 2 >
> > > > b'. How to match text between the uppermost brackets ( 1 aaa < t
> > > > bbb < a ff > > 2 )?
>
> > > > P.S. sorry for my english.
>
> > > I think most people call them "angle brackets". Anyway it should be
> > > easy to just match the outer most brackets:
>
> > > >>> import re
> > > >>> text = " 123 < 1 aaa < t bbb < a ff > > 2 >"
> > > >>> r = re.compile("<(.+)>")
> > > >>> m = r.search(text)
> > > >>> m.group(1)
>
> > > ' 1 aaa < t bbb < a ff > > 2 '
>
> > > In this case the regular expression is automatically greedy, matching
> > > the largest area possible. Note however that it won't work if you have
> > > something like this: " ".
>
> > > Matt
>
> As far as I know, you can't do that with a regular expressions (by
> definition regular expressions aren't recursive). You can use a
> regular expression to aid you, but there is no magic expression that
> will give it to you for free.
>
> In this case it is actually pretty easy to do it without regular
> expressions at all:
>
> >>> text = "a < b < O > d > here starts a new group: < e < f > g >"
> >>> def get_nested_strings(text, depth=0):
>
> ... stack = []
> ... for i, c in enumerate(text):
> ... if c == '<':
> ... stack.append(i)
> ... elif c == '>':
> ... start = stack.pop() + 1
> ... if len(stack) == depth:
> ... yield text[start:i]
> ...>>> for seg in get_nested_strings(text):
>
> ... print seg
> ...
> b < O > d
> e < f > g
>
> Matt
--
http://mail.python.org/mailman/listinfo/python-list
