本文整理汇总了C++中PyDict_Keys函数的典型用法代码示例。如果您正苦于以下问题:C++ PyDict_Keys函数的具体用法?C++ PyDict_Keys怎么用?C++ PyDict_Keys使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyDict_Keys函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _scrap_get_types
/*
* Gets the currently available types from the active clipboard.
*/
static PyObject*
_scrap_get_types (PyObject *self, PyObject *args)
{
int i = 0;
char **types;
PyObject *list;
PyObject *tmp;
PYGAME_SCRAP_INIT_CHECK ();
if (!pygame_scrap_lost ())
{
switch (_currentmode)
{
case SCRAP_SELECTION:
return PyDict_Keys (_selectiondata);
case SCRAP_CLIPBOARD:
default:
return PyDict_Keys (_clipdata);
}
}
list = PyList_New (0);
types = pygame_scrap_get_types ();
if (!types)
return list;
while (types[i] != NULL)
{
tmp = PyString_FromString (types[i]);
PyList_Append (list, tmp);
Py_DECREF (tmp);
i++;
}
return list;
}
示例2: castToPyObject
configParserStruct::pythonParser::containerForVariables configParserStruct::pythonParser::listOfVariables( void *Object )
{
containerForVariables Result;
if ( Object == NULL )
return Result;
PyObject *Dict = castToPyObject(Object);
if ( ! PyDict_Check( Dict ) )
return Result;
PyObject *KeysList = PyDict_Keys( Dict );
Py_ssize_t KeysListSize = PyList_Size( KeysList );
for ( ptrdiff_t Index = 0; Index < static_cast<ptrdiff_t>(KeysListSize); Index++ )
{
PyObject *Key = PyList_GetItem( KeysList, Index );
PyObject *KeyRepr = PyObject_Repr( Key );
std::string KeyString = dequoteString( PyString_AsString( KeyRepr ), "'" );
PyObject *Item = PyDict_GetItem( Dict, Key );
if ( PyDict_Check(Item) )
{
containerForVariables ListOfItemsinSubDict = listOfVariables( Item );
for ( containerForVariables::const_iterator i = ListOfItemsinSubDict.begin(); i != ListOfItemsinSubDict.end(); ++i )
Result.insert( KeyString + structSeparator() + *i );
} else {
Result.insert( KeyString );
}
}
return Result;
}
示例3: checkDictKeys
//----------------------------------------------------------------------------
// Return set(dict.keys()).issubset(set([vkeys, ...])
// keys of dict must be string or unicode in Py2 and string in Py3!
// Parameters:
// dict - the Python dictionary object to be checked
// vkeys - a null-terminated list of keys (char *)
//----------------------------------------------------------------------------
int checkDictKeys(PyObject *dict, const char *vkeys, ...)
{
int i, j, rc;
PyObject *dkeys = PyDict_Keys(dict); // = dict.keys()
if (!dkeys) return 0; // no valid dictionary
j = PySequence_Size(dkeys); // len(dict.keys())
PyObject *validkeys = PyList_New(0); // make list of valid keys
va_list ap; // def var arg list
va_start(ap, vkeys); // start of args
while (vkeys != 0) // reached end yet?
{ // build list of valid keys to check against
#if PY_MAJOR_VERSION < 3
PyList_Append(validkeys, PyBytes_FromString(vkeys)); // Python 2
#else
PyList_Append(validkeys, PyUnicode_FromString(vkeys)); // python 3
#endif
vkeys = va_arg(ap, const char *); // get next char string
}
va_end(ap); // end the var args loop
rc = 1; // prepare for success
for (i = 0; i < j; i++)
{ // loop through dictionary keys
if (!PySequence_Contains(validkeys, PySequence_GetItem(dkeys, i)))
{
rc = 0;
break;
}
}
Py_DECREF(validkeys);
Py_DECREF(dkeys);
return rc;
}
示例4: dump_ret_msg
/**
* parse the return PyObject and store the result into task_ret_pool
*/
static void
dump_ret_msg(PyObject *pRet)
{
if ( pRet == NULL) {
throw std::runtime_error("pycall failed");
}
char *ret_err_msg;
int rsize = PyDict_Size(pRet);
int taskid;
download_ret_t dr_ret; // down return mesg structure
PyObject *pret_keys_list = PyDict_Keys(pRet);
pthread_mutex_lock(&retmtx);
for (int i = 0; i < rsize; i++) {
PyObject *pkey_item = PyList_GetItem(pret_keys_list, i);
PyObject *pvalue_tuple = PyDict_GetItem(pRet, pkey_item);
PyObject *pret_flag = PyTuple_GetItem(pvalue_tuple, 0);
PyObject *perr_msg = PyTuple_GetItem(pvalue_tuple, 1);
taskid = PyInt_AsLong(pkey_item);
dr_ret.ret_flag = PyInt_AsLong(pret_flag);
ret_err_msg = PyString_AsString(perr_msg);
strncpy(dr_ret.err_msg, ret_err_msg, NAME_LEN);
task_ret_pool[taskid] = dr_ret;
}
pthread_mutex_unlock(&retmtx);
}
示例5: PyDict_Keys
std::map<std::string, std::string> PythonTools::dtom( PyObject* d )
{
std::map<std::string, std::string> results;
if ( d != NULL && PyDict_Check( d ) )
{
PyObject* keyList = PyDict_Keys( d );
PyObject* curKey;
PyObject* curVal;
for ( int i = 0; i < PyList_Size( keyList ); i++ )
{
curKey = PyList_GetItem( keyList, i );
curVal = PyDict_GetItem( d, curKey );
char* valstr = PyString_AsString( curVal );
if ( valstr == NULL )
gravUtil::logWarning( "PythonTools::dtom(): "
"value not a string\n" );
char* keystr = PyString_AsString( curKey );
if ( keystr == NULL )
gravUtil::logWarning( "PythonTools::dtom(): "
"key not a string\n" );
if ( keystr != NULL && valstr != NULL )
results[ keystr ] = valstr;
}
}
return results;
}
示例6: resulttuple_dir
/**
* Extends __dir__ with the extra attributes accessible through
* resulttuple_getattro()
*/
static PyObject *
resulttuple_dir(PyObject *self)
{
PyObject *mapping_attr;
PyObject *items = NULL;
PyObject *mapping = NULL;
PyObject *mapping_values = NULL;
PyObject *result = NULL;
mapping_attr = PYGLIB_PyUnicode_FromString (tuple_indices_key);
mapping = PyTuple_Type.tp_getattro (self, mapping_attr);
Py_DECREF (mapping_attr);
if (mapping == NULL)
goto error;
items = PyObject_Dir ((PyObject*)self->ob_type);
if (items == NULL)
goto error;
mapping_values = PyDict_Keys (mapping);
if (mapping_values == NULL)
goto error;
result = PySequence_InPlaceConcat (items, mapping_values);
error:
Py_XDECREF (items);
Py_XDECREF (mapping);
Py_XDECREF (mapping_values);
return result;
}
示例7: dictkeys
int
dictkeys(const int handle)
{
PyObject *v;
PyObject *obj = getObjectFromHandle(handle);
v = PyDict_Keys(obj);
return newHandle(v);
}
示例8: PyErr_NoMemory
static char *build_headers_string(PyObject *dict)
{
Py_ssize_t i, dict_len, buf_pos, buf_size, req_space, free_space;
PyObject *keys, *key_ob;
char *key, *value, *header_s;
const char *header_format = "%s: %s\r\n";
buf_size = 512;
buf_pos = 0;
if ((header_s = (char *) malloc(buf_size * sizeof(char))) == NULL) {
PyErr_NoMemory();
return NULL;
}
if (dict == NULL || dict == Py_None) {
sprintf(header_s, "%s", "");
return header_s;
}
if (!PyDict_Check(dict)) {
PyErr_SetString(PyExc_TypeError, "Headers must be a dict");
return NULL;
}
memset(header_s, '\0', (size_t) buf_size);
if ((keys = PyDict_Keys(dict)) == NULL) {
PyErr_SetString(PyExc_TypeError, "Key must not be null.");
free(header_s);
return NULL;
}
dict_len = PyList_Size(keys);
for (i = 0; i < dict_len; ++i) {
key_ob = PyList_GetItem(keys, i);
if ((key = PyString_AsString(key_ob)) == NULL) {
free(header_s);
return NULL;
}
if ((value = PyString_AsString(PyDict_GetItem(dict, key_ob))) == NULL) {
free(header_s);
return NULL;
}
//Make sure header_s has enough space
req_space = strlen(key) + strlen(value) + strlen(header_format);
free_space = buf_size - buf_pos;
while ((req_space + 1) > free_space) { // +1 to be sure final '\0' fits
if ((header_s = (char *) realloc(header_s, buf_size * 2)) == NULL) {
PyErr_NoMemory();
free(header_s);
return NULL;
}
buf_size *= 2;
free_space = buf_size - buf_pos;
}
sprintf(header_s + buf_pos, header_format, key, value);
buf_pos += req_space;
}
header_s[buf_pos + 1] = '\0';
return header_s;
}
示例9: print_dict
static void print_dict(PyObject *dict) {
PyObject *keys = PyDict_Keys(dict);
for (Py_ssize_t i = 0; i < PyList_Size(keys); ++i) {
PyObject *key = PyList_GetItem(keys, i);
debug("\t%s", PyUnicode_AsUTF8(key));
Py_DECREF(key);
}
Py_DECREF(keys);
}
示例10: py2c
static std::map<K,V> py2c(PyObject * ob) {
pyref keys = PyDict_Keys(ob);
pyref values = PyDict_Values(ob);
std::map<K,V> res;
int len = PyDict_Size(ob);
for (int i = 0; i < len; i++)
res.emplace(py_converter<K>::py2c(PyList_GET_ITEM((PyObject*)keys, i)), //borrowed ref
py_converter<V>::py2c(PyList_GET_ITEM((PyObject*)values, i))); //borrowed ref
return res;
}
示例11: printDict
void printDict(PyObject* obj) {
if (!PyDict_Check(obj))
return;
PyObject *k, *keys;
keys = PyDict_Keys(obj);
for (int i = 0; i < PyList_GET_SIZE(keys); i++) {
k = PyList_GET_ITEM(keys, i);
char* c_name = PyString_AsString(k);
printf("%s\n", c_name);
}
}
示例12: printDict
void printDict(PyObject* obj) {
if (!PyDict_Check(obj))
return;
PyObject *k, *keys;
keys = PyDict_Keys(obj);// get keys of the dictionary,list output
for (int i = 0; i < PyList_GET_SIZE(keys); i++) {
k = PyList_GET_ITEM(keys, i);
char* c_name = PyString_AsString(k);
printf("%s/n", c_name);
}
}
示例13: list
list dict_base::keys() const
{
if (check_exact(this))
{
return list(detail::new_reference(
PyDict_Keys(this->ptr())));
}
else
{
return assume_list(this->attr("keys")());
}
}
示例14: py_set_colormap
static PyObject* py_set_colormap(PyObject *self, PyObject *args,
PyObject *kwargs) {
const char *kwnames[] = {"name", "pairs", NULL};
PyObject *newdict, *keys, *vals;
PyObject *result = NULL;
VMDApp *app;
char *name;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO!:color.set_colormap",
(char**) kwnames, &name, &PyDict_Type,
&newdict))
return NULL;
if (!(app = get_vmdapp()))
return NULL;
keys = PyDict_Keys(newdict);
vals = PyDict_Values(newdict);
for (int i=0; i<PyList_Size(keys); i++) {
char *keyname = as_charptr(PyList_GetItem(keys, i));
char *valname = as_charptr(PyList_GetItem(vals, i));
if (!keyname || !valname || PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "set_colormap dictionary invalid");
goto cleanup;
}
if (!app->color_change_name(name, keyname, valname)) {
PyErr_SetString(PyExc_ValueError,
"Invalid color category or item specified");
goto cleanup;
}
}
result = Py_None;
// Getting the keys and values from the dictionary makes a new reference.
// We tell Python we're done with it so as to not leak memory.
// This needs to happen even if there was a problem setting color, which is
// why we don't return NULL from the error checking statements above.
cleanup:
Py_DECREF(keys);
Py_DECREF(vals);
Py_XINCREF(result);
return result;
}
示例15: ops
//=============================================================================
// METHOD: SPELLconfigDict::reset()
//=============================================================================
void SPELLconfigDict::reset( PyObject* dict )
{
m_values.clear();
if (dict)
{
SPELLsafePythonOperations ops ("SPELLconfigDict::reset()");
SPELLpyHandle keys = PyDict_Keys(dict);
unsigned int numKeys = PyList_Size(keys.get());
for( unsigned int idx = 0; idx < numKeys; idx++ )
{
PyObject* key = PyList_GetItem( keys.get(), idx );
PyObject* value = PyDict_GetItem( dict, key );
m_values.insert( std::make_pair( PYSSTR(key), SPELLpyValue(value) ));
}
}
}