Search in sources :

Example 1 with PyInstance

use of org.python.core.PyInstance in project Pogamut3 by kefik.

the class PyInstanceWrapper method getChildren.

@Override
public ArrayList<PyObjectAdapter> getChildren(Object object) {
    if (!(object instanceof PyInstance))
        throw new IllegalArgumentException("object is not instance of PyInstance");
    PyInstance pyInstance = (PyInstance) object;
    PyList keys = null;
    if (pyInstance.__dict__ instanceof PyDictionary)
        keys = ((PyDictionary) pyInstance.__dict__).keys();
    if (pyInstance.__dict__ instanceof PyStringMap)
        keys = ((PyStringMap) pyInstance.__dict__).keys();
    if (keys == null)
        return null;
    final PyObject pyDict = pyInstance.__dict__;
    int count = keys.__len__();
    ArrayList<PyObjectAdapter> list = new ArrayList<PyObjectAdapter>(count);
    PyObject obj;
    for (int i = 0; i < count; ++i) {
        final PyObject key = keys.__getitem__(i);
        Object name = (String) key.__tojava__(String.class);
        if ((name == Py.NoConversion) || (!(name instanceof String)))
            continue;
        obj = pyDict.__finditem__(key);
        list.add(new PyObjectAdapter((String) name, new PyObjectPlace() {

            private PyObject myPlace = key;

            @Override
            public void set(PyObject newValue) {
                pyDict.__setitem__(myPlace, newValue);
            }

            @Override
            public PyObject get() {
                try {
                    return pyDict.__finditem__(myPlace);
                } catch (Exception e) {
                    return null;
                }
            }
        }));
    }
    return list;
}
Also used : PyInstance(org.python.core.PyInstance) ArrayList(java.util.ArrayList) PyDictionary(org.python.core.PyDictionary) PyStringMap(org.python.core.PyStringMap) PyList(org.python.core.PyList) PyObject(org.python.core.PyObject) PyObject(org.python.core.PyObject)

Aggregations

ArrayList (java.util.ArrayList)1 PyDictionary (org.python.core.PyDictionary)1 PyInstance (org.python.core.PyInstance)1 PyList (org.python.core.PyList)1 PyObject (org.python.core.PyObject)1 PyStringMap (org.python.core.PyStringMap)1