Output too long in SAGE

I have a program whos output is too long to be displayed in the terminal where I run sage. Is there a possiblity that the output of SAGE is automatically "copied" (as if I mark it and use ctrl+c) and then I can paste it into a text file? Or is there another way that the result can be automatically be saved into a text file or a way to make the terminal allow larger text before the text gets cut off?

Here the code Im talking about:

le_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.comparability_graph().edges(labels=None)]
cover_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.hasse_diagram().edges(labels=None)]

format_pt = lambda k: "'x{0}'".format(k+1)
format_relations = lambda relations: [[format_pt(a),format_pt(b)] for a,b in relations]
format_pts = lambda P: [format_pt(a) for a in P]
format_poset = lambda P: [format_pts(P), format_relations(cover_relations(P)), format_relations(le_relations(P))]

what_you_want = lambda n: [format_poset(P) for P in posets(n) if P.is_lattice()]

print(what_you_want(8))


It takes about 20 minits for my computer to finish the calculation while the calculation seems to never finish in the online sage cell https://sagecell.sagemath.org/.

edit: Another option would be to divide the set [format_poset(P) for P in posets(n) if P.is_lattice] into smaller pieces (lets say 5 pieces) and apply the code to those smaller pieces. The set contains 222 elements. Is there a way to apply the cod to the first 40 of those elements first and then I do it seperately for the next 40 and so on till I have all. Maybe it is possible to filter instead of posets(n) (for n=8) over just the first 40 posets of posets(n) and then the next 40 and so on.

edit retag close merge delete

[Bug report (?)] Should P.is_lattice be P.is_lattice() in the second to last line in the code above?

( 2017-09-24 02:57:22 +0200 )edit

Thanks I edited it.

( 2017-09-24 10:37:49 +0200 )edit

Sort by » oldest newest most voted

To save to a file, you may try the following.

with open('filename.txt','w') as outfile:
outfile.write("{0}\n".format(what_you_want(8)))


Searching for "write to file in python" might give some insight into this aspect of Python.

If you wish to process a list by chunks, then looking up "slicing in Python" might be useful. Given a list L, you may access the second chunk of length 40 by using L[40:80]. So using slicing within a for loop could produce the desired result.

more

Thank you very much!

( 2017-09-24 10:43:59 +0200 )edit

@fidbc Another question just for safety (I tested it for small examples and it works, but I want to make sure). If I want to have a "disjoint union" of a list L (say with 80 elements) into smaller lists I have to do it like L[0:40] and L[40:80] to get a disjoint union or? (instead of L[0:40] and L[41:80] )

( 2017-09-24 10:58:20 +0200 )edit

@maresageL[0:40] will output all elements in L with indices i such that 0<=i<40. Thus L[40] will not be contained in L[0:40]. However L[40:80] will contain L[40].

( 2017-09-24 23:01:36 +0200 )edit

@fidbc ok, as expected. Thank you.

( 2017-09-24 23:16:56 +0200 )edit