changeset 346:b1eff49bd139

pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62) * @pyobject/pyobject.m: Add conversion method struct, thin wrapper around __py_struct_from_dict__ internal function. * @pyobject/struct.m: Delete previous faulty implementation.
author Mike Miller <mtmiller@octave.org>
date Tue, 16 Aug 2016 16:08:43 -0700
parents baff3b90dcb1
children 7f039ffe501b
files @pyobject/pyobject.m @pyobject/struct.m
diffstat 2 files changed, 18 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/@pyobject/pyobject.m	Tue Aug 16 15:34:05 2016 -0700
+++ b/@pyobject/pyobject.m	Tue Aug 16 16:08:43 2016 -0700
@@ -139,6 +139,10 @@
       endif
     endfunction
 
+    function y = struct (x)
+      y = __py_struct_from_dict__ (x);
+    endfunction
+
     function vargout = help (x)
       idx = struct ("type", ".", "subs", "__doc__");
       s = subsref (x, idx);
@@ -341,6 +345,20 @@
 %!error double (pyobject ())
 %!error double (pyeval ("[1, 2, 3]"))
 
+## Test conversion method pyobject.struct
+%!assert (struct (pycall ("dict")), struct ())
+%!assert (struct (pyobject (struct ())), struct ())
+%!test
+%! a = struct ("a", 1, "b", 2, "three", 3);
+%! b = pyobject (a);
+%! c = struct (b);
+%! assert (c, a)
+%!test
+%! a = struct ("a", 1, "b", 2, "three", 3);
+%! b = pycall ("dict", pyargs ("a", 1, "b", 2, "three", 3));
+%! c = struct (b);
+%! assert (c, a)
+
 ## Octave fails to resolve function overloads via function handles
 %!xtest
 %! fn = @double;
--- a/@pyobject/struct.m	Tue Aug 16 15:34:05 2016 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,71 +0,0 @@
-## Copyright (C) 2016 Mike Miller
-##
-## This file is part of Pytave
-##
-## Pytave is free software; you can redistribute it and/or modify
-## it under the terms of the GNU General Public License as published
-## by the Free Software Foundation; either version 3 of the License,
-## or (at your option) any later version.
-##
-## This software is distributed in the hope that it will be useful,
-## but WITHOUT ANY WARRANTY; without even the implied warranty
-## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
-## the GNU General Public License for more details.
-##
-## You should have received a copy of the GNU General Public
-## License along with this software; see the file COPYING.
-## If not, see <http://www.gnu.org/licenses/>.
-
-## -*- texinfo -*-
-## @documentencoding UTF-8
-## @defmethod @@pyobject struct (@var{x})
-## Convert a Python object to an Octave struct containing copies of its
-## public properties.
-##
-## Example:
-## @example
-## @group
-## os = pycall ("__import__", "os");
-## struct (os)
-##   @result{} scalar structure containing the fields:
-##       ...
-##       ...
-##       environ = ...
-##       name = ...
-##       ...
-## @end group
-## @end example
-##
-## @seealso{struct, @@pyobject/fieldnames}
-## @end defmethod
-
-
-function retval = struct (x)
-
-  retval = struct ();
-  names = fieldnames (x);
-  for i = 1:numel (names)
-    retval = setfield (retval, names{i}, pycall ("getattr", x, names{i}));
-  endfor
-
-endfunction
-
-
-%!test
-%! os = pycall ("__import__", "os");
-%! rv = struct (os);
-%! assert (isstruct (rv))
-%! assert (numfields (rv) >= 10)
-%! assert (fieldnames (rv), fieldnames (os))
-%! assert (isfield (rv, "curdir"))
-%! assert (isfield (rv, "devnull"))
-%! assert (isfield (rv, "environ"))
-%! assert (isfield (rv, "name"))
-%! assert (isfield (rv, "pathsep"))
-%! assert (isfield (rv, "sep"))
-
-%!test
-%! rv = struct (pyeval ("__builtins__"));
-%! assert (rv.False, false)
-%! assert (rv.True, true)
-%! assert (isequal (rv.None, pyeval ("None")))