.. _firstnotebookrst: ============== First notebook ============== .. only:: html **Links:** :download:`notebook `, :downloadlink:`html `, :download:`PDF `, :download:`python `, :downloadlink:`slides `, :githublink:`GitHub|_doc/notebooks/first_notebook.ipynb|*` Short examples on some pieces of the modules. .. code:: ipython3 from jyquickhelper import add_notebook_menu add_notebook_menu() .. contents:: :local: CodeNodeVisitor --------------- .. code:: ipython3 import ast import inspect from pysqllike.translation.node_visitor_translator import CodeNodeVisitor def myjob(input): iter = input.select (input.age, input.nom, age2 = input.age2*input.age2) wher = iter.where( (iter.age > 60).Or(iter.age < 25)) return wher code = inspect.getsource(myjob) node = ast.parse(code) v = CodeNodeVisitor() v.visit(node) for r in v.Rows : print("{0}{1}: {2}".format(" " * r["indent"], r["type"], r["str"])) .. parsed-literal:: Module: FunctionDef: myjob arguments: arg: input Assign: Name: iter Call: select Attribute: input.select Attribute: input.age Attribute: input.nom keyword: age2 BinOp: Attribute: input.age2 Mult: Attribute: input.age2 Assign: Name: wher Call: where Attribute: iter.where Call: Or Attribute: Or Compare: Attribute: iter.age Gt: Num: 60 Compare: Attribute: iter.age Lt: Num: 25 Return: Name: wher Translate into Python --------------------- .. code:: ipython3 from pysqllike.translation.translation_to_python import Translate2Python trans = Translate2Python(myjob) code = trans.Code() print(code) .. parsed-literal:: def myjob(input): iter = [ ] for row in input: _age=row['age'] _nom=row['nom'] _age2=row['age2'] newr = { 'age':_age, 'nom':_nom, 'age2':_age2*_age2, } iter.append(newr) wher = [ ] for row in iter: _age=row['age'] _exp=(_age>60) or (_age<25) if _exp: wher.append(row) return wher