Example #1
0
            save_fop_val = sys.float_output_precision

        # Store values in sys
        sys.output_line_width = output_line_width
        sys.float_output_precision = float_output_precision

        # for regular floats
        float_tpl = "%." + str(precision) + "e"

        # if we get this far, we have everything we need for saving
        mfile = open(fname, "w")
        print >> 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.