Skip to content
Permalink
Browse files

applies pep8

  • Loading branch information...
sdpython committed Feb 28, 2015
1 parent 4e85207 commit 280e68c1de305f7455e30f8d4ca15ae98e969e0f
@@ -5,19 +5,37 @@
# sphinx-quickstart on Fri May 10 18:35:14 2013.
#

import sys, os, datetime, re
import sys
import os
import datetime
import re
import cloud_sptheme as csp

sys.path.insert(0, os.path.abspath(os.path.join(os.path.split(__file__)[0])))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.split(__file__)[0], "pysqllike")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.split(__file__)[0], "..", "..", "..", "..", "pyquickhelper", "src")))
sys.path.insert(
0,
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"pysqllike")))
sys.path.insert(
0,
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"..",
"pyquickhelper",
"src")))

from pyquickhelper.helpgen.default_conf import set_sphinx_variables
set_sphinx_variables( __file__,
"pysqllike",
"Xavier Dupré",
2014,
"cloud",
csp.get_theme_dir(),
locals(),
add_extensions = ['cloud_sptheme'])
set_sphinx_variables(__file__,
"pysqllike",
"Xavier Dupré",
2014,
"cloud",
csp.get_theme_dir(),
locals(),
add_extensions=['cloud_sptheme'])
@@ -3,18 +3,30 @@
@brief run all unit tests
"""

import unittest, os, sys, io
import unittest
import os
import sys
import io


def main():
try:
import pyquickhelper
except ImportError:
sys.path.append ( os.path.normpath (os.path.abspath(os.path.join( os.path.split(__file__)[0],"..","..","pyquickhelper","src"))))
sys.path.append(
os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"pyquickhelper",
"src"))))
import pyquickhelper

from pyquickhelper import fLOG, run_cmd, main_wrapper_tests
fLOG(OutputPrint = True)
fLOG(OutputPrint=True)
main_wrapper_tests(__file__)

if __name__ == "__main__" :
main()
if __name__ == "__main__":
main()
@@ -2,19 +2,38 @@
@brief test log(time=1s)
"""

import sys, os, unittest, inspect, ast, inspect, _ast

try :
import sys
import os
import unittest
import inspect
import ast
import inspect
import _ast

try:
import src
except ImportError :
path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", "..")))
if path not in sys.path : sys.path.append (path)
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src

try :
try:
import pyquickhelper
except ImportError :
path = os.path.abspath(os.path.join(os.path.split(__file__)[0], "..", "..", "..", "pyquickhelper", "src"))
except ImportError:
path = os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src"))
sys.path.append(path)
import pyquickhelper

@@ -28,63 +47,79 @@


def myjob(input):
iter = input.select (input.age, input.nom, age2 = input.age*input.age)
wher = iter.where( (iter.age > 60).Or(iter.age < 25))
iter = input.select(input.age, input.nom, age2=input.age * input.age)
wher = iter.where((iter.age > 60).Or(iter.age < 25))
return wher


class TestCode (unittest.TestCase):

def test_tree_job(self) :
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
def test_tree_job(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
code = inspect.getsource(myjob)
node = ast.parse(code)
inode = 0
stack = [(0,node)]
stack = [(0, node)]

while len(stack) > 0 :
ind,n = stack[-1]
while len(stack) > 0:
ind, n = stack[-1]
del stack[-1]
att = { name:ch for name,ch in ast.iter_fields(n) }
fLOG (" " * ind,type(n), att.get("name","--"), att)
for ch in ast.iter_child_nodes(n) :
stack.append( (ind+1,ch) )
att = {name: ch for name, ch in ast.iter_fields(n)}
fLOG(" " * ind, type(n), att.get("name", "--"), att)
for ch in ast.iter_child_nodes(n):
stack.append((ind + 1, ch))

def test_translation(self):
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
code = inspect.getsource(myjob)
node = ast.parse(code)
v = CodeNodeVisitor()
v.visit(node)
assert len(v.Rows)>=27
assert len(v.Rows) >= 27

def test_translate_class(self):
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
trans = TranslateClass(myjob)
#fLOG(trans)
# fLOG(trans)
s = str(trans)
trad = trans.to_str()
assert "60" in trad
assert "Gt" in trad
assert len(s)>0
assert len(s) > 0
assert "input.age2" not in s
assert "input.age" in s

def test_translate_class_code(self):
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
trans = TranslateClass(myjob)
try :
try:
code = trans.Code()
assert False
except CodeException as e :
except CodeException as e:
assert "not implemented" in str(e)

def test_translate_2_python(self):
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
trans = Translate2Python(myjob)
code = trans.Code()
assert "def myjob(input)" in code
assert "60" in code


if __name__ == "__main__" :
unittest.main ()
if __name__ == "__main__":
unittest.main()
@@ -2,19 +2,38 @@
@brief test log(time=2s)
"""

import sys, os, unittest, inspect, ast, inspect, _ast

try :
import sys
import os
import unittest
import inspect
import ast
import inspect
import _ast

try:
import src
except ImportError :
path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", "..")))
if path not in sys.path : sys.path.append (path)
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src

try :
try:
import pyquickhelper
except ImportError :
path = os.path.abspath(os.path.join(os.path.split(__file__)[0], "..", "..", "..", "pyquickhelper", "src"))
except ImportError:
path = os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src"))
sys.path.append(path)
import pyquickhelper

@@ -29,67 +48,74 @@


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))
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 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))
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


def myjob3(input):
iter = input.groupby(input.year, size = input.ext.len())
iter = input.groupby(input.year, size=input.ext.len())
return iter

data1 = [ {"ext":"pysqllike", "num":3, "year":2014 },
{"ext":"pyquickhelper", "num":1, "year":2013 },
{"ext":"pyensae", "num":2, "year":2013 },
{"ext":"pyrsslocal", "num":3, "year":2014 }
]
data1 = [{"ext": "pysqllike", "num": 3, "year": 2014},
{"ext": "pyquickhelper", "num": 1, "year": 2013},
{"ext": "pyensae", "num": 2, "year": 2013},
{"ext": "pyrsslocal", "num": 3, "year": 2014}
]


class TestCodeCross (unittest.TestCase):

def test_translation(self):
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
data = [ data1 ]
functions = [ myjob1, myjob2, myjob3 ]
translate = [ Translate2Python ]
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
data = [data1]
functions = [myjob1, myjob2, myjob3]
translate = [Translate2Python]

nb = 0
for i,f in enumerate(functions) :
fname = "myjob%d" % (i+1)
for i, f in enumerate(functions):
fname = "myjob%d" % (i + 1)

it = IterRow(None, data1)
it = IterRow(None, data1)
res = f(it)
exp = list(res)

for tr in translate :
for tr in translate:
obj = tr(f)
code = obj.Code()

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

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

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

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

nb += 1
assert nb > 0


if __name__ == "__main__" :
unittest.main ()
if __name__ == "__main__":
unittest.main()
Oops, something went wrong.

0 comments on commit 280e68c

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