[IPython-dev] Engineering formatting

Warren Weckesser warren.weckesser at enthought.com
Wed Apr 21 12:17:59 EDT 2010


(I might have sent already sent a slight variation of this email--if so, 
sorry for the spam.)

Robert Kern wrote:
> On 4/19/10 3:37 PM, Stéfan van der Walt wrote:
>   
>> Hi all,
>>
>> This is probably an easy question, but how do I tweak IPython to
>> display my numerical results in engineering notation?
>>     
>
> Use the pretty extension! In your ipy_user_conf.py:
>
>
> from IPython.Extensions import ipy_pretty
>
> ipy_pretty.activate()
>
> def float_eng_pprinter(obj, p, cycle):
>      p.text('%e' % obj)
>   

That shows the printing 'hook' that is needed, but for engineering 
notation, one wants the power of 10 to be a multiple of three.  Attached 
is some code that I have used to do this.

Following Robert's example, but defining float_eng_pprinter like this:

def float_eng_pprinter(obj, p, cycle):
   p.text(eng_format(obj))


scalar floats are printed in engineering format.  E.g.

In [25]: x = 12345.67

In [26]: y = 3.451e-7

In [27]: x
Out[27]:  12.346e3

In [28]: y
Out[28]: 345.100e-9

Making this work for arrays still remains as an exercise for the reader. :)

Warren


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: eng.py
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20100421/6c4da078/attachment.ksh>


More information about the IPython-dev mailing list