Skip to content
Permalink
Browse files

function can now be convert into python code

  • Loading branch information...
sdpython committed Mar 12, 2014
1 parent 4f1eab3 commit bbaaae98145d336a83589d5fde01bd7db11fa8e9
@@ -26,13 +26,21 @@
from src.pysqllike.translation.translation_class import TranslateClass
from src.pysqllike.translation.translation_to_python import Translate2Python
from src.pysqllike.translation.code_exception import CodeException
from src.pysqllike.generic.column_type import CFT


def myjob1(input):
iter = input.select(input.ext, input.num, num2 = input.num*input.num)
wher = iter.where( (iter.num2 < 8).And(iter.num2 > 1))
return wher

def cube(x) : return x*x*x

def myjob2(input):
iter = input.select(input.ext, input.num, num2 = input.num*input.num)
sele = iter.select(iter.ext, iter.num, iter.num2, num3 = CFT(cube, iter.num))
return sele

data1 = [ {"ext":"pysqllike", "num":3 },
{"ext":"pyquickhelper", "num":1 },
{"ext":"pyensae", "num":2 },
@@ -44,7 +52,7 @@ class TestCodeCross (unittest.TestCase):
def test_translation(self):
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
data = [ data1 ]
functions = [ myjob1 ]
functions = [ myjob1, myjob2 ]
translate = [ Translate2Python ]

nb = 0
@@ -58,13 +66,22 @@ def test_translation(self):
for tr in translate :
obj = tr(f)
code = obj.Code()
co = exec(code)

if i == len(functions)-1 :
fLOG("\n"+code)

try:
co = exec(code)
except Exception as e :
raise Exception("unable to compile code\n" + code) from e

try :
exe = eval("%s(it)" % fname)
except Exception as e :
raise Exception("\n" + code) from e
raise Exception("unable to execute\n" + code) from e

if i == len(functions)-1 :
fLOG("\nEXE:\n",exe, "\nEXP:\n", exp)
assert exe == exp

nb += 1
@@ -116,6 +116,16 @@ def print_node(node):
r.append( "{0}={1}".format(att, str(node.__dict__[att])))
return " ".join(r)

def print_tree(self):
"""
display the tree of instruction
@return string
"""
rows = [ ]
for r in self.Rows :
rows.append( ("{0}{1}: {2}".format(" " * r["indent"], r["type"], r["str"])) )
return "\n".join(rows)

@property
def Rows(self):
"""
@@ -170,8 +180,12 @@ def visit_Store(self, node):
return self.generic_visit(node, cont)

def visit_Call(self, node):
cont = { "indent":self._indent, "type": "Call", "str": node.func.attr ,
"node":node, "func":node.func }
if "attr" in node.func.__dict__ :
cont = { "indent":self._indent, "type": "Call", "str": node.func.attr ,
"node":node, "func":node.func }
else :
cont = { "indent":self._indent, "type": "Call", "str": node.func.id ,
"node":node, "func":node.func }
self.push(cont)
return self.generic_visit(node, cont)

@@ -300,6 +300,29 @@ def ResolveExpression(self, node, prefixAtt):
expre.append("({0})".format(ex))
field.update(fi)
funcs.update(funcs)

elif node["str"] == "CFT" :
# we need to look further as CFT is a way to call a function
funcName = None
subexp = [ ]
atts = [ ]
for chil in node["children"]:
if chil["type"] == "Attribute" :
chil["processed"]=True
ex,fi,fu = self.ResolveExpression(chil, prefixAtt)
subexp.append(ex)
field.update(fi)
funcs.update(funcs)
elif chil["type"] == "Name" and chil["str"] == "CFT":
pass
elif chil["type"] == "Name":
# we call function chil["str"]
funcName = chil["str"]
funcs [ chil["str"] ] = chil["str"]
else :
self.RaiseCodeException("unexpected configuration: " + node["type"])
chil["processed"] = True
expre.append( "{0}({1})".format(funcName, ",".join(subexp)))
else :
self.RaiseCodeException("not implemented for function: " + node["str"])
return " ".join(expre), field, funcs
@@ -58,8 +58,12 @@ def Select(self, name, table, rows):
# it has to be an expression
att0 = r["str"]
exp, fields, functions = self.ResolveExpression( r, "_" )

if len(functions) > 0 :
self.RaiseCodeException("not implemented yet")
# we do nothing here, we assume the function is known
# when it will be called
pass

for att_ in fields:
spl = att_.split(".")
if len(spl) != 2 :

0 comments on commit bbaaae9

Please sign in to comment.
You can’t perform that action at this time.