98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
import tokenizer
|
|
import ctoken
|
|
import expression
|
|
import jsonpickle
|
|
import copy
|
|
|
|
variables = {}
|
|
def print_vars(vars):
|
|
i = 0
|
|
for key in vars.keys():
|
|
var = vars[key]
|
|
if i != 0:
|
|
print(", %s" %(var.name), end = "")
|
|
else:
|
|
print("%s " %(var.name), end = "")
|
|
i += 1
|
|
print()
|
|
|
|
def modify_variable(var, vars):
|
|
print("%s is now selected." %(var.name))
|
|
print("The following properties are avaliable: Variable Type, Starting Point and Ending Point.")
|
|
print('Commands are "type", "start" and "end", respectly.')
|
|
|
|
while True:
|
|
response = input("Select a property to modify for %s or q to exit property modifying: " %(var.name))
|
|
|
|
if response == "type":
|
|
response = input("Enter the type of variable this is (currently %s): " %(expression.Variable.get_variable_type(var.type)))
|
|
vars[var.name].type = response
|
|
print("Updated to %s." %(response))
|
|
|
|
elif response == "start":
|
|
response = input("Enter the variable's starting point (currently %f): " %(var.starting_point))
|
|
|
|
try:
|
|
vars[var.name].starting_point = float(response)
|
|
print("Updated to %f." %(float(response)))
|
|
except ValueError:
|
|
print("The starting point must be a number.")
|
|
|
|
elif response == "end":
|
|
response = input("Enter the variable's ending point (currently %f): " %(var.ending_point))
|
|
|
|
try:
|
|
vars[var.name].ending_point = float(response)
|
|
print("Updated to %f." %(float(response)))
|
|
except ValueError:
|
|
print("The ending point must be a number.")
|
|
|
|
pass
|
|
|
|
elif response == "q":
|
|
return
|
|
|
|
else:
|
|
print('Invalid property "%s".' %(response))
|
|
print('Commands are "type", "start" and "end".')
|
|
|
|
exp = input("Enter the expression the student will use: ")
|
|
wrong_response = input("Enter the wrong response for the expression: ")
|
|
try:
|
|
wrong_response = float(wrong_response)
|
|
except ValueError:
|
|
print("The anwser must be numeric! Will not be included in output file.")
|
|
tokens = tokenizer.get_tokens_from_expression_string(exp)
|
|
rev_tokens = copy.deepcopy(tokens)
|
|
rev_tokens.reverse()
|
|
|
|
for t in rev_tokens:
|
|
if t.type == ctoken.TokenType.variable:
|
|
variables[t.value] = expression.Variable(t.value, expression.Variable.get_variable_type(t.value))
|
|
|
|
selected_variable = None
|
|
|
|
while True:
|
|
print("Variables in expression:")
|
|
print_vars(variables)
|
|
response = input("Enter a variable name to modify its properties or continue (default): ")
|
|
|
|
if response == "continue" or response == "":
|
|
break
|
|
|
|
if response in variables:
|
|
selected_variable = variables[response]
|
|
|
|
if selected_variable != None:
|
|
modify_variable(selected_variable, variables)
|
|
|
|
else:
|
|
print("Invalid selection %s." %(response))
|
|
continue
|
|
|
|
with open("expression.json", "w", encoding="utf-8") as f:
|
|
obj = jsonpickle.encode(expression.Expression(exp, variables, wrong_response))
|
|
f.write(obj)
|
|
f.close()
|
|
#json.dump(expression.Expression(exp, variables, correct_answer), f, default=transform, indent = 4)
|