I can define variables
This commit is contained in:
@@ -86,6 +86,8 @@ class Sheerka(Concept):
|
|||||||
}
|
}
|
||||||
self.sheerka_pipeables = {}
|
self.sheerka_pipeables = {}
|
||||||
|
|
||||||
|
self.locals = {}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def resolved_concepts_by_first_keyword(self):
|
def resolved_concepts_by_first_keyword(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(self.NAME, [BuiltinConcepts.EVALUATION], 50)
|
super().__init__(self.NAME, [BuiltinConcepts.EVALUATION], 50)
|
||||||
self.locals = {}
|
self.globals = {}
|
||||||
|
|
||||||
def matches(self, context, return_value):
|
def matches(self, context, return_value):
|
||||||
return return_value.status and \
|
return return_value.status and \
|
||||||
@@ -65,19 +65,18 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
|||||||
not_for_me = context.sheerka.new(BuiltinConcepts.NOT_FOR_ME, body=node)
|
not_for_me = context.sheerka.new(BuiltinConcepts.NOT_FOR_ME, body=node)
|
||||||
return sheerka.ret(self.name, False, not_for_me, parents=[return_value])
|
return sheerka.ret(self.name, False, not_for_me, parents=[return_value])
|
||||||
|
|
||||||
# get locals
|
# get globals
|
||||||
my_globals = self.get_globals(context, node)
|
my_globals = self.get_globals(context, node)
|
||||||
my_locals = {}
|
|
||||||
context.log(f"globals={my_globals}", self.name)
|
context.log(f"globals={my_globals}", self.name)
|
||||||
|
|
||||||
# eval
|
# eval
|
||||||
if isinstance(node.ast_, ast.Expression):
|
if isinstance(node.ast_, ast.Expression):
|
||||||
context.log("Evaluating using 'eval'.", self.name)
|
context.log("Evaluating using 'eval'.", self.name)
|
||||||
compiled = compile(node.ast_, "<string>", "eval")
|
compiled = compile(node.ast_, "<string>", "eval")
|
||||||
evaluated = eval(compiled, my_globals, my_locals)
|
evaluated = eval(compiled, my_globals, sheerka.locals)
|
||||||
else:
|
else:
|
||||||
context.log("Evaluating using 'exec'.", self.name)
|
context.log("Evaluating using 'exec'.", self.name)
|
||||||
evaluated = self.exec_with_return(node.ast_, my_globals, my_locals)
|
evaluated = self.exec_with_return(node.ast_, my_globals, sheerka.locals)
|
||||||
|
|
||||||
context.log(f"{evaluated=}", self.name)
|
context.log(f"{evaluated=}", self.name)
|
||||||
return sheerka.ret(self.name, True, evaluated, parents=[return_value])
|
return sheerka.ret(self.name, True, evaluated, parents=[return_value])
|
||||||
@@ -99,8 +98,8 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
|||||||
self.update_globals_with_context(my_globals, context)
|
self.update_globals_with_context(my_globals, context)
|
||||||
self.update_globals_with_node(my_globals, context, node)
|
self.update_globals_with_node(my_globals, context, node)
|
||||||
|
|
||||||
if self.locals: # when extra values are given. Add them
|
if self.globals: # when extra values are given. Add them
|
||||||
my_globals.update(self.locals)
|
my_globals.update(self.globals)
|
||||||
|
|
||||||
my_globals["sheerka"] = Expando(method_from_sheerka) # it's the last, so I cannot be overridden
|
my_globals["sheerka"] = Expando(method_from_sheerka) # it's the last, so I cannot be overridden
|
||||||
return my_globals
|
return my_globals
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
|||||||
|
|
||||||
parsed = PythonParser().parse(context, ParserInput("get_concept_name(c:foo:)"))
|
parsed = PythonParser().parse(context, ParserInput("get_concept_name(c:foo:)"))
|
||||||
python_evaluator = PythonEvaluator()
|
python_evaluator = PythonEvaluator()
|
||||||
python_evaluator.locals["get_concept_name"] = get_concept_name
|
python_evaluator.globals["get_concept_name"] = get_concept_name
|
||||||
evaluated = python_evaluator.eval(context, parsed)
|
evaluated = python_evaluator.eval(context, parsed)
|
||||||
|
|
||||||
assert evaluated.status
|
assert evaluated.status
|
||||||
@@ -130,7 +130,7 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
|||||||
# sanity, does not work otherwise
|
# sanity, does not work otherwise
|
||||||
parsed = PythonParser().parse(context, ParserInput("get_concept_name(foo)"))
|
parsed = PythonParser().parse(context, ParserInput("get_concept_name(foo)"))
|
||||||
python_evaluator = PythonEvaluator()
|
python_evaluator = PythonEvaluator()
|
||||||
python_evaluator.locals["get_concept_name"] = get_concept_name
|
python_evaluator.globals["get_concept_name"] = get_concept_name
|
||||||
evaluated = python_evaluator.eval(context, parsed)
|
evaluated = python_evaluator.eval(context, parsed)
|
||||||
|
|
||||||
assert not evaluated.status
|
assert not evaluated.status
|
||||||
@@ -150,6 +150,18 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
|||||||
assert evaluated.status
|
assert evaluated.status
|
||||||
assert sheerka.get_concepts_weights(BuiltinConcepts.PRECEDENCE) == {'1001': 1, '1002': 2}
|
assert sheerka.get_concepts_weights(BuiltinConcepts.PRECEDENCE) == {'1001': 1, '1002': 2}
|
||||||
|
|
||||||
|
def test_i_can_define_variables(self):
|
||||||
|
sheerka, context = self.init_concepts()
|
||||||
|
parsed = PythonParser().parse(context, ParserInput("a=10"))
|
||||||
|
python_evaluator = PythonEvaluator()
|
||||||
|
|
||||||
|
python_evaluator.eval(context, parsed)
|
||||||
|
|
||||||
|
parsed = PythonParser().parse(context, ParserInput("a"))
|
||||||
|
evaluated = python_evaluator.eval(context, parsed)
|
||||||
|
|
||||||
|
assert evaluated.status
|
||||||
|
assert evaluated.body == 10
|
||||||
|
|
||||||
# @pytest.mark.parametrize("text, concept_key, concept_id, use_concept", [
|
# @pytest.mark.parametrize("text, concept_key, concept_id, use_concept", [
|
||||||
# ("__C__key__C__", "key", None, False),
|
# ("__C__key__C__", "key", None, False),
|
||||||
|
|||||||
Reference in New Issue
Block a user