# preparse(False) (still) not working

I write .sage files and then attach them in the interpreter (rather than using the notebook). Sometimes I want to have a section in .sage file consisting of pure python code (using for example the python module cvxopt), and of course this sometimes leads to type errors. I've tried including preparse(False) at the beginning of my code (as suggested here: http://www.sagemath.org/doc/faq/faq-u...) but in at least one case it returns the error AttributeError: 'bool' object has no attribute 'lstrip'.

I would very much appreciate a natural solution to this problem that doesn't involve changing anything in python (by decorating it with int,r, and so on).

Here is my code (which works just fine as pure python or works fine if I type preparser(False) in the sage interpreter and then cut and paste the code into the interpreter instead of typing attach('code.sage')).

import cvxopt as cvx from cvxopt import solvers

preparse(False)

c=cvx.matrix([1.,-1.,1.])

G=[cvx.matrix([[-7., -11., -11., 3.],[ 7., -18., -18., 8.],[-2., -8., -8., 1.]]) ]

G+=[cvx.matrix([[-21., -11., 0., -11., 10., 8., 0., 8., 5.],[ 0., 10., 16., 10., -10., -10., 16., -10., 3.],[ -5., 2., -17., 2., -6., 8., -17., 8., 6.]]) ]

h =[cvx.matrix([[33., -9.], [-9., 26.]]) ]

h+=[cvx.matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]])]

sol = solvers.sdp(c, Gs=G, hs=h)

print(sol['x'])

The expected output is

 pcost       dcost       gap    pres   dres   k/t


0: -1.2037e+00 -1.8539e+02 2e+02 2e-16 8e+00 1e+00 1: -1.2937e+00 -6.8551e+00 5e+00 5e-16 3e-01 3e-02 2: -2.8964e+00 -3.7331e+00 7e-01 1e-15 4e-02 5e-02 3: -3.0150e+00 -3.2556e+00 2e-01 7e-16 1e-02 2e-02 4: -3.1389e+00 -3.1932e+00 5e-02 4e-16 3e-03 5e-03 5: -3.1533e+00 -3.1547e+00 1e-03 3e-16 7e-05 1e-04 6: -3.1535e+00 -3.1536e+00 5e-05 6e-16 3e-06 6e-06 7: -3.1535e+00 -3.1535e+00 1e-06 3e-16 7e-08 2e-07 Optimal solution found. [-3.68e-01] [ 1.90e+00] [-8.88e-01]

EDIT: tmonteil suggested changing preparse(False) to preparser(False). This works if I cut and paste the code into the interpreter but doesn't work if I attach the .sage file in the interpreter. See tmonteil's answer and my comment.

EDIT2: It also works in sage worksheet if I replace preparser(False) with %python (but I very much don't want to use the sage worksheet). Is this a bug, or am I doing something wrong?

edit retag close merge delete

Sort by » oldest newest most voted

The preparse function preparses an expression, for example preparse('2^2'). To set the preparser off, you should use the preparser function:

sage: preparser(False)


EDIT:

Another possibility is to attach a file ending with .py and not .sage so that the preparser will not be used for that file.

Also, since the only problem comes from the fact that floats like 1. are turned into Sage RealNumber's by the preparser, another possibility is to explicitely tell to keep those number unparsed, by typing : 1.r instead of 1., the letter r standing for "raw".

Yet another (dirty but lazy) possiblity is to redifine Sage RealNumber to be Python float, by adding the following line at the beginning:

sage: RealNumber=float

more

Thanks. I forgot to mention that I had already tried that and it doesn't work with attach in the above example, although it does work if you copy and paste the code. If I try to attach the above code (with preparse(False) changed to preparser(False)), I get 13 preparser(False) 14 ---> 15 c=cvx.matrix([_sage_const_1p ,-_sage_const_1p ,_sage_const_1p ]) 16 17 G=[cvx.matrix([[-_sage_const_7p , -_sage_const_11p , -_sage_const_11p , _sage_const_3p ],[ _sage_const_7p , -_sage_const_18p , -_sage_const_18p , _sage_const_8p ],[-_sage_const_2p , -_sage_const_8p , -_sage_const_8p , _sage_const_1p ]]) ] TypeError: invalid type in list So that means (I think) that the preparser is not getting turned off when I attach the .sage file.

( 2014-10-30 12:06:51 +0200 )edit

Many thanks. RealNumber=float and Integer=int seem to be my best best now, and I guess I can always define them back to usual later in the file. But this really seems to me a bug, given that %python works as advertised in a sage worksheet but preparser(False) doesn't in .sage file. Not to be ungrateful for your help, but I'll leave this answer unaccepted for now.

( 2014-10-31 13:56:15 +0200 )edit

OK. It seems it is really a good idea to reset Integer as soon I'm done with the pure python code. But I don't see how to do that. Obviously, Integer=int followed by Integer=Integer isn't going to work. On the other hand, I could just leave Integer as Integer and wrap everything with int(), but that's exactly what I don't want to do.

( 2014-10-31 14:44:29 +0200 )edit

RememberInteger=Integer ... Integer=int ... Integer= RememberInteger

( 2015-01-30 20:56:30 +0200 )edit