本文整理汇总了C++中py::Dict::hasKey方法的典型用法代码示例。如果您正苦于以下问题:C++ Dict::hasKey方法的具体用法?C++ Dict::hasKey怎么用?C++ Dict::hasKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类py::Dict
的用法示例。
在下文中一共展示了Dict::hasKey方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
//------------------------------------------------------------
//
// DictWrapper
//
//------------------------------------------------------------
DictWrapper::DictWrapper( Py::Dict result_wrappers, const std::string &wrapper_name )
: m_wrapper_name( wrapper_name )
, m_have_wrapper( false )
, m_wrapper()
{
if( result_wrappers.hasKey( wrapper_name ) )
{
m_wrapper = result_wrappers[ wrapper_name ];
m_have_wrapper = true;
}
}
示例2: module
QMap<QString, CallTip> CallTipsList::extractTips(const QString& context) const
{
Base::PyGILStateLocker lock;
QMap<QString, CallTip> tips;
if (context.isEmpty())
return tips;
try {
Py::Module module("__main__");
Py::Dict dict = module.getDict();
#if 0
QStringList items = context.split(QLatin1Char('.'));
QString modname = items.front();
items.pop_front();
if (!dict.hasKey(std::string(modname.toLatin1())))
return tips; // unknown object
// get the Python object we need
Py::Object obj = dict.getItem(std::string(modname.toLatin1()));
while (!items.isEmpty()) {
QByteArray name = items.front().toLatin1();
std::string attr = name.constData();
items.pop_front();
if (obj.hasAttr(attr))
obj = obj.getAttr(attr);
else
return tips;
}
#else
// Don't use hasattr & getattr because if a property is bound to a method this will be executed twice.
PyObject* code = Py_CompileString(static_cast<const char*>(context.toLatin1()), "<CallTipsList>", Py_eval_input);
if (!code) {
PyErr_Clear();
return tips;
}
PyObject* eval = 0;
if (PyCode_Check(code)) {
eval = PyEval_EvalCode(reinterpret_cast<PyCodeObject*>(code), dict.ptr(), dict.ptr());
}
Py_DECREF(code);
if (!eval) {
PyErr_Clear();
return tips;
}
Py::Object obj(eval, true);
#endif
// Checks whether the type is a subclass of PyObjectBase because to get the doc string
// of a member we must get it by its type instead of its instance otherwise we get the
// wrong string, namely that of the type of the member.
// Note: 3rd party libraries may use their own type object classes so that we cannot
// reliably use Py::Type. To be on the safe side we should use Py::Object to assign
// the used type object to.
//Py::Object type = obj.type();
Py::Object type(PyObject_Type(obj.ptr()), true);
Py::Object inst = obj; // the object instance
union PyType_Object typeobj = {&Base::PyObjectBase::Type};
union PyType_Object typedoc = {&App::DocumentObjectPy::Type};
union PyType_Object basetype = {&PyBaseObject_Type};
if (PyObject_IsSubclass(type.ptr(), typedoc.o) == 1) {
// From the template Python object we don't query its type object because there we keep
// a list of additional methods that we won't see otherwise. But to get the correct doc
// strings we query the type's dict in the class itself.
// To see if we have a template Python object we check for the existence of supportedProperties
if (!type.hasAttr("supportedProperties")) {
obj = type;
}
}
else if (PyObject_IsSubclass(type.ptr(), typeobj.o) == 1) {
obj = type;
}
else if (PyInstance_Check(obj.ptr())) {
// instances of old style classes
PyInstanceObject* inst = reinterpret_cast<PyInstanceObject*>(obj.ptr());
PyObject* classobj = reinterpret_cast<PyObject*>(inst->in_class);
obj = Py::Object(classobj);
}
else if (PyObject_IsInstance(obj.ptr(), basetype.o) == 1) {
// New style class which can be a module, type, list, tuple, int, float, ...
// Make sure it's not a type objec
union PyType_Object typetype = {&PyType_Type};
if (PyObject_IsInstance(obj.ptr(), typetype.o) != 1) {
// this should be now a user-defined Python class
// http://stackoverflow.com/questions/12233103/in-python-at-runtime-determine-if-an-object-is-a-class-old-and-new-type-instan
if (Py_TYPE(obj.ptr())->tp_flags & Py_TPFLAGS_HEAPTYPE) {
obj = type;
}
}
}
// If we have an instance of PyObjectBase then determine whether it's valid or not
if (PyObject_IsInstance(inst.ptr(), typeobj.o) == 1) {
Base::PyObjectBase* baseobj = static_cast<Base::PyObjectBase*>(inst.ptr());
const_cast<CallTipsList*>(this)->validObject = baseobj->isValid();
}
else {
// PyObject_IsInstance might set an exception
PyErr_Clear();
//.........这里部分代码省略.........
示例3: module
QMap<QString, CallTip> CallTipsList::extractTips(const QString& context) const
{
Base::PyGILStateLocker lock;
QMap<QString, CallTip> tips;
if (context.isEmpty())
return tips;
try {
QStringList items = context.split(QLatin1Char('.'));
Py::Module module("__main__");
Py::Dict dict = module.getDict();
QString modname = items.front();
items.pop_front();
if (!dict.hasKey(std::string(modname.toAscii())))
return tips; // unknown object
// get the Python object we need
Py::Object obj = dict.getItem(std::string(modname.toAscii()));
while (!items.isEmpty()) {
QByteArray name = items.front().toAscii();
std::string attr = name.constData();
items.pop_front();
if (obj.hasAttr(attr))
obj = obj.getAttr(attr);
else
return tips;
}
// Checks whether the type is a subclass of PyObjectBase because to get the doc string
// of a member we must get it by its type instead of its instance otherwise we get the
// wrong string, namely that of the type of the member.
// Note: 3rd party libraries may use their own type object classes so that we cannot
// reliably use Py::Type. To be on the safe side we should use Py::Object to assign
// the used type object to.
//Py::Object type = obj.type();
Py::Object type(PyObject_Type(obj.ptr()), true);
Py::Object inst = obj; // the object instance
union PyType_Object typeobj = {&Base::PyObjectBase::Type};
union PyType_Object typedoc = {&App::DocumentObjectPy::Type};
if (PyObject_IsSubclass(type.ptr(), typedoc.o) == 1) {
// From the template Python object we don't query its type object because there we keep
// a list of additional methods that we won't see otherwise. But to get the correct doc
// strings we query the type's dict in the class itself.
// To see if we have a template Python object we check for the existence of supportedProperties
if (!type.hasAttr("supportedProperties")) {
obj = type;
}
}
else if (PyObject_IsSubclass(type.ptr(), typeobj.o) == 1) {
obj = type;
}
// If we have an instance of PyObjectBase then determine whether it's valid or not
if (PyObject_IsInstance(inst.ptr(), typeobj.o) == 1) {
Base::PyObjectBase* baseobj = static_cast<Base::PyObjectBase*>(inst.ptr());
const_cast<CallTipsList*>(this)->validObject = baseobj->isValid();
}
else {
// PyObject_IsInstance might set an exception
PyErr_Clear();
}
Py::List list(PyObject_Dir(obj.ptr()), true);
// If we derive from PropertyContainerPy we can search for the properties in the
// C++ twin class.
union PyType_Object proptypeobj = {&App::PropertyContainerPy::Type};
if (PyObject_IsSubclass(type.ptr(), proptypeobj.o) == 1) {
// These are the attributes of the instance itself which are NOT accessible by
// its type object
extractTipsFromProperties(inst, tips);
}
// If we derive from App::DocumentPy we have direct access to the objects by their internal
// names. So, we add these names to the list, too.
union PyType_Object appdoctypeobj = {&App::DocumentPy::Type};
if (PyObject_IsSubclass(type.ptr(), appdoctypeobj.o) == 1) {
App::DocumentPy* docpy = (App::DocumentPy*)(inst.ptr());
App::Document* document = docpy->getDocumentPtr();
// Make sure that the C++ object is alive
if (document) {
std::vector<App::DocumentObject*> objects = document->getObjects();
Py::List list;
for (std::vector<App::DocumentObject*>::iterator it = objects.begin(); it != objects.end(); ++it)
list.append(Py::String((*it)->getNameInDocument()));
extractTipsFromObject(inst, list, tips);
}
}
// If we derive from Gui::DocumentPy we have direct access to the objects by their internal
// names. So, we add these names to the list, too.
union PyType_Object guidoctypeobj = {&Gui::DocumentPy::Type};
if (PyObject_IsSubclass(type.ptr(), guidoctypeobj.o) == 1) {
Gui::DocumentPy* docpy = (Gui::DocumentPy*)(inst.ptr());
if (docpy->getDocumentPtr()) {
App::Document* document = docpy->getDocumentPtr()->getDocument();
// Make sure that the C++ object is alive
if (document) {
std::vector<App::DocumentObject*> objects = document->getObjects();
Py::List list;
//.........这里部分代码省略.........