Java Code Examples for org.python.core.PyList#__len__

The following examples show how to use org.python.core.PyList#__len__ . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
Source File: JIntrospect.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 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__();
        if (plist.contains("__all__")) {
            PyList nPlist = (PyList) object.__getattr__("__all__");
            for (int i = 0; i < nPlist.__len__(); i++) {
                if (! plist.__contains__(nPlist.__getitem__(i))) {
                    plist.add(nPlist.__getitem__(i));
                }
            }
        }
        List<String> list = new ArrayList<>();
        String name;
        for (int i = 0; i < plist.__len__(); i++) {
            name = plist.get(i).toString();
            if (!name.contains("__")) {
                list.add(name);
            }
        }
        return list;
    } catch (Exception e){
        return null;
    }
}