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_simplify_identical_variables_division(self):
node = Node(Operations.DIVISION,
left=Node(Operations.IDENTITY, value='x'),
right=Node(Operations.IDENTITY, value='x'))
result = node.simplify()
self.assertEqual(result, True)
self.assertEqual(node.left, None)
self.assertEqual(node.right, None)
self.assertEqual(node.value, 1.0)
def test_pickle_expression(self):
node = Node(Operations.PLUS,
Node(Operations.MULTIPLICATION,
left=Node(Operations.IDENTITY, value='x'),
right=Node(Operations.NUMBER, value=4)),
Node(Operations.MULTIPLICATION,
left=Node(Operations.IDENTITY, value='y'),
right=Node(Operations.NUMBER, value=2)))
e = Expression(root=node, variables=['x', 'y'])
returned_expression = pickle.loads(pickle.dumps(e))
self.assertEqual(e.variables, returned_expression.variables)
self.assertEqual(e.root.operation._operation_type, returned_expression.root.operation._operation_type)
self.assertEqual(e.root.operation.action, returned_expression.root.operation.action)
self.assertEqual(e.root.value, returned_expression.root.value)
self.assertEqual(e.root.left.operation._operation_type, returned_expression.root.left.operation._operation_type)
self.assertEqual(e.root.left.operation.action, returned_expression.root.left.operation.action)
self.assertEqual(e.root.left.value, returned_expression.root.left.value)
self.assertEqual(e.root.right.operation._operation_type, returned_expression.root.right.operation._operation_type)
self.assertEqual(e.root.right.operation.action, returned_expression.root.right.operation.action)
self.assertEqual(e.root.right.value, returned_expression.root.right.value)
def setUp(self):
root = Node(Operations.PLUS,
Node(Operations.MULTIPLICATION,
left=Node(Operations.IDENTITY, value='x'),
right=Node(Operations.NUMBER, value=4)),
Node(Operations.MULTIPLICATION,
left=Node(Operations.IDENTITY, value='y'),
right=Node(Operations.NUMBER, value=2)))
self.f = Expression(root=root, variables=['x', 'y'])
def test_exact_value(self):
answer = Node(Operations.PLUS,
Node(Operations.MULTIPLICATION,
left=Node(Operations.IDENTITY, value='x'),
right=Node(Operations.NUMBER, value=4)),
Node(Operations.MULTIPLICATION,
left=Node(Operations.IDENTITY, value='y'),
right=Node(Operations.NUMBER, value=2)))
e = Expression(root=answer, variables=['x', 'y'])
self.assertEqual(self.f.expression_value(e), 0.0)
def is_binary(self):
return self._operation_type == Operations._binary_operation
#leave only 3 digits after decimal point
if self.is_number():
self.value = round(self.value * 1000) / 1000
#calculate unary function for number
if self.is_unary() and self.left.is_number():
self.value = self.value_in_point({})
self.operation = Operations.NUMBER
self.left = None
return True
#calculate binary function for two numbers
if (self.is_binary() and self.left.is_number() and
self.right.is_number()):
self.value = self.value_in_point({})
self.operation = Operations.NUMBER
self.left = self.right = None
return True
#calculate x / x
if (self.is_binary() and
self.left.is_variable() and self.right.is_variable() and
self.left.value == self.right.value and self.operation == Operations.DIVISION):
self.value = 1
self.operation = Operations.NUMBER
self.left = self.right = None
return True
#calculate x - x
if (self.is_binary() and
self.left.is_variable() and self.right.is_variable() and
self.left.value == self.right.value and self.operation == Operations.MINUS):
def __setstate__(self, state):
"""
Initializes operation where unpickling
"""
if state[self._dict_key] == 'number':
operator = Operations.NUMBER
elif state[self._dict_key] == 'variable':
operator = Operations.IDENTITY
elif state[self._dict_key] == '+':
operator = Operations.PLUS
elif state[self._dict_key] == '-':
operator = Operations.MINUS
elif state[self._dict_key] == '*':
operator = Operations.MULTIPLICATION
elif state[self._dict_key] == '/':
operator = Operations.DIVISION
elif state[self._dict_key] == 'sin':
operator = Operations.SIN
elif state[self._dict_key] == 'cos':
operator = Operations.COS
self._init_from_operation(operator)