Decorator question

2010-10-16 Thread Lucasm
Hello,

I have a decorator problem and hope someone is able to help me out/
assist me. Thanks in advance.

Suppose:
### Begin some_library_module ###

def some_decorator(some_method):
def inner(an_arg, *args, **kwargs):
return some_method(an_arg, *args, **kwargs)
return inner

### End some_library_module ###

### Begin my_module ###

def my_decorator(some_method):
def inner(self, an_arg, *args, **kwargs):
self.do_something()
return some_decorator(some_method)(an_arg, *args, **kwargs)
return inner


class My_Class(object):
@my_decorator
def my_method(self, an_arg, *args, **kwargs):
print self, an_arg

def do_something(self):
pass

### End My_module ###

>>> My_Class().my_method('bla')
TypeError: my_method() takes at least 2 arguments (1 given)

`self` is lost in the process, because my decorator does use it and
the library's doesn't. I fail to  find a way to keep self in the args
without modifying the library.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asyncore.poll() question

2010-10-16 Thread Lucasm
On 16 Okt, 15:31, chad  wrote:
> At the following url..
>
> http://www.nightmare.com/medusa/programming.html
>
> The author has the following code for a simple HTTP client
>
> #!/usr/bin/python
>
> import asyncore
> import socket
> import string
>
> class http_client (asyncore.dispatcher):
>
>     def __init__ (self, host, path):
>         asyncore.dispatcher.__init__ (self)
>         self.path = path
>         self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
>         self.connect ((host, 80))
>
>     def handle_connect (self):
>         self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)
>
>     def handle_read (self):
>         data = self.recv (8192)
>         print data
>
>     def handle_write (self):
>         pass
>
> if __name__ == '__main__':
>     import sys
>     import urlparse
>     for url in sys.argv[1:]:
>         parts = urlparse.urlparse (url)
>         if parts[0] != 'http':
>             raise ValueError, "HTTP URL's only, please"
>         else:
>             host = parts[1]
>             path = parts[2]
>             http_client (host, path)
>     asyncore.loop()
>
> Right after that, the author states the following...
>
> " A really good way to understand select() is to put a print statement
> into the asyncore.poll() function:
>
>         [...]
>         (r,w,e) = select.select (r,w,e, timeout)
>         print '---'
>         print 'read', r
>         print 'write', w
>         [...]
>
> Each time through the loop you will see which channels have fired
> which events.
> "
>
> How the heck do I modify the code put the print statement into the
> asyncore.poll() function?
>
> Chad

Hi,

You can find the file in your Python directory, in my case /usr/lib/
Python2.6/asyncore.py. You should delete the .pyc file to make sure it
is recompiled. And you will need root access :).

Lucas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Decorator question

2010-10-17 Thread Lucasm
On 16 Okt, 16:49, Peter Otten <[email protected]> wrote:
> Lucasm wrote:
> > I have a decorator problem and hope someone is able to help me out/
> > assist me. Thanks in advance.
>
> > Suppose:
> > ### Begin some_library_module ###
>
> > def some_decorator(some_method):
> >     def inner(an_arg, *args, **kwargs):
> >         return some_method(an_arg, *args, **kwargs)
> >     return inner
>
> > ### End some_library_module ###
>
> > ### Begin my_module ###
>
> > def my_decorator(some_method):
> >     def inner(self, an_arg, *args, **kwargs):
> >         self.do_something()
>
> At this point some_method() is just a Python function. You have to build a
> bound method or at least something that "knows" about self before you pass
> it on:
>
>           bound_method = some_method.__get__(self)
>           # bound_method = new.instancemethod(some_method, self)
>           # bound_method = functools.partial(some_method, self)
>           return some_decorator(bound_method)(an_arg, *args, **kwargs)
>
>
>
> >     return inner
>
> > class My_Class(object):
> >     @my_decorator
> >     def my_method(self, an_arg, *args, **kwargs):
> >         print self, an_arg
>
> >     def do_something(self):
> >         pass
>
> > ### End My_module ###
>
> >>>> My_Class().my_method('bla')
> > TypeError: my_method() takes at least 2 arguments (1 given)
>
> > `self` is lost in the process, because my decorator does use it and
> > the library's doesn't. I fail to  find a way to keep self in the args
> > without modifying the library.

