##// END OF EJS Templates
add %precision magic...
MinRK -
Show More
@@ -3459,5 +3459,77 b' Defaulting color scheme to \'NoColor\'"""'
3459 3459
3460 3460 See %xmode for changing exception reporting modes."""
3461 3461 self.shell.showtraceback()
3462
3463 @testdec.skip_doctest
3464 def magic_precision(self, s=''):
3465 """Set floating point precision for pretty printing.
3466
3467 Can set either integer precision or a format string.
3468
3469 If numpy has been imported and precision is an int,
3470 numpy display precision will also be set, via ``numpy.set_printoptions``.
3471
3472 If no argument is given, defaults will be restored.
3473
3474 Examples
3475 --------
3476 ::
3477
3478 In [1]: from math import pi
3479
3480 In [2]: %precision 3
3481
3482 In [3]: pi
3483 Out[3]: 3.142
3484
3485 In [4]: %precision %i
3486
3487 In [5]: pi
3488 Out[5]: 3
3489
3490 In [6]: %precision %e
3491
3492 In [7]: pi**10
3493 Out[7]: 9.364805e+04
3494
3495 In [8]: %precision
3496
3497 In [9]: pi**10
3498 Out[9]: 93648.047476082982
3499
3500 """
3501
3502 if '%' in s:
3503 # got explicit format string
3504 fmt = s
3505 try:
3506 fmt%3.14159
3507 except:
3508 raise TypeError("Precision must be int or format string, not %r"%s)
3509 elif s:
3510 # otherwise, should be an int
3511 try:
3512 i = int(s)
3513 assert i >= 0
3514 except:
3515 raise TypeError("Precision must be non-negative int or format string, not %r"%s)
3516
3517 fmt = '%%.%if'%i
3518 if 'numpy' in sys.modules:
3519 import numpy
3520 numpy.set_printoptions(precision=i)
3521 else:
3522 # default back to repr
3523 fmt = '%r'
3524 if 'numpy' in sys.modules:
3525 import numpy
3526 # numpy default is 8
3527 numpy.set_printoptions(precision=8)
3528
3529 def _pretty_float(obj,p,cycle):
3530 p.text(fmt%obj)
3531
3532 ptformatter = self.shell.display_formatter.formatters['text/plain']
3533 ptformatter.for_type(float, _pretty_float)
3462 3534
3463 3535 # end Magic
General Comments 0
You need to be logged in to leave comments. Login now