From 521b322e7b1d92d2a35267480878994ad19531bc Mon Sep 17 00:00:00 2001 From: Michael Prieto Date: Sun, 14 Feb 2016 16:49:59 -0500 Subject: [PATCH] Add import command and module data type --- tests/test_data_types.py | 2 ++ tests/test_eval_command.py | 2 ++ voicecoding/__init__.py | 2 +- voicecoding/commandfunctions/import_module.py | 11 ++++++++++ voicecoding/data_types.py | 22 +++++++++++++++++++ voicecoding/instructions.py | 20 ++++++++++------- 6 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 voicecoding/commandfunctions/import_module.py diff --git a/tests/test_data_types.py b/tests/test_data_types.py index a571b02..e7be9e8 100644 --- a/tests/test_data_types.py +++ b/tests/test_data_types.py @@ -7,6 +7,7 @@ "command, expected", [ ("string hello world", '"hello world"'), ("hello world", '"hello world"'), + ("hello world method upper", '"hello world".upper()'), ("integer 10", "10"), ("10", "10"), ("ten", "10"), @@ -35,6 +36,7 @@ ("set", "set()"), ("set one cut two cut x", "{1, 2, x}"), ("function string params x", "str(x)"), + ("module math method factorial params ten", "math.factorial(10)") ] ) def test_data_types(command, expected): diff --git a/tests/test_eval_command.py b/tests/test_eval_command.py index 42da5db..8e2780b 100644 --- a/tests/test_eval_command.py +++ b/tests/test_eval_command.py @@ -8,6 +8,8 @@ ("assign one hundred to variable x", "x = 100"), ("call function print params hello world", 'print("hello world")'), ("print hello world", 'print("hello world")'), + ("import math", "import math"), + ("import date time", "import datetime") ] ) def test_single_line(command, expected): diff --git a/voicecoding/__init__.py b/voicecoding/__init__.py index 6849410..8c0d5d5 100644 --- a/voicecoding/__init__.py +++ b/voicecoding/__init__.py @@ -1 +1 @@ -__version__ = "1.1.0" +__version__ = "2.0.0" diff --git a/voicecoding/commandfunctions/import_module.py b/voicecoding/commandfunctions/import_module.py new file mode 100644 index 0000000..73add7f --- /dev/null +++ b/voicecoding/commandfunctions/import_module.py @@ -0,0 +1,11 @@ +from ..code_class import Code + + +# imports a module so it can be utilized +def import_module(to_parse): + # formats module name + module = to_parse.replace(" ", "") + + # add module to defined vars so it doesn't have to be specified + Code.defined_vars.add(module) + return "import {0}".format(module) diff --git a/voicecoding/data_types.py b/voicecoding/data_types.py index 699e9d6..88d2ef5 100644 --- a/voicecoding/data_types.py +++ b/voicecoding/data_types.py @@ -194,6 +194,26 @@ def check_var_assumed(val): return variable +def check_module(val): + module = val.replace(" ", "") + if not module: + return False + else: + return module + + +def check_module_assumed(val): + module = val.replace(" ", "") + module = module.replace("-", "") + if not module: + return False + else: + if module not in Code.defined_vars: + return False + else: + return module + + # returns an equation def check_equation(val): # maps words to an operation @@ -375,6 +395,7 @@ def check_func(val): # data types that don't need to be explicitly named assumed_data_types = [ check_var_assumed, + check_module_assumed, check_int, check_float, check_str @@ -387,6 +408,7 @@ def check_func(val): "float": check_float, "boolean": check_bool, "variable": check_var, + "module": check_module, "equation": check_equation, "comparison": check_comp, "list": check_list, diff --git a/voicecoding/instructions.py b/voicecoding/instructions.py index 514f290..9555e4e 100644 --- a/voicecoding/instructions.py +++ b/voicecoding/instructions.py @@ -6,13 +6,17 @@ from .commandfunctions.while_loop import while_loop from .commandfunctions.def_func import def_func from .commandfunctions.return_func import return_func +from .commandfunctions.import_module import import_module # commands that can be called by the user; the first word in the voice command -instructions = {"assign": assign_variable, - "print": print_data, - "call": call_func_method, - "if": if_else, - "for": for_loop, - "while": while_loop, - "define": def_func, - "return": return_func} +instructions = { + "assign": assign_variable, + "print": print_data, + "call": call_func_method, + "if": if_else, + "for": for_loop, + "while": while_loop, + "define": def_func, + "return": return_func, + "import": import_module +}