Created a small utility program to generate JSON files for testing the main input.

This commit is contained in:
2020-04-12 00:03:33 -05:00
parent 4386b1af99
commit 1265f8ac3e
7 changed files with 294 additions and 8 deletions
+96
View File
@@ -0,0 +1,96 @@
import tokenizer
import token
import expression
import json
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".')
def transform(object):
if isinstance(object, expression.Expression) or isinstance(object, expression.Variable):
return object.__dict__
else:
raise TypeError("Only Books and Index will be JSON serialized!")
exp = input("Enter the expression the student will use: ")
tokens = tokenizer.get_tokens_from_expression_string(exp)
for t in tokens:
if t.type == token.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("data.json", "w", encoding="utf-8") as f:
json.dump(expression.Expression(exp, variables), f, default=transform, indent = 4)