Ask Your Question

pipe into stdin vs attach -- differences?

asked 2015-03-14 02:38:59 +0200

Kevin Buzzard gravatar image

Here's some code, called slopes.sage:

def classical_slopes(N,p,r,k,i):
    assert p>2
    assert r>1 ## wild
    G=DirichletGroup(p^r) ## cyclic
    K=alpha.base_ring() ## cyclotomic
    primroot=primitive_root(p) ## p>2
    assert len(X)==1 ## ideal should be a prime power
    Qt = PolynomialRing(QQ, 't')
    c2=[2^(a.valuation(P)) if a!=0 else 0 for a in f.coefficients()]
    return (e,Qt(c2).newton_slopes(2))

# off we go. The import sys bit is to flush stdout.
import sys
for k in range(2,10):
    print N,p,3,k,i,classical_slopes(N,p,3,k,i)

If I pipe it into sage like this:

sage < slopes.sage

I get the following output:

$ sage < slopes.sage 
│ Sage Version 6.5, Release Date: 2015-02-17                         │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
sage: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: sage: sage: sage: sage: sage: ....: ....: ....: ....: 
Exiting Sage (CPU time 0m0.03s, Wall time 0m0.20s).

But if I attach the file, within a sage console session, it runs as I expect it to run. Is this something to do with the pre-parser? If I've made a slip then that's great, but if it's harder than I think to pipe sage script into sage then I'd appreciate some tips as I would like to start a large sage job on a remote machine via ssh.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2015-03-14 07:14:48 +0200

nbruin gravatar image

You can do

sage slopes.sage

to run the file from the command line. The bad interaction is probably between the ipython readline interface and a non-tty stdin, not with the preparser. Note that IPython does some undesirable things for file input. For one thing, it uses auto-indent, which makes it hard or impossible to input some more complicated loops (unless you use %cpaste).

Sage or Ipython could check if stdin is a tty and revert to "file processing" (i.e., not use readline etc.) if it's not, but apparently (judging from your example) it doesn't.

In any case, the preparser does apply a few more efficient tricks (such as factoring out constants) when you use sage <file>, so it's better to use that if you can, rather than redirect the input. It has the side-effect of writing into the current directory, though.

edit flag offensive delete link more


For what its' worth: this did not work for me initially, but then I realised why -- I had mis-installed sage in a silly way (and slopes.sage was not actually being passed to sage at all). Thanks as ever Nils.

Kevin Buzzard gravatar imageKevin Buzzard ( 2015-03-14 18:05:50 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-03-14 02:38:59 +0200

Seen: 550 times

Last updated: Mar 14 '15