Created
December 13, 2009 13:21
-
-
Save bebraw/255413 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
class TestUnsetVariable(TestInterpreter): | |
def test_unset_variable(self): | |
assert self.interpret('a') == 'null' | |
def test_variable_in_expression(self): | |
assert self.interpret('a+3') == 'null' | |
class TestPrintVariables(TestInterpreter): | |
command = 'variables' | |
def test_print_none(self): | |
assert self.interpret(self.command) == 'No stored variables' | |
def test_print_variables(self): | |
self.interpret('a=14') | |
self.interpret('b=-4') | |
self.interpret("animal='boar'") | |
assert self.interpret(self.command) == "Stored variables:\n" + \ | |
"a=14\nb=-4\nanimal='boar'" | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Interpreter: | |
def __init__(self): | |
self.vars = {} | |
def interpret(self, expression): | |
if expression in ('variables', 'vars'): | |
variable_str = '' | |
for name, value in self.vars.items(): | |
if isinstance(value, str): | |
value_str = "'" + value + "'" | |
else: | |
value_str = str(value) | |
variable_str += '\n' + name + '=' + value_str | |
if variable_str: | |
return 'Stored variables:' + variable_str | |
return 'No stored variables' | |
try: | |
return eval(expression, {}, self.vars) | |
except NameError: | |
return 'null' | |
except SyntaxError: | |
l_value, r_value = expression.split('=') | |
try: | |
self.vars[l_value] = int(r_value) | |
except ValueError: | |
self.vars[l_value] = self.interpret(r_value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
class TestCleanVariables(TestInterpreter): | |
def test_clean_none(self): | |
assert self.interpret('vars') == 'No stored variables' | |
self.interpret('clean') | |
assert self.interpret('vars') == 'No stored variables' | |
def test_clean_variables(self): | |
self.interpret('a=12') | |
assert self.interpret('vars') == 'Stored variables:\na=12' | |
self.interpret('clean') | |
assert self.interpret('vars') == 'No stored variables' | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
return 'No stored variables' | |
elif expression is 'clean': | |
self.vars = {} | |
return | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from commands import Commands | |
class Interpreter: | |
def __init__(self): | |
self.commands = Commands() | |
self.vars = {} | |
def interpret(self, expression): | |
matching_command = self.commands.match(expression) | |
if matching_command: | |
return matching_command.execute(self) | |
try: | |
return eval(expression, {}, self.vars) | |
except NameError: | |
return 'null' | |
except SyntaxError: | |
l_value, r_value = expression.split('=') | |
try: | |
self.vars[l_value] = int(r_value) | |
except ValueError: | |
self.vars[l_value] = self.interpret(r_value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Command(object): | |
def matches(self, expression): | |
if hasattr(self.aliases, '__iter__'): | |
return expression in self.aliases | |
return expression == self.aliases | |
class Clean(Command): | |
aliases = 'clean' | |
def execute(self, interpreter): | |
interpreter.vars = {} | |
class Variables(Command): | |
aliases = ('variables', 'vars', ) | |
def execute(self, interpreter): | |
variable_str = '' | |
for name, value in interpreter.vars.items(): | |
if isinstance(value, str): | |
value_str = "'" + value + "'" | |
else: | |
value_str = str(value) | |
variable_str += '\n' + name + '=' + value_str | |
if variable_str: | |
return 'Stored variables:' + variable_str | |
return 'No stored variables' | |
class Commands: | |
_commands = (Clean(), Variables(), ) | |
def match(self, expression): | |
for command in self._commands: | |
if command.matches(expression): | |
return command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
class TestHelp(TestInterpreter): | |
def test_just_help(self): | |
assert self.interpret('help') == 'clean - Cleans up stored' + \ | |
' variables\nvariables, vars - Shows stored variables' | |
def test_specific_help(self): | |
assert self.interpret('help clean') == 'Cleans up stored variables' | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Command(object): | |
def matches(self, expression): | |
if hasattr(self.aliases, '__iter__'): | |
return expression in self.aliases | |
return expression == self.aliases | |
class Clean(Command): | |
aliases = 'clean' | |
description = 'Cleans up stored variables' | |
def execute(self, interpreter): | |
interpreter.vars = {} | |
class Help(Command): | |
aliases = 'help' | |
def matches(self, expression): | |
parts = expression.split() | |
if parts[0] == 'help': | |
if len(parts) > 1: | |
self.target = parts[1] | |
else: | |
self.target = None | |
return True | |
def execute(self, interpreter): | |
ret = '' | |
if self.target: | |
target_command = interpreter.commands.match(self.target) | |
return target_command.description | |
else: | |
for command in interpreter.commands: | |
if hasattr(command, 'description'): | |
if hasattr(command.aliases, '__iter__'): | |
ret += ', '.join(command.aliases) | |
else: | |
ret += command.aliases | |
ret += ' - ' + command.description + '\n' | |
return ret.rstrip() | |
class Variables(Command): | |
aliases = ('variables', 'vars', ) | |
description = 'Shows stored variables' | |
def execute(self, interpreter): | |
variable_str = '' | |
for name, value in interpreter.vars.items(): | |
if isinstance(value, str): | |
value_str = "'" + value + "'" | |
else: | |
value_str = str(value) | |
variable_str += '\n' + name + '=' + value_str | |
if variable_str: | |
return 'Stored variables:' + variable_str | |
return 'No stored variables' | |
class Commands(list): | |
def __init__(self): | |
commands = (Clean(), Help(), Variables(), ) | |
super(Commands, self).__init__(commands) | |
def match(self, expression): | |
for command in self: | |
if command.matches(expression): | |
return command |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment