(v)
return v
from_json(v)

    If v has the format {'_type':..., '_value':...} as would have been created
    using to_json(...), then it is replaced:

        '_type': 'complex' -> complex
        '_type': 'ndarray' -> non-complex ndarray
        '_type': 'complex_ndarray' -> complex ndarray
        '_type': 'sympy' -> sympy.Expr
        '_type': 'sympy_matrix' -> sympy.Matrix

    If v encodes an ndarray and has the field '_dtype', this function recovers
    its dtype.

    This function does not try to recover information like the assumptions on
    variables in a sympy expression.

    If v does not have the format {'_type':..., '_value':...}, then it is
    returned without change.
from_json(v)
from_json ( v )
def from_json(v):
    """from_json(v)

    If v has the format {'_type':..., '_value':...} as would have been created
    using to_json(...), then it is replaced:

        '_type': 'complex' -> complex
        '_type': 'ndarray' -> non-complex ndarray
        '_type': 'complex_ndarray' -> complex ndarray
        '_type': 'sympy' -> sympy.Expr
        '_type': 'sympy_matrix' -> sympy.Matrix

    If v encodes an ndarray and has the field '_dtype', this function recovers
    its dtype.

    This function does not try to recover information like the assumptions on
    variables in a sympy expression.

    If v does not have the format {'_type':..., '_value':...}, then it is
    returned without change.
    """
    if isinstance(v, dict):
        if '_type' in v:
            if v['_type'] == 'complex':
                if ('_value' in v) and ('real' in v['_value']) and ('imag' in v['_value']):
                    return complex(v['_value']['real'], v['_value']['imag'])
                else:
                    raise Exception('variable of type complex should have value with real and imaginary pair')
            elif v['_type'] == 'ndarray':
                if ('_value' in v):
                    if ('_dtype' in v):
                        return np.array(v['_value']).astype(v['_dtype'])
                    else:
                        return np.array(v['_value'])
                else:
                    raise Exception('variable of type ndarray should have value')
            elif v['_type'] == 'complex_ndarray':
                if ('_value' in v) and ('real' in v['_value']) and ('imag' in v['_value']):
                    if ('_dtype' in v):
                        return (np.array(v['_value']['real']) + np.array(v['_value']['imag']) * 1j).astype(v['_dtype'])
                    else:
                        return np.array(v['_value']['real']) + np.array(v['_value']['imag']) * 1j
                else:
                    raise Exception('variable of type complex_ndarray should have value with real and imaginary pair')
            elif v['_type'] == 'sympy':
                return json_to_sympy(v)
            elif v['_type'] == 'sympy_matrix':
                if ('_value' in v) and ('_variables' in v) and ('_shape' in v):
                    value = v['_value']
                    variables = v['_variables']
                    shape = v['_shape']
                    M = sympy.Matrix.zeros(shape[0], shape[1])
                    for i in range(0, shape[0]):
                        for j in range(0, shape[1]):
                            M[i, j] = convert_string_to_sympy(value[i][j], variables)
                    return M
                else:
                    raise Exception('variable of type sympy_matrix should have value, variables, and shape')
            elif v['_type'] == 'dataframe':
                if ('_value' in v) and ('index' in v['_value']) and ('columns' in v['_value']) and ('data' in v['_value']):
                    val = v['_value']
                    return pandas.DataFrame(index=val['index'], columns=val['columns'], data=val['data'])
                else:
                    raise Exception('variable of type dataframe should have value with index, columns, and data')
            else:
                raise Exception('variable has unknown type {:s}'.format(v['_type']))
    return v
def from_json ( v ) : if isinstance ( v , dict ) : if '_type' in v : if v [ '_type' ] == 'complex' : if ( '_value' in v ) and ( 'real' in v [ '_value' ] ) and ( 'imag' in v [ '_value' ] ) : return complex ( v [ '_value' ] [ 'real' ] , v [ '_value' ] [ 'imag' ] ) else : raise Exception ( 'variable of type complex should have value with real and imaginary pair' ) elif v [ '_type' ] == 'ndarray' : if ( '_value' in v ) : if ( '_dtype' in v ) : return np . array ( v [ '_value' ] ) . astype ( v [ '_dtype' ] ) else : return np . array ( v [ '_value' ] ) else : raise Exception ( 'variable of type ndarray should have value' ) elif v [ '_type' ] == 'complex_ndarray' : if ( '_value' in v ) and ( 'real' in v [ '_value' ] ) and ( 'imag' in v [ '_value' ] ) : if ( '_dtype' in v ) : return ( np . array ( v [ '_value' ] [ 'real' ] ) + np . array ( v [ '_value' ] [ 'imag' ] ) * 1j ) . astype ( v [ '_dtype' ] ) else : return np . array ( v [ '_value' ] [ 'real' ] ) + np . array ( v [ '_value' ] [ 'imag' ] ) * 1j else : raise Exception ( 'variable of type complex_ndarray should have value with real and imaginary pair' ) elif v [ '_type' ] == 'sympy' : return json_to_sympy ( v ) elif v [ '_type' ] == 'sympy_matrix' : if ( '_value' in v ) and ( '_variables' in v ) and ( '_shape' in v ) : value = v [ '_value' ] variables = v [ '_variables' ] shape = v [ '_shape' ] M = sympy . Matrix . zeros ( shape [ 0 ] , shape [ 1 ] ) for i in range ( 0 , shape [ 0 ] ) : for j in range ( 0 , shape [ 1 ] ) : M [ i , j ] = convert_string_to_sympy ( value [ i ] [ j ] , variables ) return M else : raise Exception ( 'variable of type sympy_matrix should have value, variables, and shape' ) elif v [ '_type' ] == 'dataframe' : if ( '_value' in v ) and ( 'index' in v [ '_value' ] ) and ( 'columns' in v [ '_value' ] ) and ( 'data' in v [ '_value' ] ) : val = v [ '_value' ] return pandas . DataFrame ( index = val [ 'index' ] , columns = val [ 'columns' ] , data = val [ 'data' ] ) else : raise Exception ( 'variable of type dataframe should have value with index, columns, and data' ) else : raise Exception ( 'variable has unknown type {:s}' . format ( v [ '_type' ] ) ) return v