_Session.set_variable for ndarray and spmatrix

This function will correctly serialize numpy arrays as json due to
customizations to the JSON encoder. Additionally, this function will
handle scipy sparse matrices as well.

The matlab magic now calls this function.

This commit *DOES NOT* add any new dependencies to numpy and scipy, and
the code should function as normal without them.
Sorry - I didn't even notice who responded :-)

Thanks for chiming in!


On Fri, Aug 29, 2014 at 1:50 PM, Ariel Rokem <arokem@gmail.com> wrote:

> OK - then I must be misunderstanding: why are you using a random number
> here? I thought it was for uniqueness.
>
>
> On Fri, Aug 29, 2014 at 1:00 PM, Ismail Badawi <notifications@github.com>
> wrote:
>
>> In pymatbridge/pymatbridge.py:
>>
>> > @@ -192,6 +211,24 @@ def run_code(self, code):
>> >      def get_variable(self, varname):
>> >          return self._json_response(cmd='get_var', varname=varname)['var']
>> >
>> > +    def set_variable(self, varname, value):
>> > +        if isinstance(value, spmatrix):
>> > +            return self._set_sparse_variable(varname, value)
>> > +        return self.run_func('pymat_set_variable.m',
>> > +                             {'name': varname, 'value': value})
>> > +
>> > +    def _set_sparse_variable(self, varname, value):
>> > +        value = value.todok()
>> > +        prefix = 'pymatbridge_temp_sparse_%d_' % randint(0, 1000000)
>>
>> If you're worried about uniqueness an easy thing would be to use
>> uuid.uuid4().hex. I'm not sure a collision would matter though, since
>> the variable is cleared right after and I don't think you can have multiple
>> threads using the same connection anyway (?).
>>
>> —
>> Reply to this email directly or view it on GitHub
>> <https://github.com/arokem/python-matlab-bridge/pull/85/files#r16917891>.
>>
>
>