tags:

views:

151

answers:

4

I often use the python interpreter for doing quick numerical calculations and would like all numerical results to be automatically printed using, e.g., exponential notation. Is there a way to set this for the entire session?

For example, I want:

>>> 1.e12
1.0e+12

not:

>>> 1.e12
1000000000000.0
A: 

As you know you can use the % operator or str.format to format strings:

For example:

>>> "%e" % 1.e12
'1.000000e+12'

I was wondering if you could monkey patch the built-in float class to change the formatting but it seems that Python won't let you:

>>> 1.e12.__class__.__repr__ = lambda x: "%e" % x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'float'

So the only other thing I can think of is to write your own file-like object which captures output, reformats it and sends it to the standard output. You'd then redirect standard output of the interpreter to this object:

sys.stdout = my_formatting_object
Dave Webb
Yes, but I do not want to have to type this with every input. Since I want the same formatting for the entire session I would like to set it once and not bother with it anymore.
Michael Schneider
Whoops. Sorry, didn't read your question properly. Have expanded the answer to cover your question properly, as far as I can.
Dave Webb
+1  A: 

Hm... It's not a 100% solution, but this have come to my mind...

How about defining a subclass of float which would have an overridden __str__ method (to print with the exp notation). And then you would have to wrap all the expressions with object construction of this class). It would be a bit shorter than Dave's solution, you would define the class once and then write something like:

>>> F(1.e12)
1.0e+12
>>> F(3.)
3.0e+0
>>> F(1.+2.+3.+4.)
1.0e+1
...
Grzegorz Oledzki
+3  A: 

Create a Python script called whatever you want (say mystartup.py) and then set an environment variable PYTHONSTARTUP to the path of this script. Python will then load this script on startup of an interactive session (but not when running scripts). In this script, define a function similar to this:

def _(v):
    if type(v) == type(0.0):
        print "%e" % v
    else:
        print v

Then, in an interactive session:

C:\temp>set PYTHONSTARTUP=mystartup.py

C:\temp>python
ActivePython 2.5.2.2 (ActiveState Software Inc.) based on
Python 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> _(1e12)
1.000000e+012
>>> _(14)
14
>>> _(14.0)
1.400000e+001
>>>

Of course, you can define the function to be called whaetver you want and to work exactly however you want.

Even better than this would be to use IPython. It's great, and you can set the number formatting how you want by using result_display.when_type(some_type)(my_print_func) (see the IPython site or search for more details on how to use this).

Vinay Sajip
+1  A: 

Building on Dave Webb's hint above. You can of course set the precision if you like ("%.3e") and perhaps override writelines if needed.

import os
import sys

class ExpFloatFileObject:
    def write(self, s):
        try:
            s = "%e"%float(s)
        except ValueError:
            pass
        sys.__stdout__.write(s)

    def __getattr__(self, name):
        return getattr(sys.__stdout__, name)

sys.stdout = ExpFloatFileObject()

and usage:

>>> 14000    
1.400000e+04  
>>> "text"
'text'