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_pickle_variable(self):
operation = Operations.IDENTITY
returned_operation = pickle.loads(pickle.dumps(operation))
self._test_for_equality(operation, returned_operation)
def test_simplify_multiply_by_one_right(self):
node = Node(Operations.MULTIPLICATION,
left=Node(Operations.IDENTITY, value='x'),
right=Node(Operations.NUMBER, value=1))
result = node.simplify()
self.assertEqual(result, True)
self.assertEqual(node.operation, Operations.IDENTITY)
self.assertEqual(node.value, 'x')
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)
traverse_tree(node.left)
if node.is_binary():
if node.left is None:
leaves.append(node)
else:
traverse_tree(node.left)
if node.right is not None:
traverse_tree(node.right)
traverse_tree(root)
for node in leaves:
if random.random() > 0.5:
node.operation = Operations.NUMBER
node.value = Expression.generate_number()
else:
node.operation = Operations.IDENTITY
node.value = random.choice(variables)
return Expression(root=root, variables=variables)
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)