Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_evaluate_should_raise(self):
expr = Expression()
self.assertRaises(NotImplementedError, expr.evaluate)
Guard.against_empty(args, "In order to create a new attribute expression you need to provide some attributes.")
self.attributes = []
self.add_attributes(args)
def add_attributes(self, attrs):
for attr in attrs:
if isinstance(attr, GetAttributeExpression):
self.add_attributes(attr.attributes)
else:
self.attributes.append(attr)
def __unicode__(self):
return unicode(".".join([str(attr) for attr in self.attributes]))
__str__ = __unicode__
class UnaryExpression(Expression):
#operation types
CollectionLength = "CollectionLength"
Negate = "Negate"
Not = "Not"
#operation representations
representations = {
CollectionLength:"len(%s)",
Negate:"negate(%s)",
Not:"(not %s)",
}
def __init__(self, node_type, rhs):
'''Initializes the UnaryExpression with the specified arguments.
Arguments:
node_type - Specifies the type of operation that this UnaryExpression represents
def __unicode__(self):
return unicode("%s" % self.value)
__str__ = __unicode__
class NameExpression(Expression):
def __init__(self, name):
self.name = name
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return unicode(self.name)
class GetAttributeExpression(Expression):
def __init__(self, *args):
Guard.against_empty(args, "In order to create a new attribute expression you need to provide some attributes.")
self.attributes = []
self.add_attributes(args)
def add_attributes(self, attrs):
for attr in attrs:
if isinstance(attr, GetAttributeExpression):
self.add_attributes(attr.attributes)
else:
self.attributes.append(attr)
def __unicode__(self):
return unicode(".".join([str(attr) for attr in self.attributes]))
__str__ = __unicode__
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from os.path import dirname, abspath, join
root_path = abspath(join(dirname(__file__), "../../"))
sys.path.insert(0, root_path)
from pynq.guard import Guard
class Expression(object):
def evaluate(self):
raise NotImplementedError("The evaluate method needs to be overriden in a base class of Expression.")
class ConstantExpression(Expression):
def __init__(self, value):
'''Initializes the ConstantExpression with the specified value.
Arguments:
value - Value to initialize the ConstantExpression with.
'''
self.value = value
def evaluate(self):
'''Returns the value for this constant expression.'''
return self.value
def __unicode__(self):
return unicode("%s" % self.value)
__str__ = __unicode__
class NameExpression(Expression):
def __init__(self, value):
'''Initializes the ConstantExpression with the specified value.
Arguments:
value - Value to initialize the ConstantExpression with.
'''
self.value = value
def evaluate(self):
'''Returns the value for this constant expression.'''
return self.value
def __unicode__(self):
return unicode("%s" % self.value)
__str__ = __unicode__
class NameExpression(Expression):
def __init__(self, name):
self.name = name
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return unicode(self.name)
class GetAttributeExpression(Expression):
def __init__(self, *args):
Guard.against_empty(args, "In order to create a new attribute expression you need to provide some attributes.")
self.attributes = []
self.add_attributes(args)
def add_attributes(self, attrs):
node_type - Specifies the type of operation that this UnaryExpression represents
rhs - Right-hand site of the operation. Since this is an unary operation, this is the only argument.
'''
Guard.against_empty(node_type, "The UnaryExpression node type is required")
if node_type == self.CollectionLength:
Guard.accepts(rhs, (ConstantExpression,), "The CollectionLength unary expression can only take ConstantExpressions that hold tuples or lists as parameters.")
if not isinstance(rhs.evaluate(), (list, tuple)):
raise ValueError("The CollectionLength unary expression can only take ConstantExpressions that hold tuples or lists as parameters.")
self.node_type = node_type
self.rhs = rhs
def __str__(self):
'''Returns a string representing the expression.'''
return self.representations[self.node_type] % str(self.rhs)
class BinaryExpression(Expression):
#operation types
#Arithmetic
Add = "Add"
Subtract = "Subtract"
Multiply = "Multiply"
Divide = "Divide"
Power = "Power"
Modulo = "Modulo"
#Bitwise
And = "And"
Or = "Or"
#Comparison Operators
Equal = "Equal"