<s> from __future__ import absolute_import , division , print_function <EOL> import pytest <EOL> import pandas as pd <EOL> from operator import ( add , sub , mul , floordiv , mod , pow , truediv , eq , ne , lt , <EOL> gt , le , ge , getitem ) <EOL> from functools import partial <EOL> from datetime import datetime <EOL> import datashape <EOL> from datashape . predicates import iscollection , isscalar <EOL> from blaze import CSV <EOL> from blaze . expr import ( Symbol , projection , Field , selection , Broadcast , <EOL> join , cos , by , exp , distinct , Apply , <EOL> broadcast , eval_str , merge , common_subexpression , sum , <EOL> Label , ReLabel , Head , Sort , any , summary , <EOL> Summary , count , symbol , Field , discover , <EOL> max , min , label , Symbol , transform <EOL> ) <EOL> from blaze . compatibility import PY3 , builtins <EOL> from blaze . utils import raises , tmpfile <EOL> from datashape import dshape , var , int32 , int64 , Record , DataShape <EOL> from toolz import identity , first <EOL> import numpy as np <EOL> def test_dshape ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t . dshape == dshape ( '<STR_LIT>' ) <EOL> def test_length ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = Symbol ( '<STR_LIT:s>' , '<STR_LIT>' ) <EOL> assert t . dshape == dshape ( '<STR_LIT>' ) <EOL> assert len ( t ) == <NUM_LIT:10> <EOL> assert len ( t . name ) == <NUM_LIT:10> <EOL> assert len ( t [ [ '<STR_LIT:name>' ] ] ) == <NUM_LIT:10> <EOL> assert len ( t . sort ( '<STR_LIT:name>' ) ) == <NUM_LIT:10> <EOL> assert len ( t . head ( <NUM_LIT:5> ) ) == <NUM_LIT:5> <EOL> assert len ( t . head ( <NUM_LIT:50> ) ) == <NUM_LIT:10> <EOL> with pytest . raises ( ValueError ) : <EOL> len ( s ) <EOL> def test_symbol_eq ( ) : <EOL> assert not ( Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) == <EOL> Symbol ( '<STR_LIT:v>' , '<STR_LIT>' ) ) <EOL> def test_symbol_name ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> r = Symbol ( '<STR_LIT:r>' , '<STR_LIT>' ) <EOL> with pytest . raises ( AttributeError ) : <EOL> t . name <EOL> with pytest . raises ( AttributeError ) : <EOL> r . name <EOL> def test_shape ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t . shape <EOL> assert isinstance ( t . shape , tuple ) <EOL> assert len ( t . shape ) == <NUM_LIT:1> <EOL> def test_eq ( ) : <EOL> assert Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) . isidentical ( <EOL> Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) ) <EOL> assert not Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) . isidentical ( <EOL> Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) ) <EOL> def test_arithmetic ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> x , y , z = t [ '<STR_LIT:x>' ] , t [ '<STR_LIT:y>' ] , t [ '<STR_LIT:z>' ] <EOL> exprs = [ x + <NUM_LIT:1> , x + y , <NUM_LIT:1> + y , <EOL> x - y , <NUM_LIT:1> - x , x - <NUM_LIT:1> , <EOL> x ** y , x ** <NUM_LIT:2> , <NUM_LIT:2> ** x , <EOL> x * y , x ** <NUM_LIT:2> , <NUM_LIT:2> ** x , <EOL> x / y , x / <NUM_LIT:2> , <NUM_LIT:2> / x , <EOL> x % y , x % <NUM_LIT:2> , <NUM_LIT:2> % x ] <EOL> def test_column ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t . fields == [ '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> assert eval ( str ( t . name ) ) == t . name <EOL> assert str ( t . name ) == "<STR_LIT>" <EOL> with pytest . raises ( AttributeError ) : <EOL> t . name . balance <EOL> with pytest . raises ( ( NotImplementedError , ValueError ) ) : <EOL> getitem ( t , set ( '<STR_LIT>' ) ) <EOL> def test_symbol_projection_failures ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) : <EOL> t . _project ( [ '<STR_LIT:name>' , '<STR_LIT:id>' ] ) <EOL> with pytest . raises ( AttributeError ) : <EOL> t . foo <EOL> with pytest . raises ( TypeError ) : <EOL> t . _project ( t . dshape ) <EOL> def test_Projection ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> p = projection ( t , [ '<STR_LIT>' , '<STR_LIT:name>' ] ) <EOL> assert p . schema == dshape ( '<STR_LIT>' ) <EOL> print ( t [ '<STR_LIT>' ] . dshape ) <EOL> print ( dshape ( '<STR_LIT>' ) ) <EOL> assert t [ '<STR_LIT>' ] . dshape == dshape ( '<STR_LIT>' ) <EOL> assert t [ '<STR_LIT>' ] . _name == '<STR_LIT>' <EOL> assert eval ( str ( p ) ) . isidentical ( p ) <EOL> assert p . _project ( [ '<STR_LIT>' , '<STR_LIT:name>' ] ) == p [ [ '<STR_LIT>' , '<STR_LIT:name>' ] ] <EOL> with pytest . raises ( ValueError ) : <EOL> p . _project ( '<STR_LIT>' ) <EOL> def test_Projection_retains_shape ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t [ [ '<STR_LIT:name>' , '<STR_LIT>' ] ] . dshape == dshape ( '<STR_LIT>' ) <EOL> def test_indexing ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t [ [ '<STR_LIT>' , '<STR_LIT:id>' ] ] == projection ( t , [ '<STR_LIT>' , '<STR_LIT:id>' ] ) <EOL> assert t [ '<STR_LIT>' ] . isidentical ( Field ( t , '<STR_LIT>' ) ) <EOL> def test_relational ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> r = ( t [ '<STR_LIT:name>' ] == '<STR_LIT>' ) <EOL> assert '<STR_LIT:bool>' in str ( r . dshape ) <EOL> assert r . _name <EOL> def test_selection ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = selection ( t , t [ '<STR_LIT:name>' ] == '<STR_LIT>' ) <EOL> f = selection ( t , t [ '<STR_LIT:id>' ] > t [ '<STR_LIT>' ] ) <EOL> p = t [ t [ '<STR_LIT>' ] > <NUM_LIT:100> ] <EOL> with pytest . raises ( ValueError ) : <EOL> selection ( t , p ) <EOL> assert s . dshape == t . dshape <EOL> def test_selection_typecheck ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert raises ( TypeError , lambda : t [ t [ '<STR_LIT>' ] + t [ '<STR_LIT:id>' ] ] ) <EOL> assert raises ( TypeError , lambda : t [ t [ '<STR_LIT:name>' ] ] ) <EOL> def test_selection_by_indexing ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> result = t [ t [ '<STR_LIT:name>' ] == '<STR_LIT>' ] <EOL> assert t . schema == result . schema <EOL> assert '<STR_LIT>' in str ( result ) <EOL> def test_selection_by_getattr ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> result = t [ t . name == '<STR_LIT>' ] <EOL> assert t . schema == result . schema <EOL> assert '<STR_LIT>' in str ( result ) <EOL> def test_selection_path_check ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> t2 = t [ t . name == '<STR_LIT>' ] <EOL> t3 = t2 [ t2 . amount > <NUM_LIT:0> ] <EOL> def test_path_issue ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , "<STR_LIT>" ) <EOL> t2 = transform ( t , sizes = t . result . map ( lambda x : ( x - MIN ) * <NUM_LIT:10> / ( MAX - MIN ) , <EOL> schema = '<STR_LIT>' , name = '<STR_LIT:size>' ) ) <EOL> assert builtins . any ( t2 . sizes . isidentical ( node ) for node in t2 . children ) <EOL> def test_getattr_doesnt_override_properties ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert callable ( t . _subs ) <EOL> assert isinstance ( t . schema , DataShape ) <EOL> def test_dir_contains_columns ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> result = dir ( t ) <EOL> columns_set = set ( t . fields ) <EOL> assert set ( result ) & columns_set == columns_set <EOL> def test_selection_consistent_children ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> expr = t [ '<STR_LIT:name>' ] [ t [ '<STR_LIT>' ] < <NUM_LIT:0> ] <EOL> assert list ( expr . fields ) == [ '<STR_LIT:name>' ] <EOL> def test_str ( ) : <EOL> import re <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> expr = t [ t [ '<STR_LIT>' ] < <NUM_LIT:0> ] [ '<STR_LIT:id>' ] * <NUM_LIT:2> <EOL> assert '<STR_LIT>' not in str ( expr ) <EOL> assert not re . search ( '<STR_LIT>' , str ( expr ) ) <EOL> assert eval ( str ( expr ) ) == expr <EOL> assert '<STR_LIT:*>' in str ( expr ) <EOL> def test_join ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> r = Symbol ( '<STR_LIT:r>' , '<STR_LIT>' ) <EOL> q = Symbol ( '<STR_LIT:q>' , '<STR_LIT>' ) <EOL> j = join ( t , s , '<STR_LIT:name>' , '<STR_LIT:name>' ) <EOL> assert j . schema == dshape ( '<STR_LIT>' ) <EOL> assert join ( t , s , '<STR_LIT:name>' ) == join ( t , s , '<STR_LIT:name>' ) <EOL> assert join ( t , s , '<STR_LIT:name>' ) . on_left == '<STR_LIT:name>' <EOL> assert join ( t , s , '<STR_LIT:name>' ) . on_right == '<STR_LIT:name>' <EOL> assert join ( t , r , ( '<STR_LIT:name>' , '<STR_LIT>' ) ) . on_left == [ '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> with pytest . raises ( TypeError ) : <EOL> join ( t , q , '<STR_LIT:name>' ) <EOL> with pytest . raises ( ValueError ) : <EOL> join ( t , s , how = '<STR_LIT>' ) <EOL> def test_join_different_on_right_left_columns ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> j = join ( t , s , '<STR_LIT:x>' , '<STR_LIT:a>' ) <EOL> assert j . on_left == '<STR_LIT:x>' <EOL> assert j . on_right == '<STR_LIT:a>' <EOL> def test_joined_column_first_in_schema ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = Symbol ( '<STR_LIT:s>' , '<STR_LIT>' ) <EOL> assert join ( t , s ) . schema == dshape ( '<STR_LIT>' ) <EOL> def test_outer_join ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> jleft = join ( t , s , '<STR_LIT:name>' , '<STR_LIT:name>' , how = '<STR_LIT:left>' ) <EOL> jright = join ( t , s , '<STR_LIT:name>' , '<STR_LIT:name>' , how = '<STR_LIT:right>' ) <EOL> jinner = join ( t , s , '<STR_LIT:name>' , '<STR_LIT:name>' , how = '<STR_LIT>' ) <EOL> jouter = join ( t , s , '<STR_LIT:name>' , '<STR_LIT:name>' , how = '<STR_LIT>' ) <EOL> js = [ jleft , jright , jinner , jouter ] <EOL> assert len ( set ( js ) ) == <NUM_LIT:4> <EOL> assert jinner . schema == dshape ( '<STR_LIT>' ) <EOL> assert jleft . schema == dshape ( '<STR_LIT>' ) <EOL> assert jright . schema == dshape ( '<STR_LIT>' ) <EOL> assert jouter . schema == dshape ( '<STR_LIT>' ) <EOL> assert join ( t , s , '<STR_LIT:name>' , '<STR_LIT:name>' , how = '<STR_LIT>' ) == join ( t , s , '<STR_LIT:name>' , '<STR_LIT:name>' ) <EOL> def test_join_default_shared_columns ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert join ( t , s ) == join ( t , s , '<STR_LIT:name>' , '<STR_LIT:name>' ) <EOL> def test_multi_column_join ( ) : <EOL> a = Symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = Symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> j = join ( a , b , [ '<STR_LIT:x>' , '<STR_LIT:y>' ] ) <EOL> assert set ( j . fields ) == set ( '<STR_LIT>' ) <EOL> assert j . on_left == j . on_right == [ '<STR_LIT:x>' , '<STR_LIT:y>' ] <EOL> assert hash ( j ) <EOL> assert j . fields == [ '<STR_LIT:x>' , '<STR_LIT:y>' , '<STR_LIT:z>' , '<STR_LIT:w>' ] <EOL> def test_traverse ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t in list ( t . _traverse ( ) ) <EOL> expr = t . amount . sum ( ) <EOL> trav = list ( expr . _traverse ( ) ) <EOL> assert builtins . any ( t . amount . isidentical ( x ) for x in trav ) <EOL> def test_unary_ops ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> expr = cos ( exp ( t [ '<STR_LIT>' ] ) ) <EOL> assert '<STR_LIT>' in str ( expr ) <EOL> assert '<STR_LIT>' in str ( ~ ( t . amount > <NUM_LIT:0> ) ) <EOL> def test_reduction ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> r = sum ( t [ '<STR_LIT>' ] ) <EOL> assert r . dshape in ( dshape ( '<STR_LIT>' ) , <EOL> dshape ( '<STR_LIT>' ) , <EOL> dshape ( '<STR_LIT>' ) ) <EOL> assert '<STR_LIT>' not in str ( t . count ( ) . dshape ) <EOL> assert t . count ( ) . dshape [ <NUM_LIT:0> ] in ( int32 , int64 ) <EOL> assert '<STR_LIT:int>' in str ( t . count ( ) . dshape ) <EOL> assert '<STR_LIT:int>' in str ( t . nunique ( ) . dshape ) <EOL> assert '<STR_LIT:string>' in str ( t [ '<STR_LIT:name>' ] . max ( ) . dshape ) <EOL> assert '<STR_LIT:string>' in str ( t [ '<STR_LIT:name>' ] . min ( ) . dshape ) <EOL> assert '<STR_LIT:string>' not in str ( t . count ( ) . dshape ) <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert '<STR_LIT:int>' in str ( t [ '<STR_LIT:id>' ] . sum ( ) . dshape ) <EOL> assert '<STR_LIT:int>' not in str ( t [ '<STR_LIT>' ] . sum ( ) . dshape ) <EOL> def test_reduction_name ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert ( t . amount + t . id ) . sum ( ) . _name <EOL> def test_max_min_class ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert str ( max ( t ) . dshape ) == '<STR_LIT>' <EOL> assert str ( min ( t ) . dshape ) == '<STR_LIT>' <EOL> @ pytest . fixture <EOL> def symsum ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> return t , t . amount . sum ( ) <EOL> @ pytest . fixture <EOL> def ds ( ) : <EOL> return dshape ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:}>" ) <EOL> def test_discover_dshape_symbol ( ds ) : <EOL> t_ds = Symbol ( '<STR_LIT:t>' , dshape = ds ) <EOL> assert t_ds . fields is not None <EOL> class TestScalarArithmetic ( object ) : <EOL> ops = { '<STR_LIT:+>' : add , '<STR_LIT:->' : sub , '<STR_LIT:*>' : mul , '<STR_LIT:/>' : truediv , '<STR_LIT>' : floordiv , '<STR_LIT:%>' : mod , <EOL> '<STR_LIT>' : pow , '<STR_LIT>' : eq , '<STR_LIT>' : ne , '<STR_LIT:<>' : lt , '<STR_LIT:>>' : gt , '<STR_LIT>' : le , '<STR_LIT>' : ge } <EOL> def test_scalar_arith ( self , symsum ) : <EOL> def runner ( f ) : <EOL> result = f ( r , <NUM_LIT:1> ) <EOL> assert eval ( '<STR_LIT>' % op ) . isidentical ( result ) <EOL> a = f ( r , r ) <EOL> b = eval ( '<STR_LIT>' % op ) <EOL> assert a is b or a . isidentical ( b ) <EOL> result = f ( <NUM_LIT:1> , r ) <EOL> assert eval ( '<STR_LIT>' % op ) . isidentical ( result ) <EOL> t , r = symsum <EOL> r = t . amount . sum ( ) <EOL> for op , f in self . ops . items ( ) : <EOL> runner ( f ) <EOL> def test_scalar_usub ( self , symsum ) : <EOL> t , r = symsum <EOL> result = - r <EOL> assert eval ( str ( result ) ) . isidentical ( result ) <EOL> @ pytest . mark . xfail <EOL> def test_scalar_uadd ( self , symsum ) : <EOL> t , r = symsum <EOL> + r <EOL> def test_summary ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = summary ( total = t . amount . sum ( ) , num = t . id . count ( ) ) <EOL> assert s . dshape == dshape ( '<STR_LIT>' ) <EOL> assert hash ( s ) <EOL> assert eval ( str ( s ) ) . isidentical ( s ) <EOL> assert '<STR_LIT>' in str ( s ) <EOL> assert '<STR_LIT>' in str ( s ) <EOL> assert '<STR_LIT>' in str ( s ) <EOL> assert str ( t . amount . sum ( ) ) in str ( s ) <EOL> assert not summary ( total = t . amount . sum ( ) ) . _child . isidentical ( <EOL> t . amount . sum ( ) ) <EOL> assert iscollection ( summary ( total = t . amount . sum ( ) + <NUM_LIT:1> ) . _child . dshape ) <EOL> def test_reduction_arithmetic ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> expr = t . amount . sum ( ) + <NUM_LIT:1> <EOL> assert eval ( str ( expr ) ) . isidentical ( expr ) <EOL> def test_Distinct ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> r = distinct ( t [ '<STR_LIT:name>' ] ) <EOL> print ( r . dshape ) <EOL> assert r . dshape == dshape ( '<STR_LIT>' ) <EOL> assert r . _name == '<STR_LIT:name>' <EOL> r = t . distinct ( ) <EOL> assert r . dshape == t . dshape <EOL> def test_by ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> r = by ( t [ '<STR_LIT:name>' ] , total = sum ( t [ '<STR_LIT>' ] ) ) <EOL> print ( r . schema ) <EOL> assert isinstance ( r . schema [ <NUM_LIT:0> ] , Record ) <EOL> assert str ( r . schema [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] ) == '<STR_LIT:string>' <EOL> def test_by_summary ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> a = by ( t [ '<STR_LIT:name>' ] , sum = sum ( t [ '<STR_LIT>' ] ) ) <EOL> b = by ( t [ '<STR_LIT:name>' ] , summary ( sum = sum ( t [ '<STR_LIT>' ] ) ) ) <EOL> assert a . isidentical ( b ) <EOL> def test_by_summary_printing ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert str ( by ( t . name , total = sum ( t . amount ) ) ) == '<STR_LIT>' <EOL> def test_by_columns ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert len ( by ( t [ '<STR_LIT:id>' ] , total = t [ '<STR_LIT>' ] . sum ( ) ) . fields ) == <NUM_LIT:2> <EOL> assert len ( by ( t [ '<STR_LIT:id>' ] , count = t [ '<STR_LIT:id>' ] . count ( ) ) . fields ) == <NUM_LIT:2> <EOL> print ( by ( t , count = t . count ( ) ) . fields ) <EOL> assert len ( by ( t , count = t . count ( ) ) . fields ) == <NUM_LIT:4> <EOL> def test_sort ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = t . sort ( '<STR_LIT>' , ascending = True ) <EOL> print ( str ( s ) ) <EOL> assert eval ( str ( s ) ) . isidentical ( s ) <EOL> assert s . schema == t . schema <EOL> assert t [ '<STR_LIT>' ] . sort ( ) . key == '<STR_LIT>' <EOL> def test_head ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = t . head ( <NUM_LIT:10> ) <EOL> assert eval ( str ( s ) ) . isidentical ( s ) <EOL> assert s . schema == t . schema <EOL> def test_label ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> quantity = ( t [ '<STR_LIT>' ] + <NUM_LIT:100> ) . label ( '<STR_LIT>' ) <EOL> assert eval ( str ( quantity ) ) . isidentical ( quantity ) <EOL> assert quantity . fields == [ '<STR_LIT>' ] <EOL> with pytest . raises ( ValueError ) : <EOL> quantity [ '<STR_LIT>' ] <EOL> def test_map_label ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> c = t . amount . map ( identity , schema = '<STR_LIT>' ) <EOL> assert c . label ( '<STR_LIT:bar>' ) . _name == '<STR_LIT:bar>' <EOL> assert c . label ( '<STR_LIT:bar>' ) . _child . isidentical ( c . _child ) <EOL> def test_columns ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert list ( t . fields ) == [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:id>' ] <EOL> assert list ( t [ '<STR_LIT:name>' ] . fields ) == [ '<STR_LIT:name>' ] <EOL> ( t [ '<STR_LIT>' ] + <NUM_LIT:1> ) . fields <EOL> def test_relabel ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> rl = t . relabel ( { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:id>' : '<STR_LIT>' } ) <EOL> rlc = t [ '<STR_LIT>' ] . relabel ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert eval ( str ( rl ) ) . isidentical ( rl ) <EOL> print ( rl . fields ) <EOL> assert rl . fields == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert not isscalar ( rl . dshape . measure ) <EOL> assert isscalar ( rlc . dshape . measure ) <EOL> def test_relabel_join ( ) : <EOL> names = Symbol ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> siblings = join ( names . relabel ( { '<STR_LIT>' : '<STR_LIT:left>' } ) , <EOL> names . relabel ( { '<STR_LIT>' : '<STR_LIT:right>' } ) , '<STR_LIT>' ) <EOL> assert siblings . fields == [ '<STR_LIT>' , '<STR_LIT:left>' , '<STR_LIT:right>' ] <EOL> def test_map ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> inc = lambda x : x + <NUM_LIT:1> <EOL> assert isscalar ( t [ '<STR_LIT>' ] . map ( inc , schema = '<STR_LIT:int>' ) . dshape . measure ) <EOL> s = t [ '<STR_LIT>' ] . map ( inc , schema = '<STR_LIT>' ) <EOL> assert not isscalar ( s . dshape . measure ) <EOL> assert s . dshape == dshape ( '<STR_LIT>' ) <EOL> expr = ( t [ [ '<STR_LIT:name>' , '<STR_LIT>' ] ] <EOL> . map ( identity , schema = '<STR_LIT>' ) ) <EOL> assert expr . _name is None <EOL> @ pytest . mark . xfail ( reason = "<STR_LIT>" ) <EOL> def test_map_without_any_info ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert iscolumn ( t [ '<STR_LIT>' ] . map ( inc , '<STR_LIT:int>' ) ) <EOL> assert not iscolumn ( t [ [ '<STR_LIT:name>' , '<STR_LIT>' ] ] . map ( identity ) ) <EOL> def test_apply ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> s = t [ '<STR_LIT>' ] . apply ( sum , dshape = '<STR_LIT>' ) <EOL> r = t [ '<STR_LIT>' ] . apply ( sum , dshape = '<STR_LIT>' ) <EOL> assert s . dshape == dshape ( '<STR_LIT>' ) <EOL> assert r . schema == dshape ( '<STR_LIT>' ) <EOL> def test_Symbol_printing_is_legible ( ) : <EOL> accounts = Symbol ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> expr = ( exp ( accounts . balance * <NUM_LIT:10> ) ) + accounts [ '<STR_LIT:id>' ] <EOL> assert "<STR_LIT>" in str ( expr ) <EOL> assert "<STR_LIT>" in str ( expr ) <EOL> def test_merge ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> p = Symbol ( '<STR_LIT:p>' , '<STR_LIT>' ) <EOL> accounts = Symbol ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> new_amount = ( accounts . balance * <NUM_LIT> ) . label ( '<STR_LIT>' ) <EOL> c = merge ( accounts [ [ '<STR_LIT:name>' , '<STR_LIT>' ] ] , new_amount ) <EOL> assert c . fields == [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert c . schema == dshape ( '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) : <EOL> merge ( t , t ) <EOL> with pytest . raises ( ValueError ) : <EOL> merge ( t , p ) <EOL> def test_merge_repeats ( ) : <EOL> accounts = Symbol ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) : <EOL> merge ( accounts , ( accounts . balance + <NUM_LIT:1> ) . label ( '<STR_LIT>' ) ) <EOL> def test_merge_project ( ) : <EOL> accounts = Symbol ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> new_amount = ( accounts [ '<STR_LIT>' ] * <NUM_LIT> ) . label ( '<STR_LIT>' ) <EOL> c = merge ( accounts [ [ '<STR_LIT:name>' , '<STR_LIT>' ] ] , new_amount ) <EOL> assert c [ '<STR_LIT>' ] . isidentical ( new_amount ) <EOL> assert c [ '<STR_LIT:name>' ] . isidentical ( accounts [ '<STR_LIT:name>' ] ) <EOL> assert c [ [ '<STR_LIT:name>' , '<STR_LIT>' ] ] . isidentical ( merge ( accounts . name , new_amount ) ) <EOL> inc = lambda x : x + <NUM_LIT:1> <EOL> def test_subterms ( ) : <EOL> a = Symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> assert list ( a . _subterms ( ) ) == [ a ] <EOL> assert set ( a [ '<STR_LIT:x>' ] . _subterms ( ) ) == set ( [ a , a [ '<STR_LIT:x>' ] ] ) <EOL> assert set ( a [ '<STR_LIT:x>' ] . map ( inc , '<STR_LIT:int>' ) . _subterms ( ) ) == set ( [ a , a [ '<STR_LIT:x>' ] , a [ '<STR_LIT:x>' ] . map ( inc , '<STR_LIT:int>' ) ] ) <EOL> assert a in set ( ( a [ '<STR_LIT:x>' ] + <NUM_LIT:1> ) . _subterms ( ) ) <EOL> def test_common_subexpression ( ) : <EOL> a = Symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> assert common_subexpression ( a ) . isidentical ( a ) <EOL> assert common_subexpression ( a , a [ '<STR_LIT:x>' ] ) . isidentical ( a ) <EOL> assert common_subexpression ( a [ '<STR_LIT:y>' ] + <NUM_LIT:1> , a [ '<STR_LIT:x>' ] ) . isidentical ( a ) <EOL> assert common_subexpression ( a [ '<STR_LIT:x>' ] . map ( inc , '<STR_LIT:int>' ) , a [ '<STR_LIT:x>' ] ) . isidentical ( a [ '<STR_LIT:x>' ] ) <EOL> def test_schema_of_complex_interaction ( ) : <EOL> a = Symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> expr = ( a [ '<STR_LIT:x>' ] + a [ '<STR_LIT:y>' ] ) / a [ '<STR_LIT:z>' ] <EOL> assert expr . schema == dshape ( '<STR_LIT>' ) <EOL> expr = expr . label ( '<STR_LIT:foo>' ) <EOL> assert expr . schema == dshape ( '<STR_LIT>' ) <EOL> def iscolumn ( x ) : <EOL> return isscalar ( x . dshape . measure ) <EOL> def test_iscolumn ( ) : <EOL> a = Symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> assert not iscolumn ( a ) <EOL> assert iscolumn ( a [ '<STR_LIT:x>' ] ) <EOL> assert not iscolumn ( a [ [ '<STR_LIT:x>' , '<STR_LIT:y>' ] ] ) <EOL> assert not iscolumn ( a [ [ '<STR_LIT:x>' ] ] ) <EOL> assert iscolumn ( ( a [ '<STR_LIT:x>' ] + a [ '<STR_LIT:y>' ] ) ) <EOL> assert iscolumn ( a [ '<STR_LIT:x>' ] . distinct ( ) ) <EOL> assert not iscolumn ( a [ [ '<STR_LIT:x>' ] ] . distinct ( ) ) <EOL> assert not iscolumn ( by ( a [ '<STR_LIT:x>' ] , total = a [ '<STR_LIT:y>' ] . sum ( ) ) ) <EOL> assert iscolumn ( a [ '<STR_LIT:x>' ] [ a [ '<STR_LIT:x>' ] > <NUM_LIT:1> ] ) <EOL> assert not iscolumn ( a [ [ '<STR_LIT:x>' , '<STR_LIT:y>' ] ] [ a [ '<STR_LIT:x>' ] > <NUM_LIT:1> ] ) <EOL> assert iscolumn ( a [ '<STR_LIT:x>' ] . sort ( ) ) <EOL> assert not iscolumn ( a [ [ '<STR_LIT:x>' , '<STR_LIT:y>' ] ] . sort ( ) ) <EOL> assert iscolumn ( a [ '<STR_LIT:x>' ] . head ( ) ) <EOL> assert not iscolumn ( a [ [ '<STR_LIT:x>' , '<STR_LIT:y>' ] ] . head ( ) ) <EOL> assert iscolumn ( Symbol ( '<STR_LIT:b>' , '<STR_LIT:int>' ) ) <EOL> assert not iscolumn ( Symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) ) <EOL> def test_discover ( ) : <EOL> ds = '<STR_LIT>' <EOL> a = Symbol ( '<STR_LIT:a>' , ds ) <EOL> assert discover ( a ) == dshape ( ds ) <EOL> def test_improper_selection ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert raises ( Exception , lambda : t [ t [ '<STR_LIT:x>' ] > <NUM_LIT:0> ] [ t . sort ( ) [ t [ '<STR_LIT:y>' > <NUM_LIT:0> ] ] ] ) <EOL> def test_serializable ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> import pickle <EOL> t2 = pickle . loads ( pickle . dumps ( t ) ) <EOL> assert t . isidentical ( t2 ) <EOL> s = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> expr = join ( t [ t . amount < <NUM_LIT:0> ] , s ) . sort ( '<STR_LIT:id>' ) . city . head ( ) <EOL> expr2 = pickle . loads ( pickle . dumps ( expr ) ) <EOL> assert expr . isidentical ( expr2 ) <EOL> def test_symbol_coercion ( ) : <EOL> from datetime import date <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert ( t . amount + '<STR_LIT>' ) . rhs == <NUM_LIT:10> <EOL> assert ( t . timestamp < '<STR_LIT>' ) . rhs == date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:1> ) <EOL> def test_isnan ( ) : <EOL> from blaze import isnan <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> for expr in [ t . amount . isnan ( ) , ~ t . amount . isnan ( ) ] : <EOL> assert eval ( str ( expr ) ) . isidentical ( expr ) <EOL> assert iscollection ( t . amount . isnan ( ) . dshape ) <EOL> assert '<STR_LIT:bool>' in str ( t . amount . isnan ( ) . dshape ) <EOL> def test_distinct_name ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t . name . isidentical ( t [ '<STR_LIT:name>' ] ) <EOL> assert t . distinct ( ) . name . isidentical ( t . distinct ( ) [ '<STR_LIT:name>' ] ) <EOL> assert t . id . distinct ( ) . _name == '<STR_LIT:id>' <EOL> assert t . name . _name == '<STR_LIT:name>' <EOL> def test_leaves ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> v = Symbol ( '<STR_LIT:v>' , '<STR_LIT>' ) <EOL> x = symbol ( '<STR_LIT:x>' , '<STR_LIT>' ) <EOL> assert t . _leaves ( ) == [ t ] <EOL> assert t . id . _leaves ( ) == [ t ] <EOL> assert by ( t . name , count = t . id . nunique ( ) ) . _leaves ( ) == [ t ] <EOL> assert join ( t , v ) . _leaves ( ) == [ t , v ] <EOL> assert join ( v , t ) . _leaves ( ) == [ v , t ] <EOL> assert ( x + <NUM_LIT:1> ) . _leaves ( ) == [ x ] <EOL> @ pytest . fixture <EOL> def t ( ) : <EOL> return Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> def funcname ( x , y = '<STR_LIT>' ) : <EOL> if PY3 : <EOL> return '<STR_LIT>' % ( x , y ) <EOL> return '<STR_LIT>' % y <EOL> class TestRepr ( object ) : <EOL> def test_partial_lambda ( self , t ) : <EOL> expr = t . amount . map ( partial ( lambda x , y : x + y , <NUM_LIT:1> ) ) <EOL> s = str ( expr ) <EOL> assert s == ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> funcname ( '<STR_LIT>' ) ) <EOL> def test_lambda ( self , t ) : <EOL> expr = t . amount . map ( lambda x : x ) <EOL> s = str ( expr ) <EOL> assert s == ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> funcname ( '<STR_LIT>' ) ) <EOL> def test_partial ( self , t ) : <EOL> def myfunc ( x , y ) : <EOL> return x + y <EOL> expr = t . amount . map ( partial ( myfunc , <NUM_LIT:1> ) ) <EOL> s = str ( expr ) <EOL> assert s == ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % funcname ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> def test_builtin ( self , t ) : <EOL> expr = t . amount . map ( datetime . fromtimestamp ) <EOL> s = str ( expr ) <EOL> assert s == ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_udf ( self , t ) : <EOL> def myfunc ( x ) : <EOL> return x + <NUM_LIT:1> <EOL> expr = t . amount . map ( myfunc ) <EOL> s = str ( expr ) <EOL> assert s == ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % funcname ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_nested_partial ( self , t ) : <EOL> def myfunc ( x , y , z ) : <EOL> return x + y + z <EOL> f = partial ( partial ( myfunc , <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> expr = t . amount . map ( f ) <EOL> s = str ( expr ) <EOL> assert s == ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> funcname ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_count_values ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t . name . count_values ( sort = False ) . isidentical ( <EOL> by ( t . name , count = t . name . count ( ) ) ) <EOL> assert t . name . count_values ( sort = True ) . isidentical ( <EOL> by ( t . name , count = t . name . count ( ) ) . sort ( '<STR_LIT:count>' , ascending = False ) ) <EOL> def test_dir ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in dir ( t . dt ) <EOL> assert '<STR_LIT>' not in dir ( t . dt ) <EOL> assert '<STR_LIT>' in dir ( t . amount ) <EOL> assert '<STR_LIT>' not in dir ( t [ [ '<STR_LIT>' , '<STR_LIT>' ] ] ) <EOL> assert '<STR_LIT>' not in dir ( t . name ) <EOL> def test_distinct_column ( ) : <EOL> t = Symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t . name . distinct ( ) . name . dshape == t . name . distinct ( ) . dshape <EOL> assert t . name . distinct ( ) . name . isidentical ( t . name . distinct ( ) ) </s>
