This repository was archived by the owner on Jun 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkw.m
49 lines (45 loc) · 1.53 KB
/
kw.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
% Class kw: Python Keywords
% Python allows function calls to include keyword arguments as well as
% positional arguments. In some cases arguments *must* be supplied as keywords.
% MATLAB has no similar syntax, so some sort of marker is needed to indicate
% keyword arguments to be provided to Python calls, like so:
% x = pyfunc(a, kw('b', 'bee'), c, kw('d', 'dee', 'e', 'iii'))
% The kw() constructor takes key/value pairs and produces an object array.
% When MATLAB tries to call a python function using the 'call' method (or the ()
% syntax), any objects of type kw will be concatenated, converted to a python
% dict, and passed in as the **kwargs parameter.
classdef kw
properties
keyword
value
end
methods
function kwargs = kw(varargin)
arg = 0;
if mod(nargin,2)
error('kw:needspairs', 'Input must be in pairs.');
elseif nargin == 0
kwargs(1) = [];
end
if nargin == 2
kwargs.keyword = varargin{1};
kwargs.value = varargin{2};
assert(ischar(kwargs.keyword));
else
for i = 1:2:nargin
arg = arg + 1;
kwargs(arg) = kw(varargin{i:i+1});
end
end
end
function d = dict(kwargs)
dict = pybuiltins('dict');
d = dict();
for i=1:numel(kwargs)
d{kwargs(i).keyword} = kwargs(i).value;
end
end
end
end
% Copyright (c) 2009 Ken Watford (kwatford@cise.ufl.edu)
% For full license details, see the LICENSE file.