ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 22 Apr 2020 13:09:34 +0200@fork decorator with try/except on different platforms, not executing except clausehttps://ask.sagemath.org/question/50912/fork-decorator-with-tryexcept-on-different-platforms-not-executing-except-clause/ I am having problems using the **@fork** decorator with a try & except clause. On SageMathCell the piece of code runs fine, whereas on both Jupyter and CoCalc it doesn't throw the exception clause properly. On CoCalc it didn't recognise the **@fork** decorator at all at first, but `from sage.all import *` (idea from question: "@fork decorator not recognized in script") seemed to help.
**Piece of code:**
from sage.all import * #for CoCalc
a_0,a_1 = var('a_0,a_1');s = [a_0,a_1]
equations = [69*a_0 + 4556 == 69*a_0 + 63*a_1, 69*a_1 - 3350 == -67*a_0 + 57*a_1, 63*a_0 - 3876 == -1542, 63*a_1 + 2850 == 7406]
try:
@fork(timeout=0.1, verbose=True) #use e.g. 0.1 and 10
def DirectSolution():
sage_solution = solve(equations, s , solution_dict=True)
print('Solves in time ,','sage_solution:',sage_solution)
return sage_solution
sage_solution = DirectSolution()[0]
except KeyboardInterrupt:
sage_solution = []
print('Takes too long , ','sage_solution:',sage_solution)
print('Execute the rest of the code, ','sage_solution:',sage_solution)
Running the @fork decorator without try/except with 0.1 seconds I got the `KeyboardInterrupt` error, that's why I used it in the except clause. Shouldn't this exception usually be "raised when the user hits the interrupt key" ?
- As mentioned above, on SageMathCell
the code works as intended:
With 10 seconds it computes the
solution and prints out the text. For
0.1 seconds it sets
`sage_solution = []` and prints out
the text.
- However, both on Jupyter and CoCalc it doesn't use the exception properly for 0.1 seconds.
I get the following message:
`Killing subprocess 1346 with input ((), {}) which took too long`
`Execute the rest of the code, sage_solution: N`
Meaning it didn't execute the except clause:
`sage_solution = []`
`print('Takes too long , ','sage_solution:',sage_solution)`
I am not sure what to look for here, because it is working on one platform. A simple try/except example worked fine. Another idea was that the `KeyboardInterrupt` could be the problem, but removing it didn't change anything.
I am new to Sage/Python, so there probably is a simple solution but I am happy for any help given. Unfortunately my karma was insufficient to publish links.RikWed, 22 Apr 2020 13:09:34 +0200https://ask.sagemath.org/question/50912/syntax error when doing try-except with the except commandhttps://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/I am running sage via virtual box and for some reason I am getting a syntax error for except. I was wondering if this wasn't available because I am using virtual box or if I am doing something wrong.
example below,
sage: def total_chromatic_number(G, certificate=False):
....: nmax = len(G) + len(G.edges()) # trivial upper bound on the number of colors.
....: for n in range(1,nmax+1):
....: p = MixedIntegerLinearProgram()
....: bv = p.new_variable(binary=True)
....: be = p.new_variable(binary=True)
....: for v in G.vertices():
....: p.add_constraint(sum(bv[v,c] for c in range(n)) == 1)
....: for e in G.edges(labels=False):
....: p.add_constraint(sum(be[e,c] for c in range(n)) == 1)
....: for v in G.vertices():
....: for c in range(n):
....: p.add_constraint(bv[v,c] + sum(be[e,c] for e in G.edges_incident(v, labels=False)) <= 1)
....: for v,w in G.edges(labels=False):
....: for c in range(n):
....: p.add_constraint(bv[v,c] + bv[w,c] + be[(v,w),c] <= 1)
....: try:
....: p.solve()
....: if certificate:
....: bv_sol = p.get_values(bv)
....: be_sol = p.get_values(be)
....: coloration = {}
....: for v in G.vertices():
....: for c in range(n):
....: if bv_sol[v,c] == 1:
....: coloration[v] = c
....: for e in G.edges(labels=False):
....: for c in range(n):
....: if be_sol[e,c] == 1:
....: coloration[e] = c
....: return coloration
....: else:
....: return n
....: except MIPSolverException:
File "<ipython-input-17-059a0ac5e5a6>", line 34
except MIPSolverException:
^
SyntaxError: invalid syntaxlivvy94Wed, 04 Jan 2017 14:29:30 +0100https://ask.sagemath.org/question/36168/Any way to detect division by zero?https://ask.sagemath.org/question/7800/any-way-to-detect-division-by-zero/In Sage, is there any way (function call) to check whether a given expression will produce division by zero error. Is something like the following possible?
if IsDivisionByZero(expr):
return False
ShuWed, 08 Dec 2010 19:02:45 +0100https://ask.sagemath.org/question/7800/