Expressions.py :  » Development » Frowns » frowns » Smarts » 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 » Development » Frowns 
Frowns » frowns » Smarts » Expressions.py
"""A set of objects that can create logical expressions via
combining logical operators and Smarts Primitives.
"""
class NotMatch:
    def __init__(self, child):
        self.child = child
    def __eq__(self, obj):
        return not self.child == obj
    def __str__(self):
        return "not (%s)" % (self.child,)

class AndMatch:
    def __init__(self, left, right):
        self.left = left
        self.right = right
    def __eq__(self, obj):
        return self.left == obj and self.right == obj
    def __str__(self):
        return "AND(%s, %s)" % (self.left, self.right)
    
class OrMatch:
    def __init__(self, left, right):
        self.left = left
        self.right = right
    def __eq__(self, obj):
        return (self.left == obj) or (self.right == obj)
    def __str__(self):
        return "OR(%s, %s)" % (self.left, self.right)

bool_unary_not = 76
bool_strong_and = 77
bool_or = 78
bool_weak_and = 79
binary_operators = [bool_strong_and, bool_or, bool_weak_and]
boolean_operators = binary_operators + [bool_unary_not]
text_to_bool = {
    "&": bool_strong_and,
    ",": bool_or,
    ";": bool_weak_and,
    "!": bool_unary_not,
    }

class ExpressionList:
    def __init__(self):
        self.matchers = []
        
    def __nonzero__(self):
        return len(self.matchers) != 0
    
    def add_matcher(self, obj):
        # see if the matcher is not in the boolean list
        if self.matchers:
            target = self.matchers[-1]
            for _matcher in boolean_operators:
                if target is _matcher:
                    break
            else:
                self.matchers.append(bool_strong_and)
        #if self.matchers and self.matchers[-1] not in boolean_operators:
        #    self.matchers.append(bool_strong_and)
        self.matchers.append(obj)
        
    def add_operator(self, op):
        assert op in binary_operators or op == bool_unary_not
        if __debug__:
            if self.matchers:
                if op in binary_operators:
                    s = self.matchers[-1]
                    for _matcher in binary_operators:
                        assert s is not _matcher
            else:
                for _matcher in binary_operators:
                    assert op is not _matcher

        self.matchers.append(op)

    def make_matcher(self):
        matchers = self.matchers[:]
        i = 0
        while i < len(matchers):
            if matchers[i] is bool_unary_not:
                matchers[i:i+2] = [NotMatch(matchers[i+1])]
            else:
                i = i + 1
        i = 1
        while i < len(matchers):
            if matchers[i] is bool_strong_and:
                matchers[i-1:i+2] = [AndMatch(matchers[i-1], matchers[i+1])]
            else:
                i = i + 1
        i = 1
        while i < len(matchers):
            if matchers[i] is bool_or:
                matchers[i-1:i+2] = [OrMatch(matchers[i-1], matchers[i+1])]
            else:
                i = i + 1
        i = 1
        while i < len(matchers):
            if matchers[i] is bool_weak_and:
                matchers[i-1:i+2] = [AndMatch(matchers[i-1], matchers[i+1])]
            else:
                i = i + 1
        assert len(matchers) == 1, matchers
        return matchers[0]

class AtomExpression(ExpressionList):
    pass

class BondExpression(ExpressionList):
    pass
        

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.