Re: safely rename a method with a decorator

2009-03-22 Thread MRAB

Daniel Fetchinson wrote:

I'd like to implement a decorator that would rename the method which
it decorates. Since it's a tricky thing in general involving all sorts
of __magic__ I thought I would ask around first before writing
something buggy :)

It should work something like this:

class myclass( object ):
@rename( 'hello' )
def method( self ):
print 'ok'

# tests

inst = myclass( )
inst.method( )   # raise an AttributeError
inst.hello( )   # prints 'ok'
myclass.method   # raise an AttributeError
myclass.hello   # prints unbound method myclass.hello
assert 'method' in dir( myclass ) is False
assert 'hello' in dir( myclass ) is True

Any ideas?


What is your use case? Why don't you just give the method the right name
in the first place? :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: safely rename a method with a decorator

2009-03-22 Thread Daniel Fetchinson
 I'd like to implement a decorator that would rename the method which
 it decorates. Since it's a tricky thing in general involving all sorts
 of __magic__ I thought I would ask around first before writing
 something buggy :)

 It should work something like this:

 class myclass( object ):
 @rename( 'hello' )
 def method( self ):
 print 'ok'

 # tests

 inst = myclass( )
 inst.method( )   # raise an AttributeError
 inst.hello( )   # prints 'ok'
 myclass.method   # raise an AttributeError
 myclass.hello   # prints unbound method myclass.hello
 assert 'method' in dir( myclass ) is False
 assert 'hello' in dir( myclass ) is True

 Any ideas?

 What is your use case? Why don't you just give the method the right name
 in the first place? :-)

The use case is that I'm writing a turbogears application in which the
URLs are determined by the method names. People might want to change
these names if they want to change the URLs. One way would be to put
the method names into a turbogears configuration file and the @rename
decorator could fetch it from there.

Cheers,
Daniel



-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: safely rename a method with a decorator

2009-03-22 Thread R. David Murray
Daniel Fetchinson fetchin...@googlemail.com wrote:
  I'd like to implement a decorator that would rename the method which
  it decorates. Since it's a tricky thing in general involving all sorts
  of __magic__ I thought I would ask around first before writing
  something buggy :)
 
  It should work something like this:
 
  class myclass( object ):
  @rename( 'hello' )
  def method( self ):
  print 'ok'
 
  # tests
 
  inst = myclass( )
  inst.method( )   # raise an AttributeError
  inst.hello( )   # prints 'ok'
  myclass.method   # raise an AttributeError
  myclass.hello   # prints unbound method myclass.hello
  assert 'method' in dir( myclass ) is False
  assert 'hello' in dir( myclass ) is True
 
  Any ideas?
 
  What is your use case? Why don't you just give the method the right name
  in the first place? :-)
 
 The use case is that I'm writing a turbogears application in which the
 URLs are determined by the method names. People might want to change
 these names if they want to change the URLs. One way would be to put
 the method names into a turbogears configuration file and the @rename
 decorator could fetch it from there.

Use a WSGI routing engine instead.

--
R. David Murray   http://www.bitdance.com

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


Re: safely rename a method with a decorator

2009-03-22 Thread andrew cooke

there was discussion related to this same problem earlier in the week.

http://groups.google.com/group/comp.lang.python/browse_thread/thread/ad08eb9eb83a4e61/d1906cbc26e16d15?q=Mangle+function+name+with+decorator%3F

andrew


Daniel Fetchinson wrote:
 I'd like to implement a decorator that would rename the method which
 it decorates. Since it's a tricky thing in general involving all sorts
 of __magic__ I thought I would ask around first before writing
 something buggy :)

 It should work something like this:

 class myclass( object ):
 @rename( 'hello' )
 def method( self ):
 print 'ok'

 # tests

 inst = myclass( )
 inst.method( )   # raise an AttributeError
 inst.hello( )   # prints 'ok'
 myclass.method   # raise an AttributeError
 myclass.hello   # prints unbound method myclass.hello
 assert 'method' in dir( myclass ) is False
 assert 'hello' in dir( myclass ) is True

 Any ideas?

 Cheers,
 Daniel

 --
 Psss, psss, put it down! - http://www.cafepress.com/putitdown
 --
 http://mail.python.org/mailman/listinfo/python-list




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


Re: safely rename a method with a decorator

2009-03-22 Thread Daniel Fetchinson
 there was discussion related to this same problem earlier in the week.

 http://groups.google.com/group/comp.lang.python/browse_thread/thread/ad08eb9eb83a4e61/d1906cbc26e16d15?q=Mangle+function+name+with+decorator%3F


Thanks this was very helpful!


 I'd like to implement a decorator that would rename the method which
 it decorates. Since it's a tricky thing in general involving all sorts
 of __magic__ I thought I would ask around first before writing
 something buggy :)

 It should work something like this:

 class myclass( object ):
 @rename( 'hello' )
 def method( self ):
 print 'ok'

 # tests

 inst = myclass( )
 inst.method( )   # raise an AttributeError
 inst.hello( )   # prints 'ok'
 myclass.method   # raise an AttributeError
 myclass.hello   # prints unbound method myclass.hello
 assert 'method' in dir( myclass ) is False
 assert 'hello' in dir( myclass ) is True

 Any ideas?

 Cheers,
 Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list