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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations