Tuesday, March 01, 2005

Replacing a simple spreadsheet: Python needs what?

We were retracing the setup of a testbench for a simulation at work when my boss wanted to keep a record of a particular calculation we needed. It was really nothing too fancy - we just had to calculate how many device clock cycles elapsed in a JTAG idle statement from a good simulation , and make sure we had the same number when modifying the JTAG clock (which we could do independently of the device clock). This would ensure we idled for the same number of device clock cycles when we changed JTAG clock frequency ensuring the device had the same time to come out of reset.
Now my boss went for a simple spreadsheet solution. something like:
Original Values
(hidden cell function)
jtag_hi
640000

jtag_lo
640000

jtag_period
1280000
=b1+b2
jtag_idle
500




New Values

jtag_hi 1300

jtag_lo 100

jtag_period 1400
=b7+b8
jtag_idle 457000
=b3/b9*b4

I got thinking about the immediate use of the spreadsheet and thought why would I not do this in Python?

The idea is to show how a result is calculated ideally to be cut and pasted into a larger document or printed out.
In Ipython I would get the following log:
@pc[PC1]|2> jtag_lo=640000
@pc[PC1]|3> jtag_hi=640000
@pc[PC1]|4> jtag_period=jtag_lo+jtag_hi
@pc[PC1]|5> jtag_idle=500
@pc[PC1]|6> jtag_lo2=1300
@pc[PC1]|7> jtag_hi2=100
@pc[PC1]|8> jtag_period2=jtag_lo2+jtag_hi2
@pc[PC1]|9> jtag_idle2=jtag_idle*jtag_period/jtag_period2
@pc[PC1]|10> print jtag_idle2
457142
@pc[PC1]|11>

Notice how although the functions are shown, the intermediate answers are not, requiring the print at the end to display the final answer.
What would be ideal is if Ipython had a mode where it would show the value assigned to any name automatically, (when a simple = is typed interactively).
This mode should give the following type of output at the command line:
@pc[PC1]|2> jtag_lo=640000
= 640000
@pc[PC1]|3> jtag_hi=640000
= 640000
@pc[PC1]|4> jtag_period=jtag_lo+jtag_hi
= 1280000
@pc[PC1]|5> jtag_idle=500
= 500
@pc[PC1]|6> jtag_lo2=1300
= 1300
@pc[PC1]|7> jtag_hi2=100
= 100
@pc[PC1]|8> jtag_period2=jtag_lo2+jtag_hi2
= 1400
@pc[PC1]|9> jtag_idle2=jtag_idle*jtag_period/jtag_period2
= 457142

Finally, if Ipython allowed me to edit a line, e.g. line five to set jtag_idle to 50 , then replay lines 2 to 9 with the edited line 5 and show the new intermediate results, then I would prefer the Ipython solution to the spreadsheet one as I am very familiar with the procedural code and the resultant log, when printed or cut and pasted into a larger document would be both more informative than the spreadsheet (both the calculation and intermediate results would be shown at once); and more succinct (either plain text or simple HTML/RTF versus an embedded spreadsheet or HTML table that would loose the formulae).