"""Utilities to convert python variables into Mathematica sources. The routines in this module enable the quick generation of valid Mathematica source code for variables computed in Python.""" __author__ = 'Fernando Perez ' import sys import Numeric as N def pyvars2file(varnames,fname,ns=None,precision=18): """Save a list of names to a Mathematica file. The resulting file can be used in Mathematica via '<> mfile,'(* File automatically created by Python. *)\n' for name in varnames: var = val[name] if type(var) == N.ArrayType: # numeric arrays vstr = N.array_repr(var) vstr = vstr.replace('[','{').replace(']','}').replace('e','*^') vstr = vstr.replace('array(','').replace(')','') # hack to remove typecode marks if vstr[-4:-2] == ",'": vstr = vstr[:-4] elif isinstance(var,str): # simple strings vstr = '"%s"' % var elif hasattr(var,'__getslice__'): # lists, tuples, etc vstr = str(var).replace('[','{').replace(']','}').replace('e','*^') elif isinstance(var,float): # single floats vstr = (float_tpl % var).replace('e','*^') elif isinstance(var,complex): # complex numbers vstr = str(var).replace('e','*^') else: # the rest. This probably won't be valid Mathematica # except for integers. vstr = str(var) # Save the variable's representation to the Mathematica file, removing # all underscores from the name print >> mfile,'%s = %s;\n' % (name.replace('_',''),vstr) mfile.close() finally: # Restore user settings if save_olw: sys.output_line_width = save_olw_val if save_fop: sys.float_output_precision = save_fop_val # some trivially simple tests if __name__=='__main__': def test1(): import math arr_int0 = N.arange(4,typecode=N.Int0) arr_int16 = N.reshape(N.arange(4,typecode=N.Int16),(2,2)) arr_int32 = N.reshape(N.arange(4,typecode=N.Int16),(2,2)) arr_int = N.reshape(N.arange(4,typecode=N.Int),(2,2)) arr_float32 = (1.2345*N.reshape(N.arange(4),(2,2))).astype(N.Float32) arr_float64 = (-1.2345*N.reshape(N.arange(8),(2,2,2))).astype(N.Float64) sparr_int = N.zeros(5) sparr_int[1] = sparr_int[3] = 99 sparr_float = N.reshape(N.zeros(6,typecode=N.Float32),(2,3)) sparr_float[0,1] = sparr_float[1,2] = 42 pi = math.pi scalar_int = 99 fname = 'py2mathtest.m' pyvars2file(['arr_int0','arr_int16','arr_int32','arr_int', 'arr_float32','arr_float64','pi','scalar_int', 'sparr_int','sparr_float'],fname) print 'Test file left in:',fname print 'Try loading it in Mathematica with <<%s' % fname test1()