ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 12 Mar 2013 03:03:35 -0500Using timeit in an interact without string formattinghttp://ask.sagemath.org/question/9883/using-timeit-in-an-interact-without-string-formatting/I wanted the following to work, but it didn't. Anyone have an idea how to make it do so?
def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(n=6739815371):
T(n)
timeit('T(n)')
Instead I had to do
def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(n=6739815371):
T(n)
timeit('T(%s)'%n)
But I feel like the first thing did indeed work in the past (though I tried it with a Sage from 2010 to no avail).
I guess in some sense this is a silly question - `'T(n)'` is a string, pure and simple. At the same time, in the command line
sage: def T(n):
....: top = ceil(math.sqrt(n))
....: print top
....:
sage: n=6739815371
sage: timeit('T(n)')
625 loops, best of 3: 5.96 µs per loop
and `'T(n)'` is a string there too. So what is different about `interact` that this doesn't work (in Sage or the cell server)?Tue, 05 Mar 2013 14:21:49 -0600http://ask.sagemath.org/question/9883/using-timeit-in-an-interact-without-string-formatting/Answer by ndomes for <p>I wanted the following to work, but it didn't. Anyone have an idea how to make it do so?</p>
<pre><code>def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(n=6739815371):
T(n)
timeit('T(n)')
</code></pre>
<p>Instead I had to do </p>
<pre><code>def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(n=6739815371):
T(n)
timeit('T(%s)'%n)
</code></pre>
<p>But I feel like the first thing did indeed work in the past (though I tried it with a Sage from 2010 to no avail). </p>
<p>I guess in some sense this is a silly question - <code>'T(n)'</code> is a string, pure and simple. At the same time, in the command line</p>
<pre><code>sage: def T(n):
....: top = ceil(math.sqrt(n))
....: print top
....:
sage: n=6739815371
sage: timeit('T(n)')
625 loops, best of 3: 5.96 µs per loop
</code></pre>
<p>and <code>'T(n)'</code> is a string there too. So what is different about <code>interact</code> that this doesn't work (in Sage or the cell server)?</p>
http://ask.sagemath.org/question/9883/using-timeit-in-an-interact-without-string-formatting/?answer=14645#post-id-14645As far as n is in the global namespace, the error message of your example is misleading.
My example below gives the error message:
NameError: global name 'k' is not defined
def T(k):
top = ceil(math.sqrt(k))
print top
@interact
def _(k=6739815371):
timeit('T(k)')
So timeit apparently looks in the global namespace.
My first try was:
@interact
def _(k=6739815371):
global k
timeit('T(k)')
Traceback (most recent call last): def _(k=6739815371): File "", line 1, in <module> File "/tmp/tmpVWF0nv/___code___.py", line 8 @interact SyntaxError: name 'k' is local and global
So we have to outwit the @interact function:
@interact
def _(k0=6739815371):
global k
k = k0
timeit('T(k)')Tue, 12 Mar 2013 03:03:35 -0500http://ask.sagemath.org/question/9883/using-timeit-in-an-interact-without-string-formatting/?answer=14645#post-id-14645Answer by ppurka for <p>I wanted the following to work, but it didn't. Anyone have an idea how to make it do so?</p>
<pre><code>def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(n=6739815371):
T(n)
timeit('T(n)')
</code></pre>
<p>Instead I had to do </p>
<pre><code>def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(n=6739815371):
T(n)
timeit('T(%s)'%n)
</code></pre>
<p>But I feel like the first thing did indeed work in the past (though I tried it with a Sage from 2010 to no avail). </p>
<p>I guess in some sense this is a silly question - <code>'T(n)'</code> is a string, pure and simple. At the same time, in the command line</p>
<pre><code>sage: def T(n):
....: top = ceil(math.sqrt(n))
....: print top
....:
sage: n=6739815371
sage: timeit('T(n)')
625 loops, best of 3: 5.96 µs per loop
</code></pre>
<p>and <code>'T(n)'</code> is a string there too. So what is different about <code>interact</code> that this doesn't work (in Sage or the cell server)?</p>
http://ask.sagemath.org/question/9883/using-timeit-in-an-interact-without-string-formatting/?answer=14631#post-id-14631I suspect it is some python idiosyncrasy. It is somehow not getting the variable n as defined in the function declaration. The following works (for some reason using `n=n` again didn't work, probably because `n` is defined globally to be some function):
def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(k=6739815371):
T(k)
k = k
timeit('T(k)')Tue, 05 Mar 2013 19:11:24 -0600http://ask.sagemath.org/question/9883/using-timeit-in-an-interact-without-string-formatting/?answer=14631#post-id-14631Comment by kcrisman for <p>I suspect it is some python idiosyncrasy. It is somehow not getting the variable n as defined in the function declaration. The following works (for some reason using <code>n=n</code> again didn't work, probably because <code>n</code> is defined globally to be some function):</p>
<pre><code>def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(k=6739815371):
T(k)
k = k
timeit('T(k)')
</code></pre>
http://ask.sagemath.org/question/9883/using-timeit-in-an-interact-without-string-formatting/?comment=18095#post-id-18095Huh, bizarre.Wed, 06 Mar 2013 06:38:00 -0600http://ask.sagemath.org/question/9883/using-timeit-in-an-interact-without-string-formatting/?comment=18095#post-id-18095