本文整理汇总了C++中boost::python::object::attr方法的典型用法代码示例。如果您正苦于以下问题:C++ object::attr方法的具体用法?C++ object::attr怎么用?C++ object::attr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::python::object
的用法示例。
在下文中一共展示了object::attr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: promptDlg
boost::python::object NotepadPlusWrapper::prompt(boost::python::object promptObj, boost::python::object title, boost::python::object initial)
{
PromptDialog promptDlg(m_hInst, m_nppHandle);
const char *cPrompt = NULL;
const char *cTitle = NULL;
const char *cInitial = NULL;
if (!promptObj | !promptObj.is_none())
cPrompt = (const char *)boost::python::extract<const char *>(promptObj.attr("__str__")());
if (!title | !title.is_none())
cTitle= (const char *)boost::python::extract<const char *>(title.attr("__str__")());
if (!initial | !initial.is_none())
cInitial = (const char *)boost::python::extract<const char *>(initial.attr("__str__")());
PromptDialog::PROMPT_RESULT result;
GILRelease release;
result = promptDlg.showPrompt(cPrompt, cTitle, cInitial);
release.reacquire();
if (PromptDialog::RESULT_OK == result)
{
return boost::python::str(promptDlg.getText());
}
else
{
return boost::python::object();
}
}
示例2: update
void update(boost::python::object source)
{
// See if we have a dictionary-like object.
if (py_hasattr(source, "items"))
{
return this->update(source.attr("items")());
}
if (!py_hasattr(source, "__iter__")) {
THROW_EX(ValueError, "Must provide a dictionary-like object to update()");
}
object iter = source.attr("__iter__")();
while (true) {
PyObject *pyobj = PyIter_Next(iter.ptr());
if (!pyobj) break;
if (PyErr_Occurred()) {
throw_error_already_set();
}
object obj = object(handle<>(pyobj));
tuple tup = extract<tuple>(obj);
std::string attr = extract<std::string>(tup[0]);
std::string value = extract<std::string>(tup[1]);
setitem(attr, value);
}
}
示例3: fromNumpyVector
/** Converts a one-dimensional numpy vector to eigen VectorXd. */
Eigen::VectorXd fromNumpyVector(py::object vec) {
assert(("Input numpy matrix is not one dimensional.", py::extract<int>(vec.attr("ndim"))==1));
vec = np_mod.attr("array")(vec, "float64");
int size = py::extract<int>(vec.attr("size"));
double* vec_data = getPointer<double>(vec);
return Eigen::Map<Eigen::VectorXd>(vec_data, size);
}
示例4: mat
static Eigen::MatrixXd fromNP(py::object arr) {
arr = np_mod.attr("array")(arr, "float64");
int rows = py::extract<int>(arr.attr("shape")[0]);
int cols = py::extract<int>(arr.attr("shape")[1]);
Eigen::MatrixXd mat(rows, cols);
double* p = getPointer<double>(arr);
memcpy(&mat(0,0), p, rows*cols*sizeof(double));
return mat;
}
示例5: sizeof
static void from2dArray(PointCloudT* cloud, py::object arr) {
arr = np_mod.attr("array")(arr, "float32");
int npoints = py::extract<int>(arr.attr("shape")[0]);
int floatfields = py::extract<int>(arr.attr("shape")[1]);
FAIL_IF_FALSE(floatfields == sizeof(T)/4);
cloud->resize(npoints);
float* p = getPointer<float>(arr);
memcpy(&cloud->points[0], p, npoints*floatfields*sizeof(float));
}
示例6: fromNumpy
/** Converts a two-dimensional numpy matrix to eigen MatrixXd. */
Eigen::MatrixXd fromNumpy(py::object mat) {
assert(("Input numpy matrix is not two dimensional.", py::extract<int>(mat.attr("ndim"))==2));
mat = np_mod.attr("array")(mat, "float64");
int rows = py::extract<int>(mat.attr("shape")[0]);
int cols = py::extract<int>(mat.attr("shape")[1]);
double* mat_data = getPointer<double>(mat);
return Eigen::Map<Eigen::MatrixXd>(mat_data, rows, cols);
}
示例7: memcpy
static void from3dArray(PointCloudT* cloud, py::object arr) {
arr = np_mod.attr("array")(arr, "float32");
int height = py::extract<int>(arr.attr("shape")[0]);
int width = py::extract<int>(arr.attr("shape")[1]);
int floatfields = py::extract<int>(arr.attr("shape")[2]);
FAIL_IF_FALSE(floatfields==sizeof(T)/4);
cloud->resize(width*height);
cloud->height = height;
cloud->width = width;
float* p = getPointer<float>(arr);
memcpy(&cloud->points[0], p, width*height*floatfields*sizeof(float));
}
示例8: logic_error
/** Constructor. dtype_name determines type of matrices created. */
numpy_converter( const char * dtype_name = "float64" )
{
PyObject* module = ::PyImport_Import( boost::python::object( "numpy" ).ptr() );
if( ! module )
{
throw std::logic_error( "Could not import numpy" );
}
numpy = boost::python::object( boost::python::handle<>( module ) );
array_type = numpy.attr( "ndarray" );
array_function = numpy.attr( "array" );
set_dtype( dtype_name );
}
示例9: setstate
static
void
setstate(boost::python::object w_obj, boost::python::tuple state)
{
using namespace boost::python;
world& w = extract<world&>(w_obj)();
if (len(state) != 2)
{
PyErr_SetObject(PyExc_ValueError,
("expected 2-item tuple in call to __setstate__; got %s"
% state).ptr()
);
throw_error_already_set();
}
// restore the object's __dict__
dict d = extract<dict>(w_obj.attr("__dict__"))();
d.update(state[0]);
// restore the internal state of the C++ object
long number = extract<long>(state[1]);
if (number != 42)
w.set_secret_number(number);
}
示例10: MaxMinPicks
// REVIEW: the poolSize can be pulled from the numeric array
RDKit::INT_VECT MaxMinPicks(MaxMinPicker *picker, python::object distMat,
int poolSize, int pickSize,
python::object firstPicks, int seed) {
if (pickSize >= poolSize) {
throw ValueErrorException("pickSize must be less than poolSize");
}
if (!PyArray_Check(distMat.ptr())) {
throw ValueErrorException("distance mat argument must be a numpy matrix");
}
PyArrayObject *copy;
copy = (PyArrayObject *)PyArray_ContiguousFromObject(distMat.ptr(),
PyArray_DOUBLE, 1, 1);
double *dMat = (double *)copy->data;
RDKit::INT_VECT firstPickVect;
for (unsigned int i = 0;
i < python::extract<unsigned int>(firstPicks.attr("__len__")()); ++i) {
firstPickVect.push_back(python::extract<int>(firstPicks[i]));
}
RDKit::INT_VECT res =
picker->pick(dMat, poolSize, pickSize, firstPickVect, seed);
Py_DECREF(copy);
return res;
}
示例11: extractor
T
read_or_default(const boost::python::object &o,
const char *key,
T default_value)
{
// Remember AttributeError for later comparison
// boost::python::object attributeError =
// boost::python::import("AttributeError");
try
{
const boost::python::object &o2 = o.attr(key);
boost::python::extract< T > extractor(o2);
if (extractor.check())
{
return extractor;
}
else
{
return default_value;
}
} catch (const boost::python::error_already_set &)
{
// https://misspent.wordpress.com/2009/10/11/
// boost-python-and-handling-python-exceptions/
PyObject *e, *v, *t;
PyErr_Fetch(&e, &v, &t);
return default_value;
}
return default_value;
}
示例12: setToSolution
void SystemMatrix::setToSolution(escript::Data& out, escript::Data& in,
boost::python::object& options) const
{
if (in.isComplex() || out.isComplex())
{
throw PasoException("SystemMatrix::setToSolution: complex arguments not supported.");
}
options.attr("resetDiagnostics")();
Options paso_options(options);
if (out.getDataPointSize() != getColumnBlockSize()) {
throw PasoException("solve: column block size does not match the number of components of solution.");
} else if (in.getDataPointSize() != getRowBlockSize()) {
throw PasoException("solve: row block size does not match the number of components of right hand side.");
} else if (out.getFunctionSpace() != getColumnFunctionSpace()) {
throw PasoException("solve: column function space and function space of solution don't match.");
} else if (in.getFunctionSpace() != getRowFunctionSpace()) {
throw PasoException("solve: row function space and function space of right hand side don't match.");
}
out.expand();
in.expand();
out.requireWrite();
in.requireWrite();
double* out_dp = out.getExpandedVectorReference(static_cast<escript::DataTypes::real_t>(0)).data();
double* in_dp = in.getExpandedVectorReference(static_cast<escript::DataTypes::real_t>(0)).data();
solve(out_dp, in_dp, &paso_options);
paso_options.updateEscriptDiagnostics(options);
}
示例13: fill_alpha3D_complex
void fill_alpha3D_complex(bp::object points, bp::object complex)
{
typedef std::map<AlphaSimplex3D::Vertex, unsigned> ASPointMap;
Delaunay3D Dt;
ASPointMap point_map;
unsigned i = 0;
for (bp::stl_input_iterator<bp::list> pt = points; pt != bp::stl_input_iterator<bp::list>(); ++pt)
{
double x = bp::extract<double>((*pt)[0]);
double y = bp::extract<double>((*pt)[1]);
double z = bp::extract<double>((*pt)[2]);
point_map[Dt.insert(Point(x,y,z))] = i++;
}
AlphaSimplex3D::SimplexSet simplices;
fill_simplex_set(Dt, simplices);
for (AlphaSimplex3D::SimplexSet::const_iterator cur = simplices.begin(); cur != simplices.end(); ++cur)
{
dp::SimplexVD s;
for (AlphaSimplex3D::VertexContainer::const_iterator vcur = cur->vertices().begin();
vcur != cur->vertices().end(); ++vcur)
s.add(point_map[*vcur]);
s.data() = bp::object(std::make_pair(cur->value(), !cur->attached())); // regular/critical rather than attached
complex.attr("append")(s);
}
}
示例14: PythonException
boost::python::object call_(const char* name,Args... args){
using namespace boost::python;
try {
return context_.attr(name)(args...);
}
catch(error_already_set const &){
PyObject *type,*value,*traceback;
PyErr_Fetch(&type,&value,&traceback);
// Construct a message
std::string message;
if(value){
extract<std::string> value_string(value);
// Check a string can be extracted from the PyObject
if(value_string.check()){
message += value_string() +":\n";
}
}
// There may not be a traceback (e.g. if a syntax error)
if(value and traceback){
handle<> type_handle(type);
handle<> value_handle(allow_null(value));
handle<> traceback_handle(allow_null(traceback));
object formatted_list = import("traceback").attr("format_exception")(type_handle,value_handle,traceback_handle);
for(int i=0;i<len(formatted_list);i++){
extract<std::string> line_string(formatted_list[i]);
// Check a string can be extracted from the PyObject
if(line_string.check()){
message += line_string();
}
}
}
throw PythonException(message);
}
}
示例15:
static void toCPP(boost::python::object o, ImmutableTreeMap<T1, T2>& outT)
{
outT = ImmutableTreeMap<T1, T2>();
boost::python::object it = o.attr("__iter__")();
while(1)
{
T1 t1;
T2 t2;
boost::python::object key;
try {
key = it.attr("next")();
}
catch(...)
{
PyErr_Clear();
return;
}
Converter<T1>::toCPP(key, t1);
Converter<T2>::toCPP(o[key], t2);
outT = outT + make_pair(t1, t2);
}
}