8#include "MantidJson/Json.h"
16#include <boost/python/dict.hpp>
17#include <boost/python/errors.hpp>
18#include <boost/python/extract.hpp>
19#include <boost/python/import.hpp>
20#include <boost/python/list.hpp>
26std::set<std::string>
const jsonAllowedTypes{
"int",
"float",
"str",
"NoneType",
"bool"};
28inline bool isJsonAtomic(bp::object
const &
obj) {
29 std::string
const objname = bp::extract<std::string>(
obj.attr(
"__class__").attr(
"__name__"));
30 return jsonAllowedTypes.count(objname) > 0;
41bp::object recursiveDictDump(bp::object
const &
obj,
unsigned char depth = 0) {
42 static unsigned char constexpr max_depth(10);
45 if (depth >= max_depth) {
49 else if (isJsonAtomic(
obj)) {
53 else if (PyList_Check(
obj.ptr()) || PyTuple_Check(
obj.ptr())) {
55 for (bp::ssize_t i = 0; i < bp::len(
obj); i++) {
56 ls.append(recursiveDictDump(
obj[i], depth + 1));
61 else if (PyDict_Check(
obj.ptr())) {
63 bp::list keyvals = bp::extract<bp::dict>(
obj)().items();
64 for (bp::ssize_t i = 0; i < bp::len(keyvals); i++) {
65 bp::object key = keyvals[i][0];
66 bp::object val = keyvals[i][1];
67 d[key] = recursiveDictDump(val, depth + 1);
72 else if (PyObject_HasAttrString(
obj.ptr(),
"__dict__")) {
73 bp::dict
d = bp::extract<bp::dict>(
obj.attr(
"__dict__"));
74 ret = recursiveDictDump(d, depth);
91 boost::python::object rep;
94 rep = boost::python::str(
"");
97 else if (boost::python::extract<std::string>(
obj).check()) {
103 boost::python::object json = boost::python::import(
"json");
105 rep = json.attr(
"dumps")(
obj);
108 catch (boost::python::error_already_set
const &) {
110 boost::python::object dict = recursiveDictDump(
obj);
112 rep = json.attr(
"dumps")(dict);
113 }
catch (boost::python::error_already_set
const &) {
115 rep = boost::python::str(
"<unrepresentable object>");
119 return boost::python::extract<std::string>(rep);
147namespace
Mantid::PythonInterface {
165 }
catch (std::invalid_argument
const &except) {
175std::string PythonObjectProperty::setValue(std::string
const &
value) {
179 boost::python::object newVal;
182 boost::python::object json = boost::python::import(
"json");
183 newVal = json.attr(
"loads")(
value);
186 catch (boost::python::error_already_set
const &) {
189 newVal = boost::python::str(
value.c_str());
190 }
catch (boost::python::error_already_set
const &) {
192 return "Failed to interpret string as JSON or string property: " +
value;
199 }
catch (std::invalid_argument
const &except) {
211std::string PythonObjectProperty::setValueFromJson(
const Json::Value &json) {
212 std::string jsonstr = JsonHelpers::jsonToString(json);
213 return setValue(jsonstr);
216std::string PythonObjectProperty::setDataItem(
const std::shared_ptr<Kernel::DataItem> &) {
217 throw NotImplementedError(
"PythonObjectProperty::setDataItem(const std::shared_ptr<Kernel::DataItem> &)");
224bool PythonObjectProperty::isDefault()
const {
return m_value.is_none(); }
const std::string & m_value
double value
The value of the point.
double obj
the value of the quadratic function
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Marks code as not implemented yet.
The concrete, templated class for properties.
Defines a structure for acquiring/releasing the Python GIL using the RAII pattern.
std::string toString(const T &value)
Convert values to strings.
boost::python::object PythonObject
MANTID_KERNEL_DLL::Json::Value encodeAsJson(const OptionalBool &)
Encode an OptionalBool as a Json::Value.
std::string toPrettyString(const T &value, size_t maxLength=0, bool collapseLists=true)
Convert values to pretty strings.
bool isNone(const PyObject *ptr)
Helper class which provides the Collimation Length for SANS instruments.