def test_builder_MakeRefContext(self): from phi import P assert 2 == P.Pipe( Write(s=1), #s = 1 P.Seq( Write(s=P + 1), #s = 2 ), Read('s') # s == 2 )
def test_getting_started(self): from phi import P def add1(x): return x + 1 def mul3(x): return x * 3 x = P.Pipe( 1.0, #input 1 add1, #1 + 1 == 2 mul3 #2 * 3 == 6 ) assert x == 6 ################ ################ from phi import P x = P.Pipe( 1.0, #input 1 P + 1, #1 + 1 == 2 P * 3 #2 * 3 == 6 ) assert x == 6 ################ ################ from phi import P, List [x, y] = P.Pipe( 1.0, #input 1 List( P + 1 #1 + 1 == 2 , P * 3 #1 * 3 == 3 )) assert x == 2 assert y == 3 ################ ################ from phi import P, List [x, y] = P.Pipe( 1.0, #input 1 P * 2, #1 * 2 == 2 List( P + 1 #2 + 1 == 3 , P * 3 #2 * 3 == 6 )) assert x == 3 assert y == 6 ################ ################ from phi import P, Rec result = P.Pipe( 1.0, #input 1 P * 2, #1 * 2 == 2 Dict( x=P + 1 #2 + 1 == 3 , y=P * 3 #2 * 3 == 6 )) assert result.x == 3 assert result.y == 6 ################ ################ from phi import P, Rec result = P.Pipe( 1.0, #input 1 P * 2, #1 * 2 == 2 Dict( x=P + 1 #2 + 1 == 3 , y=P * 3 #2 * 3 == 6 ), Rec.x / Rec.y #3 / 6 == 0.5 ) assert result == 0.5 ################ ################ from phi import P, Rec, List, Write, Read [result, s] = P.Pipe( 1.0, #input 1 Write(s=P * 2), #s = 2 * 1 == 2 Dict( x=P + 1 #2 + 1 == 3 , y=P * 3 #2 * 3 == 6 ), List( Rec.x / Rec.y #3 / 6 == 0.5 , Read('s') #load 's' == 2 )) assert result == 0.5 assert s == 2 ################ ################ from phi import P, Rec, Write, Read, List [result, s] = P.Pipe( 1.0, #input 1 Write(s=P * 2), #s = 2 * 1 == 2 Dict( x=P + 1 #2 + 1 == 3 , y=P * 3 #2 * 3 == 6 ), List( Rec.x / Rec.y #3 / 6 == 0.5 , Read.s + 3 # 2 + 3 == 5 )) assert result == 0.5 assert s == 5 ################ ################ from phi import P, Rec, Read, Write [result, s] = P.Pipe( 1.0, #input 1 Write(s=P * 2), #s = 2 * 1 == 2 Dict( x=P + 1 #2 + 1 == 3 , y=P * 3 #2 * 3 == 6 ), List( Rec.x / Rec.y #3 / 6 == 0.5 , Read.s + 3 # 2 + 3 == 5 )) assert result == 0.5 assert s == 5 ################ ################ from phi import P, Rec, Val [result, s, val] = P.Pipe( 1.0, #input 1 Write(s=P * 2), #s = 2 * 1 == 2 Dict( x=P + 1 #2 + 1 == 3 , y=P * 3 #2 * 3 == 6 ), List( Rec.x / Rec.y #3 / 6 == 0.5 , Read.s + 3 # 2 + 3 == 5 , Val(9) + 1 #input 9 and add 1, gives 10 )) assert result == 0.5 assert s == 5 assert val == 10 ######################### ######################### from phi import P, Rec, Read, Write, Val, If [result, s, val] = P.Pipe( 1.0, #input 1 Write(s=(P + 3) / (P + 1)), #s = 4 / 2 == 2 Dict( x=P + 1 #2 + 1 == 3 , y=P * 3 #2 * 3 == 6 ), List( Rec.x / Rec.y #3 / 6 == 0.5 , Read.s + 3 # 2 + 3 == 5 , If( Rec.y > 7, Val(9) + 1 #input 9 and add 1, gives 10 ).Elif(Rec.y < 4, "Yes").Else("Sorry, come back latter."))) assert result == 0.5 assert s == 5 assert val == "Sorry, come back latter." ###################################### ####################################### from phi import P, Rec, Read, Write, Val, If f = P.Seq( Write(s=(P + 3) / (P + 1)), #s = 4 / 2 == 2 Dict( x=P + 1 #2 + 1 == 3 , y=P * 3 #2 * 3 == 6 ), List( Rec.x / Rec.y #3 / 6 == 0.5 , Read.s + 3 # 2 + 3 == 5 , If( Rec.y > 7, Val(9) + 1 #input 9 and add 1, gives 10 ).Else("Sorry, come back latter."))) [result, s, val] = f(1.0) assert result == 0.5 assert s == 5 assert val == "Sorry, come back latter."