Re: manipulating class attributes from a decorator while the class is being defined

2008-04-21 Thread Bruno Desthuilliers
Wilbert Berendsen a écrit :
 Hi, is it possible to manipulate class attributes from within a decorator 
 while the class is being defined?
 
 I want to register methods with some additional values in a class attribute. 
 But I can't get a decorator to change a class attribute while the class is 
 still being defined. Something like:
 
 class Parser(object):
   
   regexps = []
   def reg(regexp):
 def deco(func):
   regexps.append((regexp, func))
   return func
 return deco
   
   @reg(r'.*')
   def quoted_string(self):
 pass
 
 How can I reach the class attribute `regexps' from within a decorator?

Simple answer : you can't. Because, as you noticed, the class object 
doesn't exist yet.

The canonical solutions are either to store regexps outside the class 
(ie: as a module level variable) - which can be problematic in some 
cases -, or to use a two-pass scheme using a decorator and a metaclass, 
where the decorator annotate the function with required informations for 
latter processing, and the metaclass do the effective processing.

There are of course other solutions, some of them possibly simpler. The 
'best' solution of course depends on intented use of your class...

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


Re: manipulating class attributes from a decorator while the class is being defined

2008-04-21 Thread Duncan Booth
Wilbert Berendsen [EMAIL PROTECTED] wrote:

 Hi, is it possible to manipulate class attributes from within a
 decorator while the class is being defined?
 
 I want to register methods with some additional values in a class
 attribute. But I can't get a decorator to change a class attribute
 while the class is still being defined. Something like:
 
 class Parser(object):
   
   regexps = []
   def reg(regexp):
 def deco(func):
   regexps.append((regexp, func))
   return func
 return deco
   
   @reg(r'.*')
   def quoted_string(self):
 pass
 
 How can I reach the class attribute `regexps' from within a decorator?
 

Have you tried passing regexps into the decorator as a default argument?

  def reg(regexp, regexps=regexps):
def deco(func):
  regexps.append((regexp, func))
  return func
return deco
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: manipulating class attributes from a decorator while the class is being defined

2008-04-21 Thread Gerard Flanagan
On Apr 19, 11:19 pm, Wilbert Berendsen [EMAIL PROTECTED] wrote:
 Hi, is it possible to manipulate class attributes from within a decorator
 while the class is being defined?

 I want to register methods with some additional values in a class attribute.
 But I can't get a decorator to change a class attribute while the class is
 still being defined. Something like:

 class Parser(object):

   regexps = []
   def reg(regexp):
 def deco(func):
   regexps.append((regexp, func))
   return func
 return deco

   @reg(r'.*')
   def quoted_string(self):
 pass

 How can I reach the class attribute `regexps' from within a decorator?

 Thanks for any help,
 Wilbert Berendsen

 --http://www.wilbertberendsen.nl/
 You must be the change you wish to see in the world.
 -- Mahatma Gandhi

---

def reg(regexp):
def deco(func):
def inner(self, *args, **kw):
if not hasattr(self, 'regexps'):
self.regexps = []
self.regexps.append((regexp, func))
return func(self, *args, **kw)
return inner
return deco

class Parser(object):

regexps = []


@reg(r'.*')
def quoted_string(self):
print 'hi'



p = Parser()

p.quoted_string()

print p.regexps
---
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: manipulating class attributes from a decorator while the class is being defined

2008-04-20 Thread Arnaud Delobelle
On Apr 19, 10:19 pm, Wilbert Berendsen [EMAIL PROTECTED] wrote:
 Hi, is it possible to manipulate class attributes from within a decorator
 while the class is being defined?

 I want to register methods with some additional values in a class attribute.
 But I can't get a decorator to change a class attribute while the class is
 still being defined. Something like:

 class Parser(object):

   regexps = []
   def reg(regexp):
     def deco(func):
       regexps.append((regexp, func))
       return func
     return deco

   @reg(r'.*')
   def quoted_string(self):
     pass

 How can I reach the class attribute `regexps' from within a decorator?

In this particular example, it is enought to change the line

   def reg(regexp):

to:

   def reg(regexp, regexps=regexps):

So that 'regexps' inside reg() will point to the same object as
'regexps' outside reg().

Of course it wouldn't work well with inheritance, but it seems to me
that you don't want to subclass 'Parser' anyway.  If you did, you
could instead have something like this:

class Ruleset(list):

def add(self, regexp):
def decorator(func):
self.append((regexp, func))
return func
return decorator


class Parser(object):

rules = Ruleset()

@rules.add(r'.*')
def quoted_string(self):
   pass


Or, yet another solution is to change the metaclass of 'Parser' so
that it populates the class's rules by scanning its attributes.

HTH

--
Arnaud

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


manipulating class attributes from a decorator while the class is being defined

2008-04-19 Thread Wilbert Berendsen
Hi, is it possible to manipulate class attributes from within a decorator 
while the class is being defined?

I want to register methods with some additional values in a class attribute. 
But I can't get a decorator to change a class attribute while the class is 
still being defined. Something like:

class Parser(object):
  
  regexps = []
  def reg(regexp):
def deco(func):
  regexps.append((regexp, func))
  return func
return deco
  
  @reg(r'.*')
  def quoted_string(self):
pass

How can I reach the class attribute `regexps' from within a decorator?

Thanks for any help,
Wilbert Berendsen

-- 
http://www.wilbertberendsen.nl/
You must be the change you wish to see in the world.
-- Mahatma Gandhi
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: manipulating class attributes from a decorator while the class is being defined

2008-04-19 Thread Christian Heimes
Wilbert Berendsen schrieb:
 Hi, is it possible to manipulate class attributes from within a decorator 
 while the class is being defined?
 
 I want to register methods with some additional values in a class attribute. 
 But I can't get a decorator to change a class attribute while the class is 
 still being defined. Something like:
 
 class Parser(object):
   
   regexps = []
   def reg(regexp):
 def deco(func):
   regexps.append((regexp, func))
   return func
 return deco
   
   @reg(r'.*')
   def quoted_string(self):
 pass
 
 How can I reach the class attribute `regexps' from within a decorator?

It's really tricky . The class object doesn't exists yet. It's created
after all functions are parsed and created. You have can walk up the
stack frames but it's ugly.

Christian

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


Re: manipulating class attributes from a decorator while the class is being defined

2008-04-19 Thread Karl-Heinz Ruskowski
 How can I reach the class attribute `regexps' from within a decorator?
Now, the first way that comes to my mind is simply overloading the class and 
set your regexps variable in your new class. 

The other way is to create an object and set it more manually (obj.regexps = 
['.*']). Which for me is an ugly this to do because the first way is far more 
elegant :) 

-- 
GPG key: 0x04B3BB96


pgpqg8hSnWlWX.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list