Skip to content

Instantly share code, notes, and snippets.

@bebraw

bebraw/clean.py

Created Dec 13, 2009
Embed
What would you like to do?
class Clean:
aliases = 'clean'
description = 'Cleans up stored variables'
def execute(self, variables):
'''
>>> clean = Clean()
Cleaning empty vars should result in empty still
>>> variables = {}
>>> clean.execute(variables)
>>> variables
{}
Cleaning existing variables should result in empty
>>> variables = {'a': 5, }
>>> clean.execute(variables)
>>> variables
{}
'''
for variable in variables.keys():
del variables[variable]
if __name__ == "__main__":
import doctest
doctest.testmod()
class SpecificHelp:
def __init__(self, target_name):
'''
>>> from mock import Mock
>>> clean = Mock()
>>> clean.aliases = 'clean'
>>> clean.description = 'Cleans up stored variables'
>>> commands = Mock()
>>> commands.find.return_value = clean
>>> help = SpecificHelp('clean')
>>> help.execute(commands)
'Cleans up stored variables'
'''
self.target_name = target_name
def execute(self, commands):
target_command = commands.find(self.target_name)
return target_command.description
class Help:
aliases = 'help'
def matches(self, expression):
parts = expression.split()
if parts[0] == 'help':
if len(parts) > 1:
return SpecificHelp(parts[1])
return self
def execute(self, commands):
'''
>>> from mock import Mock
>>> clean = Mock()
>>> clean.aliases = 'clean'
>>> clean.description = 'Cleans up stored variables'
>>> variables = Mock()
>>> variables.aliases = ('variables', 'vars', )
>>> variables.description = 'Shows stored variables'
>>> commands = [clean, variables]
>>> help = Help()
>>> help.execute(commands)
'clean - Cleans up stored variables\\nvariables, vars - Shows stored variables'
'''
ret = ''
for command in 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()
if __name__ == "__main__":
import doctest
doctest.testmod()
class Interpreter:
def __init__(self):
self.vars = {}
def interpret(self, expression):
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)
from placidity.interpreter import Interpreter
from placidity.utils import Operations
class TestInterpreter:
def setup_method(self, method):
interpreter = Interpreter()
self.interpret = interpreter.interpret
class TestSetVariable(TestInterpreter):
def test_set(self):
self.interpret('a=6')
assert self.interpret('a') == 6
def test_set_expression(self):
self.interpret('a=4*3')
assert self.interpret('a') == 12
def test_set_variable(self):
self.interpret('a=8')
self.interpret('b=a')
assert self.interpret('b') == 8
def test_variable_in_expression(self):
self.interpret('a=12')
assert self.interpret('a+3') == 15
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 TestMath(TestInterpreter):
operations = Operations(('1+1', 2), ('5-1', 4), ('3*5', 15), ('12/4', 3), )
def test_operations(self):
for operation in self.operations:
assert self.interpret(operation.expression) == operation.result
class Variables:
aliases = ('variables', 'vars', )
description = 'Shows stored variables'
def execute(self, variables):
'''
>>> variables_command = Variables()
>>> variables = {}
>>> variables_command.execute(variables)
'No stored variables'
>>> variables = {'a': 14, 'b': -4, 'animal': 'boar', }
>>> variables_command.execute(variables)
"Stored variables:\\na=14\\nb=-4\\nanimal='boar'"
'''
variable_str = ''
for name, value in variables.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'
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment