Ask Your Question

# 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/.

The code is from the thread https://ask.sagemath.org/question/388... )

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

## Comments

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

## 1 Answer

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

## Comments

@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] )

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

## Your Answer

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

Add Answer

## Stats

Asked: 2017-09-23 22:17:13 +0200

Seen: 181 times

Last updated: Sep 24 '17