float_precision changed, set float_format accordingly. float_precision can be set by int or str. This will set float_format, after interpreting input. If numpy has been imported, numpy print precision will also be set. integer `n` sets format to '%.nf', otherwise, f
(self, change)
| 624 | |
| 625 | @observe('float_precision') |
| 626 | def _float_precision_changed(self, change): |
| 627 | """float_precision changed, set float_format accordingly. |
| 628 | |
| 629 | float_precision can be set by int or str. |
| 630 | This will set float_format, after interpreting input. |
| 631 | If numpy has been imported, numpy print precision will also be set. |
| 632 | |
| 633 | integer `n` sets format to '%.nf', otherwise, format set directly. |
| 634 | |
| 635 | An empty string returns to defaults (repr for float, 8 for numpy). |
| 636 | |
| 637 | This parameter can be set via the '%precision' magic. |
| 638 | """ |
| 639 | |
| 640 | new = change['new'] |
| 641 | if '%' in new: |
| 642 | # got explicit format string |
| 643 | fmt = new |
| 644 | try: |
| 645 | fmt%3.14159 |
| 646 | except Exception: |
| 647 | raise ValueError("Precision must be int or format string, not %r"%new) |
| 648 | elif new: |
| 649 | # otherwise, should be an int |
| 650 | try: |
| 651 | i = int(new) |
| 652 | assert i >= 0 |
| 653 | except ValueError: |
| 654 | raise ValueError("Precision must be int or format string, not %r"%new) |
| 655 | except AssertionError: |
| 656 | raise ValueError("int precision must be non-negative, not %r"%i) |
| 657 | |
| 658 | fmt = '%%.%if'%i |
| 659 | if 'numpy' in sys.modules: |
| 660 | # set numpy precision if it has been imported |
| 661 | import numpy |
| 662 | numpy.set_printoptions(precision=i) |
| 663 | else: |
| 664 | # default back to repr |
| 665 | fmt = '%r' |
| 666 | if 'numpy' in sys.modules: |
| 667 | import numpy |
| 668 | # numpy default is 8 |
| 669 | numpy.set_printoptions(precision=8) |
| 670 | self.float_format = fmt |
| 671 | |
| 672 | # Use the default pretty printers from IPython.lib.pretty. |
| 673 | @default('singleton_printers') |
nothing calls this directly
no outgoing calls
no test coverage detected