|
10 | 10 | "SortAnythingKey", |
11 | 11 | ] |
12 | 12 |
|
| 13 | +import sys |
13 | 14 | import numpy as np |
14 | 15 | from cStringIO import StringIO |
15 | 16 | from compat import optional_dep_ok |
@@ -381,13 +382,24 @@ def _mini_pretty(obj): |
381 | 382 | printer.pretty(obj) |
382 | 383 | return printer.getvalue() |
383 | 384 |
|
384 | | -if optional_dep_ok: |
385 | | - try: |
386 | | - from IPython.lib.pretty import pretty as repr_pretty_delegate |
387 | | - except ImportError: |
388 | | - repr_pretty_delegate = _mini_pretty |
389 | | -else: |
390 | | - repr_pretty_delegate = _mini_pretty |
| 385 | +def repr_pretty_delegate(obj): |
| 386 | + # If IPython is already loaded, then might as well use it. (Most commonly |
| 387 | + # this will occur if we are in an IPython session, but somehow someone has |
| 388 | + # called repr() directly. This can happen for example if printing an |
| 389 | + # container like a namedtuple that IPython lacks special code for |
| 390 | + # pretty-printing.) But, if IPython is not already imported, we do not |
| 391 | + # attempt to import it. This makes patsy itself faster to import (as of |
| 392 | + # Nov. 2012 I measured the extra overhead from loading IPython as ~4 |
| 393 | + # seconds on a cold cache), it prevents IPython from automatically |
| 394 | + # spawning a bunch of child processes (!) which may not be what you want |
| 395 | + # if you are not otherwise using IPython, and it avoids annoying the |
| 396 | + # pandas people who have some hack to tell whether you are using IPython |
| 397 | + # in their test suite (see patsy bug #12). |
| 398 | + if optional_dep_ok and "IPython" in sys.modules: |
| 399 | + from IPython.lib.pretty import pretty |
| 400 | + return pretty(obj) |
| 401 | + else: |
| 402 | + return _mini_pretty(obj) |
391 | 403 |
|
392 | 404 | def repr_pretty_impl(p, obj, args, kwargs=[]): |
393 | 405 | name = obj.__class__.__name__ |
|
0 commit comments