Skip to content

Commit

Permalink
Add import command and module data type
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpri10 committed Feb 14, 2016
1 parent ac42f89 commit 521b322
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
2 changes: 2 additions & 0 deletions tests/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_eval_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion voicecoding/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.0"
__version__ = "2.0.0"
11 changes: 11 additions & 0 deletions voicecoding/commandfunctions/import_module.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions voicecoding/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
20 changes: 12 additions & 8 deletions voicecoding/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 521b322

Please sign in to comment.