def test_source():
    A = Symbol('A')
    s = Source('s')
    assert s.name == 's'
    assert str(s.transform) == '1'
    assert str(s) == 's'
    s_q = Source('s', Symbol('Q'))
    assert s_q.name == 's'
    assert str(s_q.transform) == 'Q'
    assert str(s_q) == 'Q * s'

    assert str(s * 1.5) == '1.5 * s'
    assert str(s * A) == 'A * s'
    assert str(s_q * A) == '(Q * A) * s'

    assert str(1.5 * s) == '1.5 * s'
    assert str(A * s) == 'A * s'
    assert str(A * s_q) == '(Q * A) * s'

    assert str(-s) == '-1 * s'
    assert str(-(-s)) == 's'
    assert str(-s_q) == '-Q * s'

    assert str(s + s_q) == 's + Q * s'

    with pytest.raises(TypeError):
        s * "A"
Beispiel #2
0
def test_source():
    A = Symbol("A")
    s = Source("s")
    assert s.name == "s"
    assert str(s.transform) == "1"
    assert str(s) == "s"
    s_q = Source("s", Symbol("Q"))
    assert s_q.name == "s"
    assert str(s_q.transform) == "Q"
    assert str(s_q) == "Q * s"

    assert str(s * 1.5) == "1.5 * s"
    assert str(s * A) == "A * s"
    assert str(s_q * A) == "(Q * A) * s"

    assert str(1.5 * s) == "1.5 * s"
    assert str(A * s) == "A * s"
    assert str(A * s_q) == "(Q * A) * s"

    assert str(-s) == "-1 * s"
    assert str(-(-s)) == "s"
    assert str(-s_q) == "-Q * s"

    assert str(s + s_q) == "s + Q * s"

    with pytest.raises(TypeError):
        s * "A"
Beispiel #3
0
    def __init__(self, sources, expression):
        self.objects = {}  # the list of known terms

        # make all the module outputs as known terms
        for name in sources:
            self.objects[name] = Source(name)
        # handle the term 'dot(a, b)' to mean DotProduct(a, b)
        self.objects["dot"] = DotProduct

        # use Python's eval to do the parsing of expressions for us
        self.validate_string(expression)
        sanitized_exp = " ".join(expression.split("\n"))
        try:
            self.expression = eval(sanitized_exp, {}, self)
        except NameError as e:
            raise SpaParseError(
                "Unknown module in expression '%s': %s" % (expression, e)
            )
        except TypeError as e:
            raise SpaParseError(
                "Invalid operator in expression '%s': %s" % (expression, e)
            )

        # normalize the result to a summation
        if not isinstance(self.expression, Summation):
            self.expression = Summation([self.expression])
import pytest

from nengo.spa.action_objects import Symbol, Source, DotProduct

A = Symbol('A')
B = Symbol('B')
x = Source('x')
y = Source('y')


def test_symbol():
    A = Symbol('A')
    assert A.symbol == 'A'
    B = Symbol('B')
    assert str(B) == 'B'

    assert str(A + B) == '(A + B)'
    with pytest.raises(TypeError):
        A + "A"

    assert str(A - B) == '(A - B)'
    with pytest.raises(TypeError):
        A - []

    assert str(A * B) == '(A * B)'
    assert str(A * 1) == 'A'
    assert str(1 * A) == 'A'
    assert str(1.0 * A) == 'A'
    assert str(A * 1.0) == 'A'
    assert str(A * 1.5) == '(A * 1.5)'
    assert str(A * 2) == '(A * 2)'
Beispiel #5
0
import pytest

from nengo.exceptions import SpaParseError
from nengo.spa.action_objects import Symbol, Source, DotProduct

A = Symbol("A")
B = Symbol("B")
x = Source("x")
y = Source("y")


def test_symbol():
    A = Symbol("A")
    assert A.symbol == "A"
    B = Symbol("B")
    assert str(B) == "B"

    assert str(A + B) == "(A + B)"
    with pytest.raises(TypeError):
        A + "A"

    assert str(A - B) == "(A - B)"
    with pytest.raises(TypeError):
        A - []

    assert str(A * B) == "(A * B)"
    assert str(A * 1) == "A"
    assert str(1 * A) == "A"
    assert str(1.0 * A) == "A"
    assert str(A * 1.0) == "A"
    assert str(A * 1.5) == "(A * 1.5)"