[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

AOP decorator?

gentlestone

3/1/2010 4:22:00 PM

Hi,

suppose my source code looks like:
--------------------------------
import aspect_xy
class Basic(object, aspect_xy.Basic):
pass # basic attributes and methods ...
--------------------------------
and the source code of aspect_xy.py is:
--------------------------------
class Basic(object):
pass # aspect extra attributes and methods ...
--------------------------------
how can I write this with decorators? I want something like:
-------------------------------
import aspect_xy
@aspect_xy # extra attributes and methods ...
class Basic(object):
pass # basic attributes and methods ...
----------------------------------------------------------
I want to write class decorator function, which:
1. Takes the basic class as parameter
2. Find the same class name in aspect_xy.py
3. Inherit the found aspect class
Is it possible?






2 Answers

Terry Reedy

3/1/2010 7:41:00 PM

0

On 3/1/2010 11:22 AM, gentlestone wrote:
> Hi,
>
> suppose my source code looks like:
> --------------------------------
> import aspect_xy
> class Basic(object, aspect_xy.Basic):
> pass # basic attributes and methods ...

As a sidenote, this violates my understanding of aspect-oriented
programming, which is to add aspects that go across several derived
classes. But...

> --------------------------------
> and the source code of aspect_xy.py is:
> --------------------------------
> class Basic(object):
> pass # aspect extra attributes and methods ...
> --------------------------------
> how can I write this with decorators? I want something like:
> -------------------------------
> import aspect_xy
> @aspect_xy # extra attributes and methods ...
> class Basic(object):
> pass # basic attributes and methods ...
> ----------------------------------------------------------
> I want to write class decorator function, which:
> 1. Takes the basic class as parameter
> 2. Find the same class name in aspect_xy.py
> 3. Inherit the found aspect class
> Is it possible?

You would have to look at the difference between Basic with and without
the explicit base class and patch the latter to look like the former. At
minimum, you would have to fix .__bases__ and .__mro__, but I do not
know if those are writable. To me, it seems easier to be explicit.

tjr

Jon Clements

3/1/2010 7:50:00 PM

0

On Mar 1, 4:22 pm, gentlestone <tibor.b...@hotmail.com> wrote:
> Hi,
>
> suppose my source code looks like:
> --------------------------------
>   import aspect_xy
>   class Basic(object, aspect_xy.Basic):
>     pass # basic attributes and methods ...
> --------------------------------
> and the source code of aspect_xy.py is:
> --------------------------------
>   class Basic(object):
>     pass  # aspect extra attributes and methods ...
> --------------------------------
> how can I write this with decorators? I want something like:
> -------------------------------
>   import aspect_xy
>   @aspect_xy # extra attributes and methods ...
>   class Basic(object):
>     pass # basic attributes and methods ...
> ----------------------------------------------------------
> I want to write class decorator function, which:
> 1. Takes the basic class as parameter
> 2. Find the same class name in aspect_xy.py
> 3. Inherit the found aspect class
> Is it possible?

Untested and written quickly, so all possible disclaimers apply ;)

class BaseObject(object):
pass

def aspect_xy_dec(base):
def wrapper(cls):
return type(cls.__name__, (base, getattr(aspect_xy, cls.__name__)),
{})
return wrapper

@aspect_xy(BaseObject)
class SomeObject:
pass


Cheers,

Jon.