public void write(PyObject ttype, PyString value) { out.append(ttype) .append(":[") .append(value.getString().replace("\n", "\\n")) .append("']") .append('\n'); }
@Override public void write(DataOutput out) throws IOException { PyString pyString; try { pyString = cPickle.dumps(getPyObject()); } catch (PyException e) { LOG.fatal("Could not serialize wrapped Jython value: " + getPyObject().__str__()); throw e; } WritableUtils.writeString(out, pyString.getString()); }
@Test public void raw_usecase() { PythonInterpreter interpreter = new PyGateway().getInterpreter(); // Set a variable with the content you want to work with interpreter.set( "code", "" + "(defn year-end-evaluation\n" + " []\n" + " (if (> (rand) 0.5)\n" + " \"You get a raise!\"\n" + " \"Better luck next year!\"))"); // Simple use Pygments as you would in Python interpreter.exec( "from pygments import highlight\n" + "from pygments.lexers.jvm import ClojureLexer\n" + "from pygments.formatters import RawTokenFormatter\n" + "\n" + "result = highlight(code, ClojureLexer(), RawTokenFormatter())"); // Get the result that has been set in a variable PyObject result = interpreter.get("result"); PyString string = (PyString) result; assertThat(string.getString()) .isEqualTo( "" + "Token.Punctuation\tu'('\n" + "Token.Keyword.Declaration\tu'defn '\n" + "Token.Name.Variable\tu'year-end-evaluation'\n" + "Token.Text\tu'\\n '\n" + "Token.Punctuation\tu'['\n" + "Token.Punctuation\tu']'\n" + "Token.Text\tu'\\n '\n" + "Token.Punctuation\tu'('\n" + "Token.Keyword\tu'if '\n" + "Token.Punctuation\tu'('\n" + "Token.Name.Builtin\tu'> '\n" + "Token.Punctuation\tu'('\n" + "Token.Name.Function\tu'rand'\n" + "Token.Punctuation\tu')'\n" + "Token.Text\tu' '\n" + "Token.Literal.Number.Float\tu'0.5'\n" + "Token.Punctuation\tu')'\n" + "Token.Text\tu'\\n '\n" + "Token.Literal.String\tu'\"You get a raise!\"'\n" + "Token.Text\tu'\\n '\n" + "Token.Literal.String\tu'\"Better luck next year!\"'\n" + "Token.Punctuation\tu')'\n" + "Token.Punctuation\tu')'\n" + "Token.Text\tu'\\n'\n"); }