Message Boards Message Boards

0
|
4430 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

Logging Input and Output in a text-based interface of Wolfram language

Posted 11 years ago
Although I am using Mathematica 9 notebook usually, I am interested in the logging input and output in a text-based interface of Wolfram Language.
I found a package of SessionLog.m in Programming Mathematica written by Roman Maeder.
$cat SessionLog.m
 (* ::Package:: *)
 BeginPackage["ProgrammingInMathematica`SessionLog`"]
 OpenLog::usage = "OpenLog[filename, opts..] starts logging all input and output to filename."
 CloseLog::usage="CloseLog[] closes the logfile opened by OpenLog[]."
 Begin["`Private`"]
 logfile=""
 OpenLog[ filename_String, opts___?OptionQ] := (
     logfile = OpenWrite[filename, opts];
     If[ logfile === $Failed, Return[logfile] ];
    AppendTo[$Echo, logfile];
    AppendTo[$Output, logfile];
    logfile
    )
CloseLog[] := (
    $Echo = Complement[$Echo, {logfile}];
    $Output = Complement[$Output, {logfile}];
    Close[logfile]
    )
End[ ]
Protect[ OpenLog, CloseLog ]
EndPackage[]

My trial is the following.
 pi@raspberrypi ~/workmath $ sudo wolfram
 Wolfram Language (Raspberry Pi Pilot Release)
 Copyright 1988-2013 Wolfram Research
 Information & help: wolfram.com/raspi
 In[1]:= << SessionLog.m                                                         
 In[2]:= OpenLog["session.log"]                                                 
 Out[2]= OutputStream[session.log, 56]
 In[3]:= Expand[ (x+y)^3 ]                                                      
          3      2          2    3
Out[3]= x  + 3 x  y + 3 x y  + y
In[4]:= CloseLog[]                                                             
Out[4]= session.log
In[5]:= Quit[]
                                                         
pi@raspberrypi ~/workmath $ cat session.log
Out[2]= OutputStream["session.log", 56]
Expand[ (x+y)^3 ]Out[3]= x^3 + 3*x^2*y + 3*x*y^2 + y^3
CloseLog[]
I want to make another format which is written in the book.
Out[2]= OutputStream["session.log", 56]
Expand[ (x+y)^3 ]
Out[3]= x^3 + 3*x^2*y + 3*x*y^2 + y^3
CloseLog[]
Would you please give me a suggestion how to solve this problem ?
Yoshihiro Sato
POSTED BY: Yoshihiro Sato
Posted 10 years ago
I found a little tricky way to answer a question. 
Is there a more efficient program ?
 $ cat SessionLog.m
 (* ::Package:: *)
 BeginPackage["ProgrammingInMathematica`SessionLog`"]
 OpenLog::usage = "filename, opts..] starts logging all input and output to filename."
 CloseLog::usage="CloseLog[] closes the logfile opened by OpenLog[]."
 
 Begin["`Private`"]
 logfile=""
 OpenLog[ filename_String, opts___?OptionQ] := (
    logfile = OpenWrite[filename, opts];
        outfile=filename;
    If[ logfile ===$Failed, Return[logfile] ];
    AppendTo[$Echo, logfile];
    AppendTo[$Output, logfile];
    logfile
    )
CloseLog[] := (
    $Echo = Complement[$Echo, {logfile}];
    $Output = Complement[$Output, {logfile}];
    Close[logfile];
    (* ----- *)
   rawdata=Import[outfile,"Lines"];
   dataString=  hh2[rawdata];
   ofile=(First[#]<>"2."<>Rest[#])&@StringSplit[outfile, "."];
   Export[ofile, dataString,"Table"];
    (* input *)
   in = Select[dataString, StringTake[#, 3] =!= "Out" &];
   script=First[StringSplit[outfile, "."]] <> ".wl";
   Export[script, Most[in], "Table"];
   Print["outfile= ", outfile,", ",ofile,", ",script];
    )
(*  translate comma to String   *)
fcomma[x_]:=If[Length[x]!=0,StringJoin[First[x],MapThread[StringJoin[#1,#2]&,{Table[",",{Length[x]-1}],Rest[x]}]]]
ff[x_]:=
    If[StringTake[x[[1]],3]=="Out",
    {{fcomma[x]}},Partition[StringSplit[First[StringReplace[{fcomma[x]},"Out["->",Out["]],","],1]]
hh2[y_List]:=If[Head[y[[1]]]===String,Flatten[Apply[Join,ff[#]&/@StringSplit[y,","]]], Flatten[Apply[Join,ff[#]&/@y]]]

End[ ]
Protect[ OpenLog, CloseLog ]
EndPackage[]

 $ wolfram
 Wolfram Language (Raspberry Pi Pilot Release)
 Copyright 1988-2013 Wolfram Research
 Information & help: wolfram.com/raspi
 
 In[1]:= << SessionLog.m
                                                        
 In[2]:= OpenLog["math.log"]
                                                     
Out[2]= OutputStream[math.log, 68]

In[3]:= Expand[ (a+b)^2 ]                                                      
         2            2
Out[3]= a  + 2 a b + b

In[4]:= Factor[ x^5-1 ]                                                         
                           2    3    4
Out[4]= (-1 + x) (1 + x + x  + x  + x )

In[5]:= CloseLog[]                                                             
outfile= math.log, math2.log, math.wl

In[6]:= FilePrint["math.log"]                                                   
Out[2]= OutputStream["math.log", 68]
Expand[ (a+b)^2 ]Out[3]= a^2 + 2*a*b + b^2
Factor[ x^5-1 ]Out[4]= (-1 + x)*(1 + x + x^2 + x^3 + x^4)
CloseLog[]
                                                                  
In[7]:= FilePrint["math2.log"]                                                 
Out[2]= OutputStream["math.log", 68]
Expand[ (a+b)^2 ]
Out[3]= a^2 + 2*a*b + b^2
Factor[ x^5-1 ]
Out[4]= (-1 + x)*(1 + x + x^2 + x^3 + x^4)
CloseLog[]
                                                                     
In[8]:= FilePrint["math.wl"]                                                   
Expand[ (a+b)^2 ]
Factor[ x^5-1 ]
                                                                     
In[9]:= Quit[]

 $ wolfram < math.wl
 Wolfram Language (Raspberry Pi Pilot Release)
 Copyright 1988-2013 Wolfram Research
 Information & help: wolfram.com/raspi
 
 In[1]:=
          2            2
 Out[1]= a  + 2 a b + b
 
In[2]:=
                           2    3    4
Out[2]= (-1 + x) (1 + x + x  + x  + x )

In[3]:=
$
POSTED BY: Yoshihiro Sato
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract