Repository: zeppelin
Updated Branches:
  refs/heads/master cf327f8e1 -> a922fd28c


Small refactoring of Python interpreter

### What is this PR for?
Small refactoring of Python interpreter, that is what it is.

### What type of PR is it?
Refactoring

### Todos
* [x] refactor `help()`
* [x] impl `maxResult` fetch from JVM

### How should this be tested?
`cd python && mvn -Dpython.test.exclude='' test ` pass (given that `pip install 
pandasql` and `pip install py4j`)

### Questions:
* Does the licenses files need update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No

Author: Alexander Bezzubov <[email protected]>

Closes #1275 from bzz/python/refactoring and squashes the following commits:

15a35c8 [Alexander Bezzubov] Make .help() method a single string literal
e800fd7 [Alexander Bezzubov] Make Python fetch maxResults from JVM


Project: http://git-wip-us.apache.org/repos/asf/zeppelin/repo
Commit: http://git-wip-us.apache.org/repos/asf/zeppelin/commit/a922fd28
Tree: http://git-wip-us.apache.org/repos/asf/zeppelin/tree/a922fd28
Diff: http://git-wip-us.apache.org/repos/asf/zeppelin/diff/a922fd28

Branch: refs/heads/master
Commit: a922fd28c1163abfea8cc86225bd94360731f0ca
Parents: cf327f8
Author: Alexander Bezzubov <[email protected]>
Authored: Wed Aug 3 17:07:54 2016 +0900
Committer: Alexander Bezzubov <[email protected]>
Committed: Fri Aug 5 12:09:35 2016 +0900

----------------------------------------------------------------------
 python/src/main/resources/bootstrap.py          | 151 ++++++++++---------
 python/src/main/resources/bootstrap_input.py    |   6 +-
 ...ythonInterpreterWithPythonInstalledTest.java |  19 ++-
 3 files changed, 97 insertions(+), 79 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zeppelin/blob/a922fd28/python/src/main/resources/bootstrap.py
----------------------------------------------------------------------
diff --git a/python/src/main/resources/bootstrap.py 
b/python/src/main/resources/bootstrap.py
index 0290c5f..09e51e3 100644
--- a/python/src/main/resources/bootstrap.py
+++ b/python/src/main/resources/bootstrap.py
@@ -33,76 +33,78 @@ def intHandler(signum, frame):  # Set the signal handler
 
 signal.signal(signal.SIGINT, intHandler)
 
-
 def help():
