def test_scalar_projection(): x = Symbol('x', '{name: string, amount: int64, when: datetime}') assert x[['amount', 'when']].dshape == \ dshape('{amount: int64, when: datetime}')
def test_str(): x = Symbol('x', 'real') assert str(x + 10) == 'x + 10'
def test_scalar_name_dtype(): x = Symbol('x', 'int64') assert x._name == 'x' assert x.dshape == dshape('int64')
def test_scalar_field(): x = Symbol('x', '{name: string, amount: int64, when: datetime}') assert 'amount' in dir(x) assert x.amount.dshape == dshape('int64')
def test_basic(): expr = (x + y) * 3 assert eval(str(expr)).isidentical(expr) assert expr.isidentical(Mult(Add(Symbol('x', 'real'), Symbol('y', 'real')), 3))
class TestExprify(object): dtypes = {'x': 'int', 'y': 'real', 'z': 'int32'} x = Symbol('x', 'int') y = Symbol('y', 'real') z = Symbol('z', 'int32') name = Symbol('name', 'string') def test_basic_arithmetic(self): assert exprify('x + y', self.dtypes).isidentical(self.x + self.y) other = isnan(sin(x) + y) assert exprify('isnan(sin(x) + y)', self.dtypes).isidentical(other) # parsed as a Num in Python 2 and a UnaryOp in Python 3 assert exprify('-1', {}) == -1 # parsed as UnaryOp(op=USub(), operand=1) assert exprify('-x', self.dtypes).isidentical(-self.x) assert exprify('-x + y', self.dtypes).isidentical(-self.x + self.y) other = self.x * self.y + self.z assert exprify('x * y + z', self.dtypes).isidentical(other) assert exprify('x ** y', self.dtypes).isidentical(self.x ** self.y) other = self.x / self.y / self.z + 1 assert exprify('x / y / z + 1', self.dtypes).isidentical(other) other = self.x / self.y % self.z + 2 ** self.y assert exprify('x / y % z + 2 ** y', self.dtypes).isidentical(other) assert exprify('x // y', self.dtypes).isidentical(self.x // self.y) assert exprify('1 // y // x', self.dtypes).isidentical( 1 // self.y // self.x) def test_comparison(self): other = (self.x == 1) | (self.x == 2) assert exprify('(x == 1) | (x == 2)', self.dtypes).isidentical(other) def test_simple_boolean_not(self): other = ~self.x assert exprify('~x', {'x': 'bool'}).isidentical(other) def test_literal_string_compare(self): other = self.name == "Alice" result = exprify('name == "Alice"', {'name': 'string'}) assert isinstance(result.rhs, basestring) assert result.isidentical(other) def test_literal_int_compare(self): other = self.x == 1 result = exprify('x == 1', self.dtypes) assert isinstance(result.rhs, int) assert result.isidentical(other) def test_literal_float_compare(self): other = self.y == 1.0 result = exprify('y == 1.0', self.dtypes) assert isinstance(result.rhs, float) assert result.isidentical(other) def test_failing_exprify(self): dtypes = self.dtypes with raises(AssertionError): exprify('x < y < z', dtypes) with raises(NotImplementedError): exprify('os.listdir()', {}) with raises(NotImplementedError): exprify('os.listdir()', {'os': 'int', 'os.listdir': 'real'}) with raises(ValueError): exprify('__x + __y', {'__x': 'int', '__y': 'real'}) with raises(NotImplementedError): exprify('y if x else y', dtypes) def test_functiondef_fail(self): dtypes = self.dtypes with raises(NotImplementedError): exprify('lambda x, y: x + y', dtypes) with raises(SyntaxError): exprify('def f(x): return x', dtypes={'x': 'int'}) def test_comprehensions_fail(self): dtypes = self.dtypes if sys.version_info < (2, 7): # dict and set comprehensions are not implemented in Python < 2.7 error = SyntaxError else: # and we don't allow them in versions that do error = NotImplementedError with raises(error): exprify('{x: y for z in y}', dtypes) with raises(error): exprify('{x for z in y}', dtypes) with raises(NotImplementedError): exprify('[x for z in y]', dtypes) with raises(NotImplementedError): exprify('(x for y in z)', dtypes) def test_boolop_fails(self): dtypes = self.dtypes with raises(NotImplementedError): exprify('x or y', dtypes) with raises(NotImplementedError): exprify('x and y', dtypes) with raises(NotImplementedError): exprify('not x', dtypes) def test_scope(self): dtypes = {'sin': 'int'} with raises(ValueError): exprify('sin + 1', dtypes) with raises(TypeError): sin + 1
def test_datetime_coercion(): d = Symbol('d', 'datetime') expr = d > '2012-01-01T12:30:00' assert isinstance(expr.rhs, datetime)
from __future__ import absolute_import, division, print_function import sys from blaze.expr.arithmetic import (scalar_coerce, Mult, Add, dshape) from blaze.expr.math import sin, cos, isnan, exp, log from blaze.expr import Symbol, eval_str, exprify from blaze.compatibility import xfail, basestring, raises from datetime import date, datetime x = Symbol('x', 'real') y = Symbol('y', 'real') def test_basic(): expr = (x + y) * 3 assert eval(str(expr)).isidentical(expr) assert expr.isidentical(Mult(Add(Symbol('x', 'real'), Symbol('y', 'real')), 3)) def test_eval_str(): expr = (x + y) * 3 assert eval_str(expr) == '(x + y) * 3' assert eval_str(1) == '1' assert eval_str('Alice') == "'Alice'" assert "'Alice'" in eval_str(u'Alice') print(eval_str(-x)) assert eval_str(-x) == '-x'
yield coll coll.drop() @pytest.yield_fixture def events(db): data = [{'time': datetime(2012, 1, 1, 12, 00, 00), 'x': 1}, {'time': datetime(2012, 1, 2, 12, 00, 00), 'x': 2}, {'time': datetime(2012, 1, 3, 12, 00, 00), 'x': 3}] coll = db.tmp_collection coll = into(coll, data) yield coll coll.drop() t = Symbol('t', 'var * {name: string, amount: int}') bigt = Symbol('bigt', 'var * {name: string, amount: int, city: string}') p = Symbol('p', 'var * {x: int, y: int, z: int}') e = Symbol('e', 'var * {time: datetime, x: int}') q = MongoQuery('fake', []) def test_symbol_one(bank): assert compute_up(t, bank) == MongoQuery(bank, ()) def test_symbol(bank): assert compute(t, bank) == list(pluck(['name', 'amount'], bank_raw))