/
if_print.py
176 lines (161 loc) · 6.86 KB
/
if_print.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import SelectScript
import SelectScript.Interpreter
ss = SelectScript.Interpreter()
problem = """# if is like in functional programming, and the last statement within
# the then or else-part defines the return value... every part is segregated
# with a semi-colon
b = IF ( 2<3;
a=1., 2.; # then
a=2., 1.);# else
a/b;
"""
bytecode = SelectScript.compile(problem)
print "----------------------------------------------------------------------------"
print problem
print "----------------------------------------------------------------------------"
print "result:", ss.eval(bytecode)
# result: 0.5
####################################################################################
####################################################################################
problem = """# the opposite way ...
b = IF ( 2>3;
a=1., 2.; # then
a=2., 1.);# else
a/b;
"""
bytecode = SelectScript.compile(problem)
print "----------------------------------------------------------------------------"
print problem
print "----------------------------------------------------------------------------"
print "result:", ss.eval(bytecode)
# result: 2.0
####################################################################################
####################################################################################
problem = """# print is a little helper function that can be placed for logging purposes
# everywhere within a script ... the last statement defines the returning value
print("what", "shall", [1,2,3], "I", "print", False);
"""
bytecode = SelectScript.compile(problem)
print "----------------------------------------------------------------------------"
print problem
print "----------------------------------------------------------------------------"
result = ss.eval(bytecode)
print
print "result:", result
# what shall [1, 2, 3] I print False
#
# result: False
####################################################################################
####################################################################################
# Map
# =============================
# ______________________
# | | |
# | |____________ b |
# | a | | |
# | | c | |
# | |____________|___|
# |_____| |
# | | |
# | e | d |
# |_____|________________|
problem = """# why ifs? counting the positive and negative combinations within WHERE ...
# that do not affect the result of the query ...
positive = 0;
negative = 0;
colors = ['red', 'green', 'blue', 'yellow'];
neighbours = [[a.this, b.this], [a.this, c.this], [a.this, d.this], [a.this, e.this],
[b.this, c.this], [b.this, d.this], [c.this, d.this], [d.this, e.this]];
SELECT a.this, b.this, c.this, d.this, e.this
FROM a=colors, b=colors, c=colors, d=colors, e=colors
WHERE IF( [] == SELECT this
FROM neighbours
WHERE this[0] == this[1]
AS list;
positive = positive + 1, True;
negative = negative + 1, False)
AS dict;
"""
bytecode = SelectScript.compile(problem)
result = ss.eval(bytecode)
print "----------------------------------------------------------------------------"
print problem
print "----------------------------------------------------------------------------"
print "1. results:", result[0]
print "positive results:", ss.callVariable("positive")
print "negative results:", ss.callVariable("negative")
# 1. results: {'a': 'red', 'c': 'blue', 'b': 'green', 'e': 'green', 'd': 'yellow'}
# positive results: 48
# negative results: 976
####################################################################################
####################################################################################
problem = """# logging all results by applying print(..., ..., ...), the last parameter is
# always the return value
counter = 0;
colors = ['red', 'green', 'blue', 'yellow'];
neighbours = [[a.this, b.this], [a.this, c.this], [a.this, d.this], [a.this, e.this],
[b.this, c.this], [b.this, d.this], [c.this, d.this], [d.this, e.this]];
SELECT a.this, b.this, c.this, d.this, e.this
FROM a=colors, b=colors, c=colors, d=colors, e=colors
WHERE print("log:",
counter=counter+1,
a.this, b.this, c.this, d.this, e.this,
[] == SELECT this
FROM neighbours
WHERE this[0] == this[1]
AS list)
AS dict;"""
print "----------------------------------------------------------------------------"
print problem
print "----------------------------------------------------------------------------"
bytecode = SelectScript.compile(problem)
result = ss.eval(bytecode)
# log: 1 red red red red red False
# log: 2 red red red red green False
# log: 3 red red red red blue False
# log: 4 red red red red yellow False
# log: 5 red red red green red False
# log: 6 red red red green green False
# log: 7 red red red green blue False
# log: 8 red red red green yellow False
# log: 9 red red red blue red False
# log: 10 red red red blue green False
# log: 11 red red red blue blue False
# ...
# log: 1024 yellow yellow yellow yellow yellow False
####################################################################################
####################################################################################
problem = """# combining if and print to log only results where "a" is of red color, note that
# the else part is not alway required ...
colors = ['red', 'green', 'blue', 'yellow'];
neighbours = [[a.this, b.this], [a.this, c.this], [a.this, d.this], [a.this, e.this],
[b.this, c.this], [b.this, d.this], [c.this, d.this], [d.this, e.this]];
SELECT a.this, b.this, c.this, d.this, e.this
FROM a=colors, b=colors, c=colors, d=colors, e=colors
WHERE IF( [] == SELECT this
FROM neighbours
WHERE this[0] == this[1]
AS list;
IF( a.this == "red";
print(a.this, b.this, c.this, d.this, e.this)), True)
AS dict;
"""
print "----------------------------------------------------------------------------"
print problem
print "----------------------------------------------------------------------------"
bytecode = SelectScript.compile(problem)
result = ss.eval(bytecode)
# red green blue yellow green
# red green blue yellow blue
# red green yellow blue green
# red green yellow blue yellow
# red blue green yellow green
# red blue green yellow blue
# red blue yellow green blue
# red blue yellow green yellow
# red yellow green blue green
# red yellow green blue yellow
# red yellow blue green blue
# red yellow blue green yellow
####################################################################################
####################################################################################