AOP decorator?

G

gentlestone

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?
 
T

Terry Reedy

Hi,

suppose my source code looks like:

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
 
J

Jon Clements

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top