dispatch_example.py :  » Aspect-Oriented » logilab-aspects » aspects-0.1.4 » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Aspect Oriented » logilab aspects 
logilab aspects » aspects 0.1.4 » examples » dispatch_example.py
# -*- coding: iso-8859-1 -*-
# Copyright (c) 2003 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

"""
Example to show how DispatcherAspect (multimethods) works.

The shape example has been taken from the following article :
http://www.onlamp.com/pub/a/python/2003/05/29/multimethods.html
"""

__revision__ = '$id$'

# XXX ??? Is it useful ???
# Maybe use it with the Visitor DP ...

class Shape:
    """Basic interface for shape
    """

    def area(self):
        """returns the object's area
        """
        pass

    def combine_shape(self, other_shape):
        """The 'KEY' function for this example. This one will be called
        by user without any tests to do to check which other shape will
        be combined to self object.
        """
        print "I'm a %s and I don't want to check anything !" % \
              (self.__class__.__name__)

        

class Square(Shape):
    """Square implementation of Shape interface
    """

    def __init__(self, size):
        """
        """
        self.size = size
        
    def area(self):
        """returns the area of this square
        """
        return self.size**2

    def combine_with_square(self, square):
        """Combine self with square
        """
        # Combine stuff ...
        print "Combining a %s with a Square" % (self.__class__.__name__)

    def combine_with_circle(self, circle):
        """Combine self with circle
        """
        # Combine stuff ...
        print "Combining a %s with a Circle" % (self.__class__.__name__)
        
    
    def combine_with_triangle(self, triangle):
        """Combine self with triangle
        """
        # Combine stuff ...
        print "Combining a %s with a Triangle" % (self.__class__.__name__)



class Triangle(Shape):
    """Triangle implementation of Shape interface
    """

    def __init__(self, base_size, highness):
        """
        """
        self.base_size = base_size
        self.highness = highness

    def area(self):
        """returns the area of this triangle
        """
        return (self.base_size*self.highness) / 2.


    def combine_with_square(self, square):
        """Combine self with square
        """
        # Combine stuff ...
        print "Combining a %s with a Square" % (self.__class__.__name__)

    def combine_with_circle(self, circle):
        """Combine self with circle
        """
        # Combine stuff ...
        print "Combining a %s with a Circle" % (self.__class__.__name__)
        
    
    def combine_with_triangle(self, triangle):
        """Combine self with triangle
        """
        # Combine stuff ...
        print "Combining a %s with a Triangle" % (self.__class__.__name__)


class Circle(Shape):
    """Circle implementation of Shape interface
    """

    def __init__(self, radius):
        """
        """
        self.radius = radius
        

    def area(self):
        """returns the area of this cirlce
        """
        return 3.14159 * self.radius * self.radius

    def combine_with_square(self, square):
        """Combine self with square
        """
        # Combine stuff ...
        print "Combining a %s with a Square" % (self.__class__.__name__)

    def combine_with_circle(self, circle):
        """Combine self with circle
        """
        # Combine stuff ...
        print "Combining a %s with a Circle" % (self.__class__.__name__)
        
    
    def combine_with_triangle(self, triangle):
        """Combine self with triangle
        """
        # Combine stuff ...
        print "Combining a %s with a Triangle" % (self.__class__.__name__)



# Main Stuff ###################################################################

from logilab.aspects.lib.multimethods import DispatcherAspect,DispatchRule
from logilab.aspects.weaver import weaver


def define_rules():
    """Define shapes' combining rules
    """
    
    rule_s = DispatchRule("combine_shape",
                          "combine_with_square", [Square])
    
    rule_t = DispatchRule("combine_shape",
                          "combine_with_triangle", [Triangle])

    rule_c = DispatchRule("combine_shape",
                          "combine_with_circle", [Circle])
    
    
    return [rule_c, rule_s, rule_t]
    

def run():
    """Example
    """

    rules = define_rules()
    circle = Circle(12)
    square = Square(12)
    triangle = Triangle(10, 15)

    print "*"*30
    print "Trying to combine a square in a triangle : "
    triangle.combine_shape(square)

    print "Now, weave multimethods aspect, this should make a difference !"
    weaver.weave_methods(Shape, DispatcherAspect, rules)

    # Combining Triangle
    print "COMBINING Triangle with all types of shapes"
    triangle.combine_shape(square)
    triangle.combine_shape(circle)
    triangle.combine_shape(triangle)

    # Combining Circle
    print "COMBINING Circles with all types of shapes"
    circle.combine_shape(square)
    circle.combine_shape(circle)
    circle.combine_shape(triangle)

    # Combining Square
    print "COMBINING Square with all types of shapes"
    square.combine_shape(square)
    square.combine_shape(circle)
    square.combine_shape(triangle)

    print "*" * 30
    
    

if __name__ == '__main__':
    run()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.