On Sunday, November 3, 2013 11:43:35 AM UTC-8, William wrote:
>
> Unfortunately, the input isn't Python code, it's sage worksheet code, 
> because of both the preparser and the % cell and line decorators. 
>

But once you've stripped and processed those bits, what you're left with is 
something that gets fed to Python.
A python block is a sequence of statements, some of them bare expressions.
The old notebook (sort of) prints the result of the last statement if it's 
an expression, but even then really only if it fits on a line. Compare
{{{
[1,2,3]
///
[1,2,3]
}}}
(works as expected) and
{{{
[1,2,
3]
///
}}}
(nothing printed, because expression doesn't start on last line)

What we would LIKE, and what you seem to be trying in cloud from what I 
understand, is to print/display the results of all statements in the block 
that are expressions. Instead of trying to chop up the block into 
statements yourself, you could let Python's parser do that, look which 
statements are bare expressions, and wrap those with the appropriate code 
to send the result somewhere:

def wrap_with_print(c):
    R=ast.parse("sys.displayhook(None)").body[0]
    R.value.args[0]=c.value
    return R

def execute_block_with_display(code_string):
    M=ast.parse(code_string)
    M.body=[wrap_with_print(s) if isinstance(s,ast.Expr) else s for s in 
M.body]
    c=compile(M,"<string>","exec")
    eval(c,globals())

S="""
a=1
("value of a=",a)
a+=1
("value of a=",a)
"""
execute_block_with_display(S)

this should be much more robust than trying to roll your own partial 
implementation of a python parser. Note that you only have to do 
modifications on top level, so there is not much ast "walking" involved.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to