Re: [sage-support] python (?) help: equivalent of lisp's "let"

8 views
Skip to first unread message
Message has been deleted

William Stein

unread,
May 28, 2008, 2:13:19 PM5/28/08
to sage-s...@googlegroups.com
On Wed, May 28, 2008 at 10:59 AM, John H Palmieri
<jhpalm...@gmail.com> wrote:
>
> Here's the situation: in some sage code that I'm working on, I have a
> variable, say 'output_format', which tells sage how to print certain
> kinds of objects (in particular, they are elements of a vector space,
> and output_format essentially tells sage which basis to use and how to
> print the basis elements). Right now, users can change the value of
> output_format explicitly, but I would like to add a method for the
> class defining my elements so I can type
> "x.basis('blah')" and have the output printed as if output_format were
> set to 'blah'.
>
> In lisp, I could do something like
>
> (let ((output_format 'blah'))
>

Can you give an example of the code users would currently write
to change the property, followed by an example of the code you
wish users could instead write to change the property?

-- William

John H Palmieri

unread,
May 28, 2008, 2:48:01 PM5/28/08
to sage-support


On May 28, 11:13 am, "William Stein" <wst...@gmail.com> wrote:
> On Wed, May 28, 2008 at 10:59 AM, John H Palmieri
>
>
>
> <jhpalmier...@gmail.com> wrote:
>
> > Here's the situation: in some sage code that I'm working on, I have a
> > variable, say 'output_format', which tells sage how to print certain
> > kinds of objects (in particular, they are elements of a vector space,
> > and output_format essentially tells sage which basis to use and how to
> > print the basis elements). Right now, users can change the value of
> > output_format explicitly, but I would like to add a method for the
> > class defining my elements so I can type
> > "x.basis('blah')" and have the output printed as if output_format were
> > set to 'blah'.
>
> > In lisp, I could do something like
>
> > (let ((output_format 'blah'))
>

Hi William,

I accidentally posted this before it was ready, and then deleted the
message from the group, but you read it before it got deleted.

> Can you give an example of the code users would currently write
> to change the property, followed by an example of the code you
> wish users could instead write to change the property?

I currently have a function, set_output_format, so users can do this:

sage: set_output_format('old')
'old'
sage: x = (blah); x
(x printed in 'old' format)

I would like to be able to do this:

sage: x.format('new')
(x printed in 'new' format)
sage: x # output_format should still be set to 'old'
(x printed in 'old' format)

Anyway, I've almost solved my problem (using a try...finally...
block). Now my question is this: is there an existing sage command
which does this:

if in notebook mode and typeset box is on:
view(x)
else:
print(x)

Then I can do:

try:
old = set_output_format() # with no arguments, set_output_format
returns the current format
set_output_format(new)
fancy_print_command(x) # which behaves as I described above
finally:
set_output_format(old)

> -- William

--
John

Carl Witty

unread,
May 28, 2008, 3:35:23 PM5/28/08
to sage-support
You should look at the Python "with" statement (http://docs.python.org/
ref/with.html; see also http://docs.python.org/ref/context-managers.html).
You could implement a function "output_format" so that:

with output_format(new):
fancy_print_command(x)

is the equivalent of your try-finally block above.

Carl

John H Palmieri

unread,
May 28, 2008, 4:02:30 PM5/28/08
to sage-support
> ref/with.html; see alsohttp://docs.python.org/ref/context-managers.html).
> You could implement a function "output_format" so that:
>
> with output_format(new):
> fancy_print_command(x)
>
> is the equivalent of your try-finally block above.
>
> Carl

Thanks, that looks like a good thing to know about.

John H Palmieri

unread,
May 28, 2008, 5:03:39 PM5/28/08
to sage-support
In some code, I'd like to view or print an object depending on whether
in notebook mode with the "typeset" box checked or not. Is this a
good way to do it?

from sage.misc.misc import embedded
from sage.misc.latex import pretty_print, view
import sys
if embedded() and sys.displayhook == pretty_print:
view(x)
else:
print(x)

William Stein

unread,
Jun 10, 2008, 10:12:18 PM6/10/08
to sage-s...@googlegroups.com
On Wed, May 28, 2008 at 2:03 PM, John H Palmieri <jhpalm...@gmail.com> wrote:
>
> In some code, I'd like to view or print an object depending on whether
> in notebook mode with the "typeset" box checked or not. Is this a
> good way to do it?
>
> from sage.misc.misc import embedded
> from sage.misc.latex import pretty_print, view
> import sys
> if embedded() and sys.displayhook == pretty_print:
> view(x)
> else:
> print(x)
>

I think that is the *only* way to do it. I don't think it is a "good way"
though. It could be made much nicer by adding a single function somewhere,
e.g., in misc/*/ that has the lines of code you wrote above in it,
and has a meaningful name. Then other code that needs to do the
same thing will be easier to write and read. What do you think?

-- William

John H Palmieri

unread,
Jun 11, 2008, 12:35:23 AM6/11/08
to sage-support
On Jun 10, 7:12 pm, "William Stein" <wst...@gmail.com> wrote:
Well, I didn't know if 'sys.displayhook == pretty_print' was the right
way to check for the typeset box. Anyway, here's a ticket (with a
patch):

<http://trac.sagemath.org/sage_trac/ticket/3396>

-- John

>  -- William

William Stein

unread,
Jun 11, 2008, 12:56:05 AM6/11/08
to sage-s...@googlegroups.com

Well it works, and if we encapsulate it in a function (like you've done),
then if we decide on a better way later it will be easy to change in only
one place.

> Anyway, here's a ticket (with a
> patch):
>
> <http://trac.sagemath.org/sage_trac/ticket/3396>

Excellent. If you add a doctest I'll give it a positive review :-)

William

John H Palmieri

unread,
Jun 11, 2008, 10:28:25 AM6/11/08
to sage-support


On Jun 10, 9:56 pm, "William Stein" <wst...@gmail.com> wrote:
I'm not sure how to add a doctest for this, since its behavior depends
on the situation, and since I don't know how to provide a doctest for
'view'. Any suggestions? (Besides, none of the other functions in
latex.py have doctests. Well, maybe some of them do, but lots of them
don't.)

>
> William

William Stein

unread,
Jun 11, 2008, 11:05:06 AM6/11/08
to sage-s...@googlegroups.com

I added doctests and gave the patch a positive review:

http://trac.sagemath.org/sage_trac/ticket/3396

Reply all
Reply to author
Forward
0 new messages