[C++-sig] 'iteritems', 'iterkeys' and 'itervalues' in dict

Per Ghosh per at gorep.se
Fri Apr 11 02:26:20 CEST 2008


I am new to boost.python but this is something that worked for me, Is there a
more simple way to manage dicts and key/value pairs in the dict?

void CPyFormat::Add( boost::python::dict& rdictValues ) 
{
   gd_std::wstring stringKey, stringValue;
   boost::python::object objectKey, objectValue;
   const boost::python::object objectKeys = rdictValues.iterkeys();
   const boost::python::object objectValues = rdictValues.itervalues();
   unsigned long ulCount = boost::python::extract<unsigned
long>(rdictValues.attr("__len__")());
   for( unsigned long u = 0; u < ulCount; u++ )
   {
      objectKey = objectKeys.attr( "next" )();
      objectValue = objectValues.attr( "next" )();

      char chCheckKey = objectKey.ptr()->ob_type->tp_name[0];
      // simple check
      if( chCheckKey != 's' && chCheckKey != 'u' ) throw std::runtime_error(
"Unknown key type" );
      
      if( chCheckKey == 's' ) stringKey = boost::python::extract<const
char*>(objectKey);
      else stringKey = boost::python::extract<std::wstring>(objectKey);

      char chCheckValue = objectValue.ptr()->ob_type->tp_name[0];
      // simple check
      if( chCheckValue != 's' && chCheckValue != 'u' ) throw
std::runtime_error( "Unknown value type" );

      if( chCheckValue == 's' ) stringValue = boost::python::extract<const
char*>(objectValue);
      else stringValue = boost::python::extract<std::wstring>(objectValue);


      m_vectorProperties.push_back( std::pair<std::wstring,std::wstring>(
stringKey.c_str(), stringValue.c_str() ) );
   }
}




Nindi Singh wrote:
> 
> Apolgies for not putting the subject line in my last email.
> 
> In the Boost.Python dict class there are methods 'iteritems', 'iterkeys'
> and 'itervalues' all returning objects.
> How exactly are you supposed to use these objects (iterators) ?
> 
> 
> ___________________________________________________________ 
> Inbox full of spam? Get leading spam protection and 1GB storage with All
> New Yahoo! Mail. http://uk.docs.yahoo.com/nowyoucan.html
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 
> 

-- 
View this message in context: http://www.nabble.com/-C%2B%2B-sig--%27iteritems%27%2C-%27iterkeys%27-and-%27itervalues%27--in-dict-tp6824331p16622870.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.




More information about the Cplusplus-sig mailing list