Re: Observer implementations

2009-06-17 Thread Mike C. Fletcher
Tobias Weber wrote:
 In article mailman.1631.1245160392.8015.python-l...@python.org,
  Mike C. Fletcher mcfle...@vrplumber.com wrote:

   
 See PyDispatcher for code to do this.
 

 That was the original problem. Got it now: if used inside the class 
 definition dispatcher.connect will raise cannot create weak reference 
 to 'classmethod' object. Outside (using Class.method) it works fine.
   
Ah, I think you've got a logic problem there.  The classmethod during
class creation has no reference to the class being created, so if you
were to call *that* thing it would blow up:

class x( object ):
@classmethod
def y( cls, text ):
print text
y( 'No you do not!' )

if you were to create a reference to *that* thing (the decorated
un-bound class method) it would never have the binding for cls, so it
wouldn't do anything.  The correct way IMO to bind after-class-creation
(e.g. by a class decorator or metaclass) where you reference a bound
class method.  If you *really* want to do it this way, you can do
something like this:

class x( object ):
@classmethod
def y( cls, value ):
pass
dispatcher.connect( lambda *args: x.y( *args ), signal='hello' )

but ick.

HTH,
Mike

-- 

  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com

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


Re: Observer implementations

2009-06-16 Thread Mike C. Fletcher

Tobias Weber wrote:
...

No time to reinvent the wheel

I'd still need to know how to weakref a classmethod
  

See PyDispatcher for code to do this.

PyDispatcher, at least, is not abandoned, it would be more accurate to 
say finished.  I use it in OpenGLContext (extensively), but I haven't 
had to change anything in a rather long time.  It pretty much just works.


Enjoy,
Mike

--

 Mike C. Fletcher
 Designer, VR Plumber, Coder
 http://www.vrplumber.com
 http://blog.vrplumber.com

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


Re: Observer implementations

2009-06-15 Thread Gerhard Häring
Tobias Weber wrote:
 Hi,
 how to use the Observer pattern in Python?

Implement it in your classes?

 I found PubSub and PyDispatcher, both of which are abandoned. [...]

I haven't searched for these, but googling for python observer pattern
yields http://code.activestate.com/recipes/131499/ and this seems  like
a good inspiritation for owns own implementation.

-- Gerhard

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


Re: Observer implementations

2009-06-15 Thread Carl Banks
On Jun 15, 5:22 pm, Tobias Weber t...@gmx.net wrote:
 In article mailman.1584.1245073461.8015.python-l...@python.org,
  Gerhard Häring g...@ghaering.de wrote:

  Implement it in your classes?

 No time to reinvent the wheel

Hmm, observer pattern seems to be one of those things simple enough
that reimplementing the wheel might actually be less work than
adapting a third-party implementation.  But I'll leave that decision
to you.


 I'd still need to know how to weakref a classmethod

I can't imagine why you would need to weak reference a class method,
since they usually live forever along with the class.  But again, you
are doing it so you would know.  I can't think of an easy way to do it
without metaclass programming.

Unless you are trying to create a weakref to a bound classmethod.  I'd
say that's still pretty useless: bound classmethods only keep alive
classes and classmethods, which both live forever, and bound
classmethods are lightweight and hardly worth the effort.

Maybe show us what you need to weak reference classmethods for and we
can help you better.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Observer implementations

2009-06-15 Thread I V
On Mon, 15 Jun 2009 15:29:34 +0200, Tobias Weber wrote:
 Despite the confusion all those are useable, but I ran into the problem
 that I can't register a @classmethod because weakref doesn't like them.

What do you mean by weakref not liking class methods? This seems to work 
OK on python 2.6

class C(object):
@classmethod
def cm(cls):
return Class method of  + str(cls)

cm = C.cm
print cm()
# Outputs:
# Class method of class '__main__.C'

w = weakref.ref(cm)
print w 
# Outputs: 
# weakref at 0x1a362b8; to 'instancemethod' at 0x7ff1cc9ebb40 (cm)

print w()
# Outputs: 
# bound method type.cm of class '__main__.C'

print w()()
# Outputs:
# Class method of class '__main__.C'

del cm
print w
# Outputs:
# weakref at 0x1a362b8; dead

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