You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zeppelin.apache.org by mo...@apache.org on 2015/08/09 21:49:37 UTC
incubator-zeppelin git commit: Improve magic display system
Repository: incubator-zeppelin
Updated Branches:
refs/heads/master 3bc145f04 -> d7ef38d48
Improve magic display system
I have changed the InterpreterResult behavior so that if a magic word is detected all preceding text is omitted. This way we can use the display system and print out using the magic words in paragraphs such as spark.
example:
```
sc.parallelize(1 to 10)
```
will output:
```
res9: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[2] at parallelize at <console>:24
```
and mix paragraphs such as:
```
sc.parallelize(1 to 10)
println("%table col\tcol2\nqwe\t123\n")
```
will output the expected magic table
![selection_003](https://cloud.githubusercontent.com/assets/2227083/8849793/25ecf24c-314b-11e5-8136-ef689eaeb917.png)
This behavior can be later extended to handle complex types.
I have also added a JUnit test for the class
Author: cto <go...@gmail.com>
Closes #164 from eranwitkon/master and squashes the following commits:
afefcf2 [cto] Merge remote-tracking branch 'upstream/master'
2eee9a2 [cto] change logic to match the new rule: paragraph type will be set by the first magic word in the paragraph.
30922f2 [cto] Merge remote-tracking branch 'upstream/master'
29470ce [cto] added more simple text test and comments in code
3ef1584 [cto] refactoring to avoid the duplication of output message parsing
5b2d1c8 [cto] Improve magic display system
f3c4df3 [cto] improve magic word handling by omitting the prefix to the magic word
Project: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/commit/d7ef38d4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/tree/d7ef38d4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/diff/d7ef38d4
Branch: refs/heads/master
Commit: d7ef38d485b315707d826ee54ee609597430d068
Parents: 3bc145f
Author: cto <go...@gmail.com>
Authored: Sun Aug 9 08:25:34 2015 +0300
Committer: Lee moon soo <mo...@apache.org>
Committed: Sun Aug 9 12:49:21 2015 -0700
----------------------------------------------------------------------
zeppelin-interpreter/pom.xml | 5 +
.../zeppelin/interpreter/InterpreterResult.java | 57 ++++++----
.../interpreter/InterpreterResultTest.java | 108 +++++++++++++++++++
3 files changed, 151 insertions(+), 19 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/d7ef38d4/zeppelin-interpreter/pom.xml
----------------------------------------------------------------------
diff --git a/zeppelin-interpreter/pom.xml b/zeppelin-interpreter/pom.xml
index 8251055..3b6e49b 100644
--- a/zeppelin-interpreter/pom.xml
+++ b/zeppelin-interpreter/pom.xml
@@ -81,5 +81,10 @@
<version>1.9.0</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.3.2</version>
+ </dependency>
</dependencies>
</project>
http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/d7ef38d4/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/InterpreterResult.java
----------------------------------------------------------------------
diff --git a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/InterpreterResult.java b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/InterpreterResult.java
index 5d8d96f..20317eb 100644
--- a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/InterpreterResult.java
+++ b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/InterpreterResult.java
@@ -18,6 +18,8 @@
package org.apache.zeppelin.interpreter;
import java.io.Serializable;
+import org.apache.commons.lang3.StringUtils;
+import java.util.*;
/**
* Interpreter result template.
@@ -87,36 +89,53 @@ public class InterpreterResult implements Serializable {
if (msg == null) {
return null;
}
-
- Type[] types = Type.values();
- for (Type t : types) {
- String magic = "%" + t.name().toLowerCase();
- if (msg.startsWith(magic + " ") || msg.startsWith(magic + "\n")) {
- int magicLength = magic.length() + 1;
- if (msg.length() > magicLength) {
- return msg.substring(magicLength);
- } else {
- return "";
- }
- }
+ Type[] types = type.values();
+ TreeMap<Integer, Type> typesLastIndexInMsg = buildIndexMap(msg);
+ if (typesLastIndexInMsg.size() == 0) {
+ return msg;
+ } else {
+ Map.Entry<Integer, Type> lastType = typesLastIndexInMsg.firstEntry();
+ //add 1 for the % char
+ int magicLength = lastType.getValue().name().length() + 1;
+ // 1 for the last \n or space after magic
+ int subStringPos = magicLength + lastType.getKey() + 1;
+ return msg.substring(subStringPos);
}
-
- return msg;
}
-
private Type getType(String msg) {
if (msg == null) {
return Type.TEXT;
}
+ Type[] types = type.values();
+ TreeMap<Integer, Type> typesLastIndexInMsg = buildIndexMap(msg);
+ if (typesLastIndexInMsg.size() == 0) {
+ return Type.TEXT;
+ } else {
+ Map.Entry<Integer, Type> lastType = typesLastIndexInMsg.firstEntry();
+ return lastType.getValue();
+ }
+ }
+
+ private int getIndexOfType(String msg, Type t) {
+ if (msg == null) {
+ return 0;
+ }
+ String typeString = "%" + t.name().toLowerCase();
+ return StringUtils.indexOf(msg, typeString );
+ }
+
+ private TreeMap<Integer, Type> buildIndexMap(String msg) {
+ int lastIndexOftypes = 0;
+ TreeMap<Integer, Type> typesLastIndexInMsg = new TreeMap<Integer, Type>();
Type[] types = Type.values();
for (Type t : types) {
- String magic = "%" + t.name().toLowerCase();
- if (msg.startsWith(magic + " ") || msg.startsWith(magic + "\n")) {
- return t;
+ lastIndexOftypes = getIndexOfType(msg, t);
+ if (lastIndexOftypes >= 0) {
+ typesLastIndexInMsg.put(lastIndexOftypes, t);
}
}
- return Type.TEXT;
+ return typesLastIndexInMsg;
}
public Code code() {
http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/d7ef38d4/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/InterpreterResultTest.java
----------------------------------------------------------------------
diff --git a/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/InterpreterResultTest.java b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/InterpreterResultTest.java
new file mode 100644
index 0000000..007730a
--- /dev/null
+++ b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/InterpreterResultTest.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+
+
+
+public class InterpreterResultTest {
+
+ @Test
+ public void testTextType() {
+
+ InterpreterResult result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"this is a TEXT type");
+ assertEquals("No magic",InterpreterResult.Type.TEXT, result.type());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%this is a TEXT type");
+ assertEquals("No magic",InterpreterResult.Type.TEXT, result.type());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%\n");
+ assertEquals("No magic",InterpreterResult.Type.TEXT, result.type());
+ }
+ @Test
+ public void testSimpleMagicType() {
+ InterpreterResult result = null;
+
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%table col1\tcol2\naaa\t123\n");
+ assertEquals(InterpreterResult.Type.TABLE, result.type());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%table\ncol1\tcol2\naaa\t123\n");
+ assertEquals(InterpreterResult.Type.TABLE, result.type());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"some text before magic word %table col1\tcol2\naaa\t123\n");
+ assertEquals(InterpreterResult.Type.TABLE, result.type());
+ }
+
+ public void testComplexMagicType() {
+ InterpreterResult result = null;
+
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"some text before %table col1\tcol2\naaa\t123\n");
+ assertEquals("some text before magic return magic",InterpreterResult.Type.TABLE, result.type());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%html <h3> This is a hack </h3> %table\n col1\tcol2\naaa\t123\n");
+ assertEquals("magic A before magic B return magic A",InterpreterResult.Type.HTML, result.type());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"some text before magic word %table col1\tcol2\naaa\t123\n %html <h3> This is a hack </h3>");
+ assertEquals("text & magic A before magic B return magic A" ,InterpreterResult.Type.TABLE, result.type());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%table col1\tcol2\naaa\t123\n %html <h3> This is a hack </h3> %table col1\naaa\n123\n");
+ assertEquals("magic A, magic B, magic a' return magic A" ,InterpreterResult.Type.TABLE, result.type());
+
+ }
+
+ @Test
+ public void testSimpleMagicData() {
+
+ InterpreterResult result = null;
+
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%table col1\tcol2\naaa\t123\n");
+ assertEquals("%table col1\tcol2\naaa\t123\n","col1\tcol2\naaa\t123\n", result.message());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%table\ncol1\tcol2\naaa\t123\n");
+ assertEquals("%table\ncol1\tcol2\naaa\t123\n","col1\tcol2\naaa\t123\n", result.message());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"some text before magic word %table col1\tcol2\naaa\t123\n");
+ assertEquals("some text before magic word %table col1\tcol2\naaa\t123\n","col1\tcol2\naaa\t123\n", result.message());
+ }
+
+ @Test
+ public void testComplexMagicData() {
+
+ InterpreterResult result = null;
+
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"some text before %table col1\tcol2\naaa\t123\n");
+ assertEquals("text before %table return %table","col1\tcol2\naaa\t123\n", result.message());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%html <h3> This is a hack </h3> %table\ncol1\tcol2\naaa\t123\n");
+ assertEquals("%html before %table return %html"," <h3> This is a hack </h3> %table\n" +
+ "col1\tcol2\n" +
+ "aaa\t123\n", result.message());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"some text before magic word %table col1\tcol2\naaa\t123\n %html <h3> This is a hack </h3>");
+ assertEquals("text & %table befoe %html return %table","col1\tcol2\n" +
+ "aaa\t123\n" +
+ " %html <h3> This is a hack </h3>", result.message());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%table col1\tcol2\naaa\t123\n %html <h3> This is a hack </h3> %table col1\naaa\n123\n");
+ assertEquals("%table, %html before %table return first %table","col1\tcol2\n" +
+ "aaa\t123\n" +
+ " %html <h3> This is a hack </h3> %table col1\n" +
+ "aaa\n" +
+ "123\n", result.message());
+ result = new InterpreterResult(InterpreterResult.Code.SUCCESS,"%table col1\tcol2\naaa\t123\n %table col1\naaa\n123\n");
+ assertEquals("%table before %table return first %table","col1\tcol2\n" +
+ "aaa\t123\n" +
+ " %table col1\n" +
+ "aaa\n" +
+ "123\n", result.message());
+ }
+
+}