Mercurial > pytave
changeset 43:31df83060183
avoid duplicate conversions in python dict->octave map
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 25 May 2009 12:53:30 +0200 |
parents | c0851ec35936 |
children | f237eb38e9c3 |
files | ChangeLog python_to_octave.cc |
diffstat | 2 files changed, 31 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Mon May 25 11:19:50 2009 +0200 +++ b/ChangeLog Mon May 25 12:53:30 2009 +0200 @@ -1,3 +1,8 @@ +2009-05-25 Jaroslav Hajek <highegg@gmail.com> + + * python_to_octave.cc (pydict_to_octmap): Save key and val in an + array, to avoid doing duplicate conversions. + 2009-05-25 Jaroslav Hajek <highegg@gmail.com> * pytave.cc (init): Add parameter; only display Octave banner if
--- a/python_to_octave.cc Mon May 25 11:19:50 2009 +0200 +++ b/python_to_octave.cc Mon May 25 12:53:30 2009 +0200 @@ -215,12 +215,35 @@ bool has_dimensions = false; + Array<octave_value> vals (length); + Array<std::string> keys (length); + for(octave_idx_type i = 0; i < length; i++) { - octave_value val; + + std::string& key = keys(i); boost::python::tuple tuple = boost::python::extract<boost::python::tuple>(list[i])(); + boost::python::extract<std::string> str(tuple[0]); + if(!str.check()) { + throw object_convert_exception( + string("Can not convert key of type ") + + PyEval_GetFuncName(boost::python::object(tuple[0]).ptr()) + + PyEval_GetFuncDesc(boost::python::object(tuple[0]).ptr()) + + " to a structure field name. Field names must be strings."); + } + + key = str(); + + if (!valid_identifier(key)) { + throw object_convert_exception( + string("Can not convert key `") + key + "' to a structure " + "field name. Field names must be valid Octave identifiers."); + } + + octave_value& val = vals(i); + pyobj_to_octvalue(val, tuple[1]); if(val.is_cell()) { @@ -251,31 +274,9 @@ Octave_map map = Octave_map(dims); for(octave_idx_type i = 0; i < length; i++) { - octave_value val; - std::string key; - boost::python::tuple tuple = - boost::python::extract<boost::python::tuple>(list[i])(); - - boost::python::extract<std::string> str(tuple[0]); - if(!str.check()) { - throw object_convert_exception( - string("Can not convert key of type ") - + PyEval_GetFuncName(boost::python::object(tuple[0]).ptr()) - + PyEval_GetFuncDesc(boost::python::object(tuple[0]).ptr()) - + " to a structure field name. Field names must be strings."); - } - - key = str(); - - if (!valid_identifier(key)) { - throw object_convert_exception( - string("Can not convert key `") + key + "' to a structure " - "field name. Field names must be valid Octave identifiers."); - } - - // FIXME: Second time around we convert exactly the same object - pyobj_to_octvalue(val, tuple[1]); + std::string& key = keys(i); + octave_value val = vals(i); if(!val.is_cell()) { map.assign(key, Cell(dims, val));