Mercurial > pytave
annotate pyargs.m @ 427:750a86973625
Update doctests for display changes
The output of display was recently improved by commit
24546b7021db2ab2553357f691450144c6c5ab50 but the doctests were
not updated.
* @pyobject/dummy.m, @pyobject/cell.m, @pyobject/display.m: update doctests.
* pyargs.m: update doctests.
author | Colin Macdonald <cbm@m.fsf.org> |
---|---|
date | Wed, 25 Oct 2017 13:16:53 -0700 |
parents | 087e7bc3697f |
children |
rev | line source |
---|---|
332
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2016 Mike Miller |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
2 ## |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
3 ## This file is part of Pytave. |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
4 ## |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
5 ## Pytave is free software; you can redistribute it and/or modify it |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
8 ## your option) any later version. |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
9 ## |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
10 ## Pytave is distributed in the hope that it will be useful, but |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
13 ## General Public License for more details. |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
14 ## |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
16 ## along with Pytave; see the file COPYING. If not, see |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
18 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
20 ## @deftypefn {} {} pyargs (@var{name}, @var{value}) |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
21 ## @deftypefnx {} {} pyargs (@var{name}, @var{value}, @dots{}) |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
22 ## Construct a set of Python keyword arguments suitable for passing to |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
23 ## a Python function. |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
24 ## |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
25 ## Python keyword arguments are similar to optional named parameters accepted |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
26 ## by some Octave functions such as @code{cellfun} or @code{set}. |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
27 ## |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
28 ## For example, a dictionary can be initialized with |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
29 ## |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
30 ## @example |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
31 ## @group |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
32 ## py.dict (pyargs ("one", 1, "two", 2)) |
427
750a86973625
Update doctests for display changes
Colin Macdonald <cbm@m.fsf.org>
parents:
365
diff
changeset
|
33 ## @result{} [Python object of type dict] |
332
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
34 ## @{...@} |
365
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
332
diff
changeset
|
35 ## sort (cellfun (@@char, cell (py.list (ans.keys ())), "uniformoutput", false)) |
332
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
36 ## @result{} |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
37 ## @{ |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
38 ## [1,1] = one |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
39 ## [1,2] = two |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
40 ## @} |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
41 ## @end group |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
42 ## @end example |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
43 ## |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
44 ## And a list can be sorted in reverse order with the @code{reverse} keyword |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
45 ## |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
46 ## @example |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
47 ## @group |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
48 ## x = py.list (num2cell (int32 ([1, 2, 3]))); |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
49 ## x.sort (pyargs ("reverse", true)); |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
50 ## x |
427
750a86973625
Update doctests for display changes
Colin Macdonald <cbm@m.fsf.org>
parents:
365
diff
changeset
|
51 ## @result{} x = [Python object of type list] |
332
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
52 ## [3, 2, 1] |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
53 ## @end group |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
54 ## @end example |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
55 ## @end deftypefn |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
56 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
57 function args = pyargs (varargin) |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
58 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
59 if (mod (nargin, 2) != 0) |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
60 error ("pyargs: must be called with NAME, VALUE pairs of arguments"); |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
61 endif |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
62 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
63 keys_vals = {}; |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
64 for i = 1:(nargin / 2) |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
65 k = varargin{2*i - 1}; |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
66 v = varargin{2*i}; |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
67 if (! (ischar (k) && isrow (k))) |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
68 error ("pyargs: NAME %d must be a string", i); |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
69 endif |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
70 try |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
71 v = pyobject (v); |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
72 catch |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
73 error ("pyargs: VALUE %d must be convertible to a Python object", i); |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
74 end_try_catch |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
75 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
76 keys_vals{i} = {k, v}; |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
77 endfor |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
78 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
79 ## This function needs to return a Python mapping object that will be |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
80 ## recognized by other functions as containing keyword arguments. |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
81 ## Define the class using a type specifier so it can be defined and |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
82 ## instantiated as a single expression. |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
83 class_name = "_OctaveKwargs"; |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
84 props = "{'is_kwargs_argument': False}"; |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
85 args = pyeval (sprintf ("type('%s', (dict,), %s)()", class_name, props)); |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
86 args.update (keys_vals); |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
87 args.is_kwargs_argument = true; |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
88 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
89 endfunction |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
90 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
91 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
92 %!assert (isa (pyargs (), "pyobject")) |
365
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
332
diff
changeset
|
93 %!assert (cell (py.list (py.dict (pyargs ()).keys ())), cell (1, 0)) |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
332
diff
changeset
|
94 %!assert (sort (cellfun (@char, cell (py.list (py.dict (pyargs ("one", 1)).keys ())), "uniformoutput", false)), {"one"}) |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
332
diff
changeset
|
95 %!assert (sort (cellfun (@char, cell (py.list (py.dict (pyargs ("one", 1, "two", 2)).keys ())), "uniformoutput", false)), {"one", "two"}) |
332
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
96 |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
97 %!error pyargs (1) |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
98 %!error pyargs (1, 2) |
b8128a2e0e70
pyargs: New function to construct keyword arguments (see issue #45)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
99 %!error pyargs ("one") |