AOP decorator?

Discussion in 'Python' started by gentlestone, Mar 1, 2010.

  1. gentlestone

    gentlestone Guest

    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?
    gentlestone, Mar 1, 2010
    #1
    1. Advertising

  2. gentlestone

    Terry Reedy Guest

    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
    Terry Reedy, Mar 1, 2010
    #2
    1. Advertising

  3. gentlestone

    Jon Clements Guest

    On Mar 1, 4:22 pm, gentlestone <> 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.
    Jon Clements, Mar 1, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anu Raj Pradhan
    Replies:
    0
    Views:
    370
    Anu Raj Pradhan
    Jul 14, 2003
  2. X_AWemner_X

    XDoclet, JBOSS-AOP Example3

    X_AWemner_X, Aug 7, 2003, in forum: Java
    Replies:
    0
    Views:
    687
    X_AWemner_X
    Aug 7, 2003
  3. Michael N. Christoff

    Using AOP to implement dynamic proxies

    Michael N. Christoff, Dec 5, 2003, in forum: Java
    Replies:
    3
    Views:
    670
    Deepak Nayal
    Dec 8, 2003
  4. Tomer Ben-David

    AOP Java its implications

    Tomer Ben-David, Jan 28, 2004, in forum: Java
    Replies:
    3
    Views:
    397
    Dale King
    Feb 5, 2004
  5. glomde
    Replies:
    5
    Views:
    518
    glomde
    Mar 29, 2007
Loading...

Share This Page