Mercurial > pytave
annotate @pyobject/fieldnames.m @ 301:2047beb75f5e
Fix fieldnames cell array orientation
* @pyobject/fieldnames.m: Fix orientation.
author | Colin Macdonald <cbm@m.fsf.org> |
---|---|
date | Wed, 03 Aug 2016 21:08:06 -0700 |
parents | df1bddece9d5 |
children |
rev | line source |
---|---|
249
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2016 Mike Miller |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
2 ## |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
3 ## This file is part of Pytave |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
4 ## |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
5 ## Pytave is free software; you can redistribute it and/or modify |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; either version 3 of the License, |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
8 ## or (at your option) any later version. |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
9 ## |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
10 ## This software is distributed in the hope that it will be useful, |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
12 ## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
13 ## the GNU General Public License for more details. |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
14 ## |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
16 ## License along with this software; see the file COPYING. |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
17 ## If not, see <http://www.gnu.org/licenses/>. |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
18 |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
20 ## @documentencoding UTF-8 |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
21 ## @defmethod @@pyobject fieldnames (@var{x}) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
22 ## List the properties of a Python object. |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
23 ## |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
24 ## Returns a cell array of strings containing the names of the properties |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
25 ## of @var{x}. |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
26 ## |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
27 ## Example: |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
28 ## @example |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
29 ## @group |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
30 ## pyexec ("import sys") |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
31 ## sys = pyeval ("sys"); |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
32 ## fieldnames (sys) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
33 ## @result{} ans = |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
34 ## @{ |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
35 ## [1,1] = ... |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
36 ## [2,1] = ... |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
37 ## ... = path |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
38 ## ... = version |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
39 ## ... |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
40 ## @} |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
41 ## @end group |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
42 ## @end example |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
43 ## |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
44 ## @seealso{fieldnames, @@pyobject/methods} |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
45 ## @end defmethod |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
46 |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
47 |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
48 function names = fieldnames (x) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
49 |
284
6c133bcac33a
use pyobject instead of accessing the InOct dict
Colin Macdonald <cbm@m.fsf.org>
parents:
249
diff
changeset
|
50 cmd = pyeval (["lambda x: [a for a in dir(x)" ... |
6c133bcac33a
use pyobject instead of accessing the InOct dict
Colin Macdonald <cbm@m.fsf.org>
parents:
249
diff
changeset
|
51 " if not callable(getattr(x, a))" ... |
6c133bcac33a
use pyobject instead of accessing the InOct dict
Colin Macdonald <cbm@m.fsf.org>
parents:
249
diff
changeset
|
52 " and not isinstance(getattr(x, a), __import__('types').ModuleType)" ... |
6c133bcac33a
use pyobject instead of accessing the InOct dict
Colin Macdonald <cbm@m.fsf.org>
parents:
249
diff
changeset
|
53 " and not a.startswith('_')]"]); |
249
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
54 |
289
49832ca978fd
Use new subsref indexing to fix the failing tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
284
diff
changeset
|
55 names_obj = pycall (cmd, x); |
298
df1bddece9d5
Use subsref in "cell" method for casting to cell
Colin Macdonald <cbm@m.fsf.org>
parents:
292
diff
changeset
|
56 names = cellfun (@char, cell (names_obj), "uniformoutput", false); |
301
2047beb75f5e
Fix fieldnames cell array orientation
Colin Macdonald <cbm@m.fsf.org>
parents:
298
diff
changeset
|
57 names = names(:); |
249
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
58 |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
59 endfunction |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
60 |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
61 |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
62 %!test |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
63 %! sys = pycall ("__import__", "sys"); |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
64 %! lst = fieldnames (sys); |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
65 %! assert (iscell (lst)) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
66 %! assert (length (lst) >= 32) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
67 %! assert (any (strcmp (lst, "path"))) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
68 %! assert (any (strcmp (lst, "prefix"))) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
69 %! assert (any (strcmp (lst, "stderr"))) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
70 %! assert (any (strcmp (lst, "stdin"))) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
71 %! assert (any (strcmp (lst, "stdout"))) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
72 %! assert (any (strcmp (lst, "version"))) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
73 |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
74 %!test |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
75 %! lst = fieldnames (pyeval ("__builtins__")); |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
76 %! assert (any (strcmp (lst, "False"))) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
77 %! assert (any (strcmp (lst, "None"))) |
b1f874a68e0f
Add pyobject fieldnames and struct methods (fixes issue #33)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
78 %! assert (any (strcmp (lst, "True"))) |
292
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
79 |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
80 %!assert (fieldnames (pyeval ("object()")), cell (0, 1)) |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
81 %!assert (fieldnames (pyeval ("{}")), cell (0, 1)) |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
82 %!assert (ismember ("denominator", fieldnames (pycall ("fractions.Fraction")))) |