Thanks a lot! I didn't expect that it would be that easy :)

Regards,
Lucas
-- 
http://mail.python.org/mailman/listinfo/python-list


overriding a property

2010-10-19 Thread Lucasm
Hi,

A question. Is it possible to dynamically override a property?

class A(object):
@property
def return_five(self):
return 5

I would like to override the property for an instance of A to say the
string 'bla'.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: overriding a property

2010-10-20 Thread Lucasm
On 19 Okt, 16:07, Benjamin Peterson  wrote:
> Lucasm  gmail.com> writes:
>
> > I would like to override the property for an instance of A to say the
> > string 'bla'.
>
> A.return_five = "blah"

I guess you did not test that. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: overriding a property

2010-10-20 Thread Lucasm
On 19 Okt, 18:28, Steven D'Aprano  wrote:
> On Tue, 19 Oct 2010 06:39:56 -0700, Lucasm wrote:
> > Hi,
>
> > A question. Is it possible to dynamically override a property?
>
> > class A(object):
> >     @property
> >     def return_five(self):
> >         return 5
>
> > I would like to override the property for an instance of A to say the
> > string 'bla'.
> >>> class A(object):
>
> ...     _five = 5  # class attribute shared by all instances
> ...     @property
> ...     def return_five(self):
> ...         return self._five
> ...
>
> >>> a = A()
> >>> a._five = 'bla'  # set an instance attribute
> >>> b = A()
> >>> print a.return_five
> bla
> >>> print b.return_five
>
> 5
>
> --
> Steven

Thanks for the answers. I would like to override the property though
without making special modifications in the main class beforehand. Is
this possible?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: overriding a property

2010-10-20 Thread Lucasm
On 20 Okt, 16:09, Peter Otten <[email protected]> wrote:
> Lucasm wrote:
> > On 19 Okt, 18:28, Steven D'Aprano  > cybersource.com.au> wrote:
> >> On Tue, 19 Oct 2010 06:39:56 -0700, Lucasm wrote:
> >> > Hi,
>
> >> > A question. Is it possible to dynamically override a property?
>
> >> > class A(object):
> >> > @property
> >> > def return_five(self):
> >> > return 5
>
> >> > I would like to override the property for an instance of A to say the
> >> > string 'bla'.
> >> >>> class A(object):
>
> >> ...     _five = 5  # class attribute shared by all instances
> >> ...     @property
> >> ...     def return_five(self):
> >> ...         return self._five
> >> ...
>
> >> >>> a = A()
> >> >>> a._five = 'bla'  # set an instance attribute
> >> >>> b = A()
> >> >>> print a.return_five
> >> bla
> >> >>> print b.return_five
>
> >> 5
>
> >> --
> >> Steven
>
> > Thanks for the answers. I would like to override the property though
> > without making special modifications in the main class beforehand. Is
> > this possible?
>
> You can dynamically change the instance's class:
>
> >>> class A(object):
>
> ...     @property
> ...     def five(self): return 5
> ...>>> a = A()
> >>> b = A()
> >>> class B(type(b)):
>
> ...     @property
> ...     def five(self): return "FIVE"
> ...>>> b.__class__ = B
> >>> a.five
> 5
> >>> b.five
>
> 'FIVE'
>
> But still -- what you are trying looks like a bad idea. What's your usecase?
>
> Peter

Thanks for your answer. That's exactly the thing I'm doing right now
and it works :) My use case is testing. I want to test a class and
reduce the complexity of the output generated by certain properties.
It would be nice to know alternatives to this approach.
-- 
http://mail.python.org/mailman/listinfo/python-list