Converts dicts with sym keys to structs... #491

Merged
merged 1 commit into from Jun 23, 2016

Projects

None yet

2 participants

@genuinelucifer
Contributor

Required by @sym/solve.m

@cbm755
Owner
cbm755 commented Jun 23, 2016

I'll need to think about this a bit. Is this roughly the same as elif isinstance(x, dict): part of python_header.py?

@genuinelucifer
Contributor

Not exactly... But yes, somewhat similar...
.
It just takes care of the special sub-case where the keys are sym objects.. Else it leaves the conversion to PyTave

@genuinelucifer genuinelucifer commented on the diff Jun 23, 2016
inst/private/check_and_convert.m
if is_list_curvar
obj{i} = check_and_convert(cur_var);
+ elseif is_dict_with_sym_keys_curvar
+ %if cur_var is dictionary with symbols as keys then convert it to a struct
+ pyexec(strjoin({'_allKeysStr = []',
+ '_allValues = []',
+ ['for key, value in ' cur_var '.iteritems():'],
+ ' _allKeysStr.append(str(key))',
+ ' _allValues.append(value)'}, newl));
+ _allKeysStr = pyeval('_allKeysStr');
@genuinelucifer
genuinelucifer Jun 23, 2016 edited Contributor

I seeing all these weird looking variables on GitHub (when the variable name is preceded by '_' (underscore)..
Is there a convention not to use variable names starting with underscore in octave?

@cbm755
cbm755 Jun 23, 2016 Owner

Not allowed in Matlab. I think github does not know about Octave. That's sometimes a problem for octsympy, if Matlab even parses one of our files but I don't think it will get to this one, so don't worry about it.

@genuinelucifer
genuinelucifer Jun 23, 2016 Contributor

Oh. Thanks. :)

@cbm755 cbm755 commented on the diff Jun 23, 2016
inst/private/check_and_convert.m
if is_list_curvar
obj{i} = check_and_convert(cur_var);
+ elseif is_dict_with_sym_keys_curvar
+ %if cur_var is dictionary with symbols as keys then convert it to a struct
+ pyexec(strjoin({'_allKeysStr = []',
+ '_allValues = []',
+ ['for key, value in ' cur_var '.iteritems():'],
+ ' _allKeysStr.append(str(key))',
+ ' _allValues.append(value)'}, newl));
+ _allKeysStr = pyeval('_allKeysStr');
+ for j = 1:numel(_allKeysStr)
+ obj{i}.(_allKeysStr{j}) = get_sym_from_python(sprintf('_allValues[%d]', j-1));
@cbm755
cbm755 Jun 23, 2016 Owner

This solution is a bit fragile: but let's leave it for another fix, I filed #492.

@cbm755
cbm755 Jun 23, 2016 Owner

I think I prefer setfield but this is ok too (no need to change).

But I do think you want obj{i} = struct() before the for loop though.

@cbm755 cbm755 merged commit 0bb356b into cbm755:pytave Jun 23, 2016

1 check failed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
@cbm755
Owner
cbm755 commented Jun 23, 2016

Ok, I did the struct initialization thing and merged

@genuinelucifer
Contributor

Thanks. I was actually changing a few things and running all the tests to try if something breaks.. So couldn't do this...

@genuinelucifer genuinelucifer deleted the genuinelucifer:convert_dicts branch Nov 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment