org.python.core.PyList Java Examples

The following examples show how to use org.python.core.PyList. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JythonSupport.java    From SikuliX1 with MIT License 6 votes vote down vote up
public void getSysPath() {
  synchronized (sysPath) {
    if (null == interpreter) {
      return;
    }
    sysPath.clear();
    try {
      PySystemState pyState = interpreter.getSystemState();
      PyList pyPath = pyState.path;
      int pathLen = pyPath.__len__();
      for (int i = 0; i < pathLen; i++) {
        String entry = (String) pyPath.get(i);
        log(lvl + 1, "sys.path[%2d] = %s", i, entry);
        sysPath.add(entry);
      }
    } catch (Exception ex) {
      sysPath.clear();
    }
  }
}
 
Example #2
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public PyList for_links(final PyFunction condition, PyFunction nodeCondition, PyFunction computation) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> links = getLinks(context.current());
    final List<Object> results = new ArrayList<>();
    for (final Map.Entry<Integer, Integer> entry : links.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.LINK);
        if (condition == null || isTrueAndContainsNonNulls(condition.__call__())) {
            context.enter(entry.getValue(), GraphElementType.VERTEX);
            boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
            context.exit();
            if (otherNodeMatches) {
                final Object result = computation.__call__();
                if (!nullCheck(result)) {
                    results.add(result);
                }
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example #3
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public PyList for_edges(final PyFunction condition, PyFunction nodeCondition, PyFunction computation) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> edges = getEdges(context.current());
    final List<Object> results = new ArrayList<>();
    for (final Map.Entry<Integer, Integer> entry : edges.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.EDGE);
        if (condition == null || isTrueAndContainsNonNulls(condition.__call__())) {
            context.enter(entry.getValue(), GraphElementType.VERTEX);
            boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
            context.exit();
            if (otherNodeMatches) {
                final Object result = computation.__call__();
                if (!nullCheck(result)) {
                    results.add(result);
                }
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example #4
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public PyList for_parallel_transactions(final PyFunction condition, final PyFunction computation) throws ScriptException {
    context.require(GraphElementType.TRANSACTION);
    final Set<Integer> transactions = getParallelTransactions(context.current());
    final List<Object> results = new ArrayList<>();
    for (int transId : transactions) {
        context.enter(transId, GraphElementType.TRANSACTION);
        if (condition == null || isTrueValue(condition.__call__())) {
            final Object result = computation.__call__();
            if (!nullCheck(result)) {
                results.add(result);
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example #5
Source File: JIntrospect.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Complete package name
 *
 * @param target Target
 * @return Package names
 */
public List<String> completePackageName(String target) {
    String[] targetComponents = target.split("\\.");
    String base = targetComponents[0];
    PySystemState state = interp.getSystemState();
    PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__"));
    PyObject module = importer.__call__(Py.newString(base));
    if (targetComponents.length > 1) {
        for (int i = 1; i < targetComponents.length; i++) {
            module = module.__getattr__(targetComponents[i]);
        }
    }
    PyList plist = (PyList) module.__dir__();
    List<String> list = new ArrayList<>();
    String name;
    for (int i = 0; i < plist.__len__(); i++) {
        name = plist.get(i).toString();
        if (!name.startsWith("__")) {
            list.add(name);
        }
    }
    //list.add("*");

    return list;
}
 
Example #6
Source File: JythonSupport.java    From SikuliX1 with MIT License 6 votes vote down vote up
public List<String> getSysArgv() {
  sysArgv = new ArrayList<String>();
  if (null == interpreter) {
    sysArgv = null;
    return null;
  }
  try {
    PyList pyArgv = interpreter.getSystemState().argv;
    Integer argvLen = pyArgv.__len__();
    for (int i = 0; i < argvLen; i++) {
      String entry = (String) pyArgv.get(i);
      log(lvl + 1, "sys.path[%2d] = %s", i, entry);
      sysArgv.add(entry);
    }
  } catch (Exception ex) {
    sysArgv = null;
  }
  return sysArgv;
}
 
Example #7
Source File: JythonUtils.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
public static List<Object> getList(ArgParser ap, int position)
/*     */   {
/* 169 */     PyObject arg = ap.getPyObject(position, Py.None);
/* 170 */     if (Py.isInstance(arg, PyNone.TYPE)) {
/* 171 */       return Collections.emptyList();
/*     */     }
/*     */
/* 174 */     List ret = Lists.newArrayList();
/* 175 */     PyList array = (PyList)arg;
/* 176 */     for (int x = 0; x < array.__len__(); x++) {
/* 177 */       PyObject item = array.__getitem__(x);
/*     */
/* 179 */       Class javaClass = (Class)PYOBJECT_TO_JAVA_OBJECT_MAP.get(item.getClass());
/* 180 */       if (javaClass != null) {
/* 181 */         ret.add(item.__tojava__(javaClass));
/*     */       }
/*     */     }
/* 184 */     return ret;
/*     */   }
 
Example #8
Source File: JythonUtils.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
public static Map<String, Object> getMap(ArgParser ap, int position)
/*     */   {
/* 196 */     PyObject arg = ap.getPyObject(position, Py.None);
/* 197 */     if (Py.isInstance(arg, PyNone.TYPE)) {
/* 198 */       return Collections.emptyMap();
/*     */     }
/*     */
/* 201 */     Map ret = Maps.newHashMap();
/*     */
/* 203 */     PyDictionary dict = (PyDictionary)arg;
/* 204 */     PyList items = dict.items();
/* 205 */     for (int x = 0; x < items.__len__(); x++)
/*     */     {
/* 207 */       PyTuple item = (PyTuple)items.__getitem__(x);
/*     */
/* 209 */       String key = (String)item.__getitem__(0).__str__().__tojava__(String.class);
/* 210 */       PyObject value = item.__getitem__(1);
/*     */
/* 213 */       Class javaClass = (Class)PYOBJECT_TO_JAVA_OBJECT_MAP.get(value.getClass());
/* 214 */       if (javaClass != null) {
/* 215 */         ret.put(key, value.__tojava__(javaClass));
/*     */       }
/*     */     }
/* 218 */     return ret;
/*     */   }
 
Example #9
Source File: PythonRowDoFn.java    From components with Apache License 2.0 6 votes vote down vote up
private void flatMap(IndexedRecord input, ProcessContext context) throws IOException {
    // Prepare Python environment
    PyObject outputList = pyFn.__call__(new PyUnicode(input.toString()));

    if (outputList instanceof PyList) {
        PyList list = (PyList) outputList;
        for (Object output : list) {
            if (jsonGenericRecordConverter == null) {
                JsonSchemaInferrer jsonSchemaInferrer = new JsonSchemaInferrer(new ObjectMapper());
                Schema jsonSchema = jsonSchemaInferrer.inferSchema(output.toString());
                jsonGenericRecordConverter = new JsonGenericRecordConverter(jsonSchema);
            }
            GenericRecord outputRecord = jsonGenericRecordConverter.convertToAvro(output.toString());
            context.output(outputRecord);
        }
    }
}
 
Example #10
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
private PyList forTransactions(final PyFunction condition, PyFunction nodeCondition, PyFunction computation, final int direction) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> transactions = getTransactions(context.current(), direction);
    final List<Object> results = new ArrayList<>();
    for (final Map.Entry<Integer, Integer> entry : transactions.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.TRANSACTION);
        if (condition == null || isTrueValue(condition.__call__())) {
            context.enter(entry.getValue(), GraphElementType.VERTEX);
            boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
            context.exit();
            if (otherNodeMatches) {
                Object result = computation.__call__();
                if (!nullCheck(result)) {
                    results.add(result);
                }
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example #11
Source File: JythonUtils.java    From spork with Apache License 2.0 6 votes vote down vote up
public static PyObject pigToPython(Object object) {
    if (object instanceof Tuple) {
        return pigTupleToPyTuple((Tuple) object);
    } else if (object instanceof DataBag) {
        PyList list = new PyList();
        for (Tuple bagTuple : (DataBag) object) {
            list.add(pigTupleToPyTuple(bagTuple));
        }
        return list;
    } else if (object instanceof Map<?, ?>) {
        PyDictionary newMap = new PyDictionary();
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
            newMap.put(entry.getKey(), pigToPython(entry.getValue()));
        }
        return newMap;
    } else if (object instanceof DataByteArray) {
        return Py.java2py(((DataByteArray) object).get());
    } else {
        return Py.java2py(object);
    }
}
 
Example #12
Source File: JythonScriptSupport.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
/** @param path Path to add to head of python search path */
private void addToPythonPath(final String path)
{
    // Since using default PySystemState (see above), check if already in paths
    final PyList paths = python.getSystemState().path;

    // Prevent concurrent modification
    synchronized (JythonScriptSupport.class)
    {
        final int index = paths.indexOf(path);

        // Warn about "examples:/... path that won't really work.
        // Still add to the list so we only get the warning once,
        // plus maybe some day we'll be able to use it...
        if (index < 0  &&
            path.startsWith(ModelResourceUtil.EXAMPLES_SCHEMA + ":"))
            logger.log(Level.WARNING, "Jython will be unable to access scripts in " + path + ". Install examples in file system.");

        // Already top entry?
        if (index == 0)
            return;
        // Remove if further down in the list
        if (index > 0)
            paths.remove(index);
        // Add to front of list
        paths.add(0, path);
    }
    logger.log(Level.FINE, "Adding to jython path: {0}", path);
}
 
Example #13
Source File: JythonSupport.java    From SikuliX1 with MIT License 5 votes vote down vote up
public void setSysArgv(List<String> args) {
  if (null == interpreter) {
    return;
  }
  try {
    PyList pyArgv = interpreter.getSystemState().argv;
    pyArgv.clear();
    for (String arg : args) {
      pyArgv.add(arg);
    }
  } catch (Exception ex) {
    sysArgv = null;
  }
}
 
Example #14
Source File: JythonProcessor.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public Object createArray(List elements) {
  PyList list = new PyList();
  for (Object element : elements) {
    list.add(element);
  }
  return list;
}
 
Example #15
Source File: ListUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public Object median(final Collection<Object> collection) throws ScriptException {
    final PyList pylist = new PyList(collection);
    if (pylist.__len__() == 0) {
        return null;
    }
    return pylist.get(pylist.__len__() / 2);
}
 
Example #16
Source File: FrmMain.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Delete variables
 */
public void delVariables() {
    PythonInteractiveInterpreter interp = this.consoleDock.getInterpreter();
    PyStringMap locals = (PyStringMap) interp.getLocals();
    PyList items = locals.items();
    String name;
    for (Object a : items) {
        PyTuple at = (PyTuple) a;
        name = at.__getitem__(0).toString();
        if (!this.loadObjects.contains(name)) {
            locals.__delitem__(name);
        }
    }
}
 
Example #17
Source File: JIntrospect.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Return list of token for command.
 * @param command The command
 * @return Token list
 * @throws java.io.UnsupportedEncodingException
 */
public PyList getTokens(String command) throws UnsupportedEncodingException {
    StringBuilder sb = new StringBuilder();
    sb.append("import cStringIO");
    sb.append("\n");
    sb.append("import tokenize");
    sb.append("\n");
    sb.append("command = str('");
    sb.append(command);
    sb.append("')");
    sb.append("\n");
    sb.append("f = cStringIO.StringIO(command)");
    sb.append("\n");
    sb.append("tokens = []");
    sb.append("\n");
    sb.append("def eater(*args):");
    sb.append("\n");
    sb.append("    tokens.append(args)");
    sb.append("\n");
    sb.append("tokenize.tokenize_loop(f.readline, eater)");
    sb.append("\n");
    String code = sb.toString();
    String encoding = "utf-8";
    PythonInterpreter pi = new PythonInterpreter();
    try {
        pi.execfile(new ByteArrayInputStream(code.getBytes(encoding)));
        PyList tokens = (PyList)pi.get("tokens");            
        return tokens;
    } catch (Exception e){
        return null;
    }
}
 
Example #18
Source File: JIntrospect.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Return list of token for command.
 * @param command The command
 * @return Token list
 * @throws java.io.UnsupportedEncodingException
 */
public PyList getTokens(String command) throws UnsupportedEncodingException {
    StringBuilder sb = new StringBuilder();
    sb.append("import cStringIO");
    sb.append("\n");
    sb.append("import tokenize");
    sb.append("\n");
    sb.append("command = str('");
    sb.append(command);
    sb.append("')");
    sb.append("\n");
    sb.append("f = cStringIO.StringIO(command)");
    sb.append("\n");
    sb.append("tokens = []");
    sb.append("\n");
    sb.append("def eater(*args):");
    sb.append("\n");
    sb.append("    tokens.append(args)");
    sb.append("\n");
    sb.append("tokenize.tokenize_loop(f.readline, eater)");
    sb.append("\n");
    String code = sb.toString();
    String encoding = "utf-8";
    PythonInterpreter pi = new PythonInterpreter();
    try {
        pi.execfile(new ByteArrayInputStream(code.getBytes(encoding)));
        PyList tokens = (PyList)pi.get("tokens");            
        return tokens;
    } catch (Exception e){
        return null;
    }
}
 
Example #19
Source File: JIntrospect.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get auto complete list
 *
 * @param command The command
 * @param includeMagic
 * @param includeSingle
 * @param includeDouble
 * @return Auto complete list
 * @throws java.io.IOException
 */
public List<String> getAutoCompleteList(String command, boolean includeMagic,
        boolean includeSingle, boolean includeDouble) throws IOException {
    // Temp KLUDGE here rather than in console.py
    //command += ".";
    if (command.startsWith("import ") || command.startsWith("from ")) {
        String target = getPackageName(command);
        if (target == null) {
            return null;
        }
        return completePackageName(target);
    }
    
    String root = this.getRoot(command, ".");
    if (root.isEmpty())
        return null;
    
    try {
        PyObject object = this.interp.eval(root);
        PyList plist = (PyList) object.__dir__();
        List<String> list = new ArrayList<>();
        String name;
        for (int i = 0; i < plist.__len__(); i++) {
            name = plist.get(i).toString();
            if (!name.startsWith("__")) {
                list.add(name);
            }
        }
        return list;
    } catch (Exception e){
        return null;
    }
}
 
Example #20
Source File: ListUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public Object mean(final Collection<Object> collection) throws ScriptException {
    final PyList pylist = new PyList(collection);
    if (pylist.__len__() == 0) {
        return null;
    }
    PyObject acc = __builtin__.sum(pylist);
    acc = acc.__div__(new PyInteger(pylist.__len__()));
    return acc;
}
 
Example #21
Source File: AbstractCalculatorUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
protected static boolean isTrueAndContainsNonNulls(PyObject obj) {
    if (obj instanceof PyList) {
        ((PyList) obj).removeIf((Object t) -> nullCheck(t));
        return !((PyList) obj).isEmpty();
    }
    return isTrueValue(obj);
}
 
Example #22
Source File: DocTest.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
private void execFile(String filePath, String... arguments) {
    interp.cleanup();
    interp.set("__file__", filePath);
    sys.argv = new PyList(new PyString[]{new PyString(filePath)});
    sys.argv.extend(new PyArray(PyString.class, arguments));
    interp.execfile(filePath);
}
 
Example #23
Source File: FlightTelemetry.java    From hazelcast-jet-demos with Apache License 2.0 4 votes vote down vote up
PyList getAsList() {
    PyList list = new PyList();
    PyTuple metric = new PyTuple(metricName, new PyTuple(timestamp, metricValue));
    list.add(metric);
    return list;
}
 
Example #24
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 4 votes vote down vote up
private PyList forTransactions(final PyFunction computation, final int direction) {
    return forTransactions(null, computation, direction);
}
 
Example #25
Source File: JythonModules.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public static String makeISort(String fileContents, File f, Set<String> knownThirdParty) {
    IPythonInterpreter iPythonInterpreter = iSortThreadLocalInterpreter.get();
    IPythonInterpreter interpreter;
    String outputLine = "output = getattr(isort.SortImports(file_contents=fileContents, settings_path=settingsPath, known_third_party=knownThirdParty), 'output', None)\n";
    if (iPythonInterpreter == null) {
        // The first call may be slow because doing the imports is slow, but subsequent calls should be
        // fast as we'll be reusing the same interpreter.
        String s = ""
                + "import sys\n"
                + "import os\n"
                + "add_to_pythonpath = '%s'\n"
                + "os.chdir(add_to_pythonpath)\n"
                + "if add_to_pythonpath not in sys.path:\n"
                + "    sys.path.append(add_to_pythonpath)\n"
                + "import isort\n"
                + outputLine;

        boolean useConsole = false;

        interpreter = JythonPlugin.newPythonInterpreter(useConsole, false);
        String isortContainerLocation = null;
        try {
            isortContainerLocation = CorePlugin.getScriptWithinPySrc(
                    new Path("third_party").append("isort_container").toString()).toString();
            File isortContainer = new File(isortContainerLocation);
            if (!isortContainer.exists()) {
                Log.log("Specified location for isort_container does not exist (" + isortContainerLocation
                        + ").");
                return null;
            }
        } catch (CoreException e) {
            Log.log("Error getting isort_container location", e);
            return null;
        }

        interpreter.set("fileContents", fileContents);
        if (f != null) {
            interpreter.set("settingsPath", f.getAbsoluteFile().getParent());
        } else {
            interpreter.set("settingsPath", "");
        }
        interpreter.set("knownThirdParty", new PyList(knownThirdParty));
        s = StringUtils.format(s, StringUtils.replaceAllSlashes(isortContainerLocation));
        interpreter.exec(s);
        iSortThreadLocalInterpreter.set(interpreter);
    } else {
        interpreter = iPythonInterpreter;
        // Found interpreter in thread local storage, just use it to do the sort.
        interpreter.set("fileContents", fileContents);
        if (f != null) {
            interpreter.set("settingsPath", f.getAbsoluteFile().getParent());
        } else {
            interpreter.set("settingsPath", "");
        }
        interpreter.set("knowhThirdParty", new PyList(knownThirdParty));
        // Note that we have to clear the global caches that isort has for it to reload the settings (otherwise,
        // eclipse needs to be restarted just to get the updated caches).
        interpreter
                .exec(""
                        + "isort.settings._get_config_data.cache_clear()\n"
                        + "isort.settings.from_path.cache_clear()\n"
                        + outputLine);
    }

    PyObject pyObject = (PyObject) interpreter.get("output");
    if (pyObject != null && pyObject.__nonzero__()) {
        return pyObject.toString();
    }
    return null;

}
 
Example #26
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 4 votes vote down vote up
private PyList forTransactions(final PyFunction condition, PyFunction computation, final int direction) {
    return forTransactions(condition, null, computation, direction);
}
 
Example #27
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 4 votes vote down vote up
public PyList for_transactions(final PyFunction computation) {
    return forTransactions(computation, Graph.NOT_FOUND);
}
 
Example #28
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 4 votes vote down vote up
public PyList for_outgoing_transactions(final PyFunction computation) {
    return forTransactions(computation, Graph.OUTGOING);
}
 
Example #29
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 4 votes vote down vote up
public PyList for_incoming_transactions(final PyFunction computation) {
    return forTransactions(computation, Graph.INCOMING);
}
 
Example #30
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 4 votes vote down vote up
public PyList for_undirected_transactions(final PyFunction computation) {
    return forTransactions(computation, Graph.UNDIRECTED);
}