-    print ('%html')
-    print ('<h2>Python Interpreter help</h2>')
-    print ('<h3>Python 2 & 3 compatibility</h3>')
-    print ('<p>The interpreter is compatible with Python 2 & 3.<br/>')
-    print ('To change Python version, ')
-    print ('change in the interpreter configuration the python to the ')
-    print ('desired version (example : python=/usr/bin/python3)</p>')
-    print ('<h3>Python modules</h3>')
-    print ('<p>The interpreter can use all modules already installed ')
-    print ('(with pip, easy_install, etc)</p>')
-    print ('<h3>Forms</h3>')
-    print ('You must install py4j in order to use '
-           'the form feature (pip install py4j)')
-    print ('<h4>Input form</h4>')
-    print ('<pre>print (z.input("f1","defaultValue"))</pre>')
-    print ('<h4>Selection form</h4>')
-    print ('<pre>print(z.select("f2", [("o1","1"), ("o2","2")],2))</pre>')
-    print ('<h4>Checkbox form</h4>')
-    print ('<pre> print("".join(z.checkbox("f3", [("o1","1"), '
-           '("o2","2")],["1"])))</pre>')
-    print ('<h3>Matplotlib graph</h3>')
-    print ('<div>The interpreter can display matplotlib graph with ')
-    print ('the function z.show()</div>')
-    print ('<div> You need to already have matplotlib module installed ')
-    print ('to use this functionality !</div><br/>')
-    print ('''<pre>import matplotlib.pyplot as plt
-plt.figure()
-(.. ..)
-z.show(plt)
-plt.close()
-</pre>''')
-    print ('<div><br/> z.show function can take optional parameters ')
-    print ('to adapt graph width and height</div>')
-    print ("<div><b>example </b>:")
-    print ('''<pre>z.show(plt,width='50px')
-z.show(plt,height='150px') </pre></div>''')
-    print ('<h3>Pandas DataFrame</h3>')
-    print ('<div> You need to have Pandas module installed ')
-    print ('to use this functionality (pip install pandas) !</div><br/>')
-    print ("""
-<div>The interpreter can visualize Pandas DataFrame
-with the function z.show()
-<pre>
-import pandas as pd
-df = pd.read_csv("bank.csv", sep=";")
-z.show(df)
-</pre></div>
-""")
-    print ('<h3>SQL over Pandas DataFrame</h3>')
-    print ('<div> You need to have Pandas&Pandasql modules installed ')
-    print ('to use this functionality (pip install pandas pandasql) 
!</div><br/>')
-    print ("""
-<div>Python interpreter group includes %sql interpreter that can query
-Pandas DataFrames using SQL and visualize results using Zeppelin Table Display 
System
-
-<pre>
-%python
-import pandas as pd
-df = pd.read_csv("bank.csv", sep=";")
-</pre>
-<br />
-
-<pre>
-%python.sql
-%sql
-SELECT * from df LIMIT 5
-</pre></div>
-""")
+    print("""%html
+ <h2>Python Interpreter help</h2>
+
+ <h3>Python 2 & 3 compatibility</h3>
+ <p>The interpreter is compatible with Python 2 & 3.<br/>
+ To change Python version,
+ change in the interpreter configuration the python to the
+ desired version (example : python=/usr/bin/python3)</p>
+
+ <h3>Python modules</h3>
+ <p>The interpreter can use all modules already installed
+ (with pip, easy_install, etc)</p>
+
+ <h3>Forms</h3>
+ You must install py4j in order to use
+ the form feature (pip install py4j)
+ <h4>Input form</h4>
+ <pre>print (z.input("f1","defaultValue"))</pre>
+ <h4>Selection form</h4>
+ <pre>print(z.select("f2", [("o1","1"), ("o2","2")],2))</pre>
+ <h4>Checkbox form</h4>
+ <pre> print("".join(z.checkbox("f3", [("o1","1"), ("o2","2")],["1"])))</pre>')
+
+ <h3>Matplotlib graph</h3>
+ <div>The interpreter can display matplotlib graph with
+ the function z.show()</div>
+ <div> You need to already have matplotlib module installed
+ to use this functionality !</div><br/>
+ <pre>import matplotlib.pyplot as plt
+ plt.figure()
+ (.. ..)
+ z.show(plt)
+ plt.close()
+ </pre>
+ <div><br/> z.show function can take optional parameters
+ to adapt graph width and height</div>
+ <div><b>example </b>:
+ <pre>z.show(plt,width='50px
+ z.show(plt,height='150px') </pre></div>
+
+ <h3>Pandas DataFrame</h3>
+ <div> You need to have Pandas module installed
+ to use this functionality (pip install pandas) !</div><br/>
+ <div>The interpreter can visualize Pandas DataFrame
+ with the function z.show()
+ <pre>
+ import pandas as pd
+ df = pd.read_csv("bank.csv", sep=";")
+ z.show(df)
+ </pre></div>
+
+ <h3>SQL over Pandas DataFrame</h3>
+ <div> You need to have Pandas&Pandasql modules installed
+ to use this functionality (pip install pandas pandasql) !</div><br/>
+
+ <div>Python interpreter group includes %sql interpreter that can query
+ Pandas DataFrames using SQL and visualize results using Zeppelin Table 
Display System
+
+ <pre>
+ %python
+ import pandas as pd
+ df = pd.read_csv("bank.csv", sep=";")
+ </pre>
+ <br />
+ <pre>
+ %python.sql
+ %sql
+ SELECT * from df LIMIT 5
+ </pre>
+ </div>
+    """)
 
 
 class PyZeppelinContext(object):
@@ -112,18 +114,17 @@ class PyZeppelinContext(object):
     errorMsg = "You must install py4j Python module " \
                "(pip install py4j) to use Zeppelin dynamic forms features"
     
-    def __init__(self, zc):
-        self.z = zc
+    def __init__(self):
         self.max_result = 1000
     
     def input(self, name, defaultValue=""):
-        print (self.errorMsg)
+        print(self.errorMsg)
     
     def select(self, name, options, defaultValue=""):
-        print (self.errorMsg)
+        print(self.errorMsg)
     
     def checkbox(self, name, options, defaultChecked=[]):
-        print (self.errorMsg)
+        print(self.errorMsg)
     
     def show(self, p, **kwargs):
         if hasattr(p, '__name__') and p.__name__ == "matplotlib.pyplot":
@@ -172,4 +173,4 @@ class PyZeppelinContext(object):
         img.close()
 
 
-z = PyZeppelinContext("")
+z = PyZeppelinContext()

http://git-wip-us.apache.org/repos/asf/zeppelin/blob/a922fd28/python/src/main/resources/bootstrap_input.py
----------------------------------------------------------------------
diff --git a/python/src/main/resources/bootstrap_input.py 
b/python/src/main/resources/bootstrap_input.py
index d15b93a..e006816 100644
--- a/python/src/main/resources/bootstrap_input.py
+++ b/python/src/main/resources/bootstrap_input.py
@@ -25,11 +25,11 @@ java_import(gateway.jvm, 
"org.apache.zeppelin.display.Input")
 class Py4jZeppelinContext(PyZeppelinContext):
     """A context impl that uses Py4j to communicate to JVM
     """
-    def __init__(self, zc):
-        super(Py4jZeppelinContext, self).__init__(zc)
+    def __init__(self, z):
+        self.z = z
         self.paramOption = 
gateway.jvm.org.apache.zeppelin.display.Input.ParamOption
         self.javaList = gateway.jvm.java.util.ArrayList
-        self.max_result = 1000 #TODO(bzz): read `zeppelin.python.maxResult` 
from JVM
+        self.max_result = self.z.getMaxResult()
     
     def input(self, name, defaultValue=""):
         return self.z.getGui().input(name, defaultValue)

http://git-wip-us.apache.org/repos/asf/zeppelin/blob/a922fd28/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterWithPythonInstalledTest.java
----------------------------------------------------------------------
diff --git 
a/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterWithPythonInstalledTest.java
 
b/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterWithPythonInstalledTest.java
index 15787fd..38b46e7 100644
--- 
a/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterWithPythonInstalledTest.java
+++ 
b/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterWithPythonInstalledTest.java
@@ -42,7 +42,7 @@ import org.junit.Test;
 public class PythonInterpreterWithPythonInstalledTest {
 
   @Test
-  public void badSqlSyntaxFails() {
+  public void badPythonSyntaxFails() {
     //given
     PythonInterpreter realPython = new PythonInterpreter(
         PythonInterpreterTest.getPythonTestProperties());
@@ -58,4 +58,21 @@ public class PythonInterpreterWithPythonInstalledTest {
     assertTrue(ret.message().length() > 0);
   }
 
+  @Test
+  public void goodPythonSyntaxRuns() {
+    //given
+    PythonInterpreter realPython = new PythonInterpreter(
+        PythonInterpreterTest.getPythonTestProperties());
+    realPython.open();
+
+    //when
+    InterpreterResult ret = realPython.interpret("help()", null);
+
+    //then
+    assertNotNull("Interpreter returned 'null'", ret);
+    //System.out.println("\nInterpreter response: \n" + ret.message());
+    assertEquals(InterpreterResult.Code.SUCCESS, ret.code());
+    assertTrue(ret.message().length() > 0);
+  }
+
 }

Reply via email to