Search in sources :

Example 21 with PyObject

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

the class PyTupleWrapper method getJavaObject.

@Override
public Object getJavaObject(Object pyObject) {
    if (!(pyObject instanceof PyTuple))
        throw new IllegalArgumentException("pyObject is not instance of PyTuple");
    PyTuple pyTuple = (PyTuple) pyObject;
    int count = (pyTuple.__len__());
    ArrayList list = new ArrayList(count);
    PyObject obj = null;
    PyObjectWrapper wrapper = null;
    for (int i = 0; i < count; ++i) {
        obj = pyTuple.__getitem__(i);
        wrapper = PyObjectWrappersManager.getWrapper(obj.getClass());
        list.add(wrapper.getJavaObject(obj));
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) PyTuple(org.python.core.PyTuple) PyObject(org.python.core.PyObject)

Example 22 with PyObject

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

the class PythonEngineFolder method filterEngineVariables.

/**
 * Returns adapter for variables which have or haven't children (based on parameter hasChildren)
 *
 * @param properties whether we want properties = basic types (false) or complex types = list, dicts, classes (true)
 * @return list of adapters
 */
private ArrayList<PyObjectAdapter> filterEngineVariables(ScriptEngine engine, boolean hasChildren) {
    ScriptContext context = engine.getContext();
    List<Integer> scopes = context.getScopes();
    int scope = scopes.get(0);
    final Bindings bindings = context.getBindings(scope);
    Set<Entry<String, Object>> entries = bindings.entrySet();
    Object value;
    PyObjectWrapper wrapper = null;
    PyObjectAdapter adapter = null;
    ArrayList<PyObjectAdapter> list = new ArrayList<PyObjectAdapter>();
    for (Entry<String, Object> entry : entries) {
        final String key = entry.getKey();
        value = entry.getValue();
        wrapper = PyObjectWrappersManager.getWrapper(value.getClass());
        if (wrapper instanceof PyUnsupportedWrapper)
            continue;
        if (hasChildren) {
            // we want only types with children
            if (!wrapper.hasChildren(value))
                continue;
        } else {
            // we want only basic types
            if (wrapper.hasChildren(value))
                continue;
        }
        adapter = new PyObjectAdapter(key, new PyObjectPlace() {

            private Bindings b = bindings;

            private String k = key;

            @Override
            public Object get() {
                try {
                    return b.get(k);
                } catch (Exception e) {
                    return Py.None;
                }
            }

            @Override
            public void set(PyObject newValue) {
                bindings.put(key, newValue);
            }
        });
        list.add(adapter);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) PyString(org.python.core.PyString) IntrospectionException(cz.cuni.amis.introspection.IntrospectionException) FileNotFoundException(java.io.FileNotFoundException) Entry(java.util.Map.Entry) PyObject(org.python.core.PyObject) PyObject(org.python.core.PyObject)

Example 23 with PyObject

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

the class PyDictionaryWrapper method getChildren.

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

            private PyObject myPlace = pyKey;

            @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 : PyList(org.python.core.PyList) ArrayList(java.util.ArrayList) PyObject(org.python.core.PyObject) PyString(org.python.core.PyString) PyDictionary(org.python.core.PyDictionary) PyObject(org.python.core.PyObject)

Example 24 with PyObject

use of org.python.core.PyObject 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)

Example 25 with PyObject

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

the class PyListWrapper method getNewValue.

/**
 * Accepts Collection<PyObject> as parameter of newValue.
 * Returns PyList
 */
@Override
public PyObject getNewValue(Object newValue) {
    if (!(newValue instanceof Collection)) {
        throw new IllegalArgumentException();
    }
    Collection collection = (Collection) newValue;
    PyList list = new PyList();
    Iterator iter = collection.iterator();
    while (iter.hasNext()) {
        Object next = iter.next();
        if (!(next instanceof PyObject))
            throw new IllegalArgumentException("value of the collection is not instance of PyObject");
        list.__add__((PyObject) next);
    }
    return list;
}
Also used : PyList(org.python.core.PyList) Iterator(java.util.Iterator) Collection(java.util.Collection) PyObject(org.python.core.PyObject) PyObject(org.python.core.PyObject)

Aggregations

PyObject (org.python.core.PyObject)80 PyString (org.python.core.PyString)24 PyList (org.python.core.PyList)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)12 PyException (org.python.core.PyException)12 IOException (java.io.IOException)9 Serializable (java.io.Serializable)9 PyInteger (org.python.core.PyInteger)8 Test (org.junit.Test)7 PyStringMap (org.python.core.PyStringMap)7 PythonInterpreter (org.python.util.PythonInterpreter)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Map (java.util.Map)6 PyDictionary (org.python.core.PyDictionary)6 PyFloat (org.python.core.PyFloat)6 List (java.util.List)5 PyLong (org.python.core.PyLong)5 PyNone (org.python.core.PyNone)5 PyTuple (org.python.core.PyTuple)5