##// END OF EJS Templates
use real Warnings for formatter errors...
MinRK -
Show More
@@ -1,819 +1,825 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Display formatters.
3 3
4 4 Inheritance diagram:
5 5
6 6 .. inheritance-diagram:: IPython.core.formatters
7 7 :parts: 3
8 8
9 9 Authors:
10 10
11 11 * Robert Kern
12 12 * Brian Granger
13 13 """
14 14 #-----------------------------------------------------------------------------
15 15 # Copyright (C) 2010-2011, IPython Development Team.
16 16 #
17 17 # Distributed under the terms of the Modified BSD License.
18 18 #
19 19 # The full license is in the file COPYING.txt, distributed with this software.
20 20 #-----------------------------------------------------------------------------
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Imports
24 24 #-----------------------------------------------------------------------------
25 25
26 26 # Stdlib imports
27 27 import abc
28 28 import sys
29 29 import warnings
30 30
31 31 from IPython.external.decorator import decorator
32 32
33 33 # Our own imports
34 34 from IPython.config.configurable import Configurable
35 35 from IPython.lib import pretty
36 36 from IPython.utils import io
37 37 from IPython.utils.traitlets import (
38 38 Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List,
39 39 )
40 40 from IPython.utils.warn import warn
41 41 from IPython.utils.py3compat import (
42 42 unicode_to_str, with_metaclass, PY3, string_types, unicode_type,
43 43 )
44 44
45 45 if PY3:
46 46 from io import StringIO
47 47 else:
48 48 from StringIO import StringIO
49 49
50 50
51 51 #-----------------------------------------------------------------------------
52 52 # The main DisplayFormatter class
53 53 #-----------------------------------------------------------------------------
54 54
55 55 class DisplayFormatter(Configurable):
56 56
57 57 # When set to true only the default plain text formatter will be used.
58 58 plain_text_only = Bool(False, config=True)
59 59 def _plain_text_only_changed(self, name, old, new):
60 60 warnings.warn("""DisplayFormatter.plain_text_only is deprecated.
61 61
62 62 Use DisplayFormatter.active_types = ['text/plain']
63 63 for the same effect.
64 64 """, DeprecationWarning)
65 65 if new:
66 66 self.active_types = ['text/plain']
67 67 else:
68 68 self.active_types = self.format_types
69 69
70 70 active_types = List(Unicode, config=True,
71 71 help="""List of currently active mime-types to display.
72 72 You can use this to set a white-list for formats to display.
73 73
74 74 Most users will not need to change this value.
75 75 """)
76 76 def _active_types_default(self):
77 77 return self.format_types
78 78
79 79 def _active_types_changed(self, name, old, new):
80 80 for key, formatter in self.formatters.items():
81 81 if key in new:
82 82 formatter.enabled = True
83 83 else:
84 84 formatter.enabled = False
85 85
86 86 # A dict of formatter whose keys are format types (MIME types) and whose
87 87 # values are subclasses of BaseFormatter.
88 88 formatters = Dict()
89 89 def _formatters_default(self):
90 90 """Activate the default formatters."""
91 91 formatter_classes = [
92 92 PlainTextFormatter,
93 93 HTMLFormatter,
94 94 SVGFormatter,
95 95 PNGFormatter,
96 96 JPEGFormatter,
97 97 LatexFormatter,
98 98 JSONFormatter,
99 99 JavascriptFormatter
100 100 ]
101 101 d = {}
102 102 for cls in formatter_classes:
103 103 f = cls(parent=self)
104 104 d[f.format_type] = f
105 105 return d
106 106
107 107 def format(self, obj, include=None, exclude=None):
108 108 """Return a format data dict for an object.
109 109
110 110 By default all format types will be computed.
111 111
112 112 The following MIME types are currently implemented:
113 113
114 114 * text/plain
115 115 * text/html
116 116 * text/latex
117 117 * application/json
118 118 * application/javascript
119 119 * image/png
120 120 * image/jpeg
121 121 * image/svg+xml
122 122
123 123 Parameters
124 124 ----------
125 125 obj : object
126 126 The Python object whose format data will be computed.
127 127 include : list or tuple, optional
128 128 A list of format type strings (MIME types) to include in the
129 129 format data dict. If this is set *only* the format types included
130 130 in this list will be computed.
131 131 exclude : list or tuple, optional
132 132 A list of format type string (MIME types) to exclude in the format
133 133 data dict. If this is set all format types will be computed,
134 134 except for those included in this argument.
135 135
136 136 Returns
137 137 -------
138 138 (format_dict, metadata_dict) : tuple of two dicts
139 139
140 140 format_dict is a dictionary of key/value pairs, one of each format that was
141 141 generated for the object. The keys are the format types, which
142 142 will usually be MIME type strings and the values and JSON'able
143 143 data structure containing the raw data for the representation in
144 144 that format.
145 145
146 146 metadata_dict is a dictionary of metadata about each mime-type output.
147 147 Its keys will be a strict subset of the keys in format_dict.
148 148 """
149 149 format_dict = {}
150 150 md_dict = {}
151 151
152 152 for format_type, formatter in self.formatters.items():
153 153 if include and format_type not in include:
154 154 continue
155 155 if exclude and format_type in exclude:
156 156 continue
157 157
158 158 md = None
159 159 try:
160 160 data = formatter(obj)
161 161 except:
162 162 # FIXME: log the exception
163 163 raise
164 164
165 165 # formatters can return raw data or (data, metadata)
166 166 if isinstance(data, tuple) and len(data) == 2:
167 167 data, md = data
168 168
169 169 if data is not None:
170 170 format_dict[format_type] = data
171 171 if md is not None:
172 172 md_dict[format_type] = md
173 173
174 174 return format_dict, md_dict
175 175
176 176 @property
177 177 def format_types(self):
178 178 """Return the format types (MIME types) of the active formatters."""
179 179 return list(self.formatters.keys())
180 180
181 181
182 182 #-----------------------------------------------------------------------------
183 183 # Formatters for specific format types (text, html, svg, etc.)
184 184 #-----------------------------------------------------------------------------
185 185
186 class FormatterWarning(UserWarning):
187 """Warning class for errors in formatters"""
188
186 189 @decorator
187 190 def warn_format_error(method, self, *args, **kwargs):
188 191 """decorator for warning on failed format call"""
189 192 try:
190 193 r = method(self, *args, **kwargs)
191 194 except NotImplementedError as e:
192 195 # don't warn on NotImplementedErrors
193 196 return None
194 197 except Exception as e:
195 warn("Exception in %s formatter: %s" % (self.format_type, e))
198 warnings.warn("Exception in %s formatter: %s" % (self.format_type, e),
199 FormatterWarning,
200 )
196 201 return None
197 202 if r is None or isinstance(r, self._return_type) or \
198 203 (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
199 204 return r
200 205 else:
201 warn("%s formatter returned invalid type %s (expected %s) for object: %s" % (
202 self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])
203 ))
204
206 warnings.warn(
207 "%s formatter returned invalid type %s (expected %s) for object: %s" % \
208 (self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])),
209 FormatterWarning
210 )
205 211
206 212
207 213 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
208 214 """ Abstract base class for Formatters.
209 215
210 216 A formatter is a callable class that is responsible for computing the
211 217 raw format data for a particular format type (MIME type). For example,
212 218 an HTML formatter would have a format type of `text/html` and would return
213 219 the HTML representation of the object when called.
214 220 """
215 221
216 222 # The format type of the data returned, usually a MIME type.
217 223 format_type = 'text/plain'
218 224
219 225 # Is the formatter enabled...
220 226 enabled = True
221 227
222 228 @abc.abstractmethod
223 229 @warn_format_error
224 230 def __call__(self, obj):
225 231 """Return a JSON'able representation of the object.
226 232
227 233 If the object cannot be formatted by this formatter,
228 234 warn and return None.
229 235 """
230 236 return repr(obj)
231 237
232 238
233 239 def _mod_name_key(typ):
234 240 """Return a (__module__, __name__) tuple for a type.
235 241
236 242 Used as key in Formatter.deferred_printers.
237 243 """
238 244 module = getattr(typ, '__module__', None)
239 245 name = getattr(typ, '__name__', None)
240 246 return (module, name)
241 247
242 248
243 249 def _get_type(obj):
244 250 """Return the type of an instance (old and new-style)"""
245 251 return getattr(obj, '__class__', None) or type(obj)
246 252
247 253 _raise_key_error = object()
248 254
249 255
250 256 class BaseFormatter(Configurable):
251 257 """A base formatter class that is configurable.
252 258
253 259 This formatter should usually be used as the base class of all formatters.
254 260 It is a traited :class:`Configurable` class and includes an extensible
255 261 API for users to determine how their objects are formatted. The following
256 262 logic is used to find a function to format an given object.
257 263
258 264 1. The object is introspected to see if it has a method with the name
259 265 :attr:`print_method`. If is does, that object is passed to that method
260 266 for formatting.
261 267 2. If no print method is found, three internal dictionaries are consulted
262 268 to find print method: :attr:`singleton_printers`, :attr:`type_printers`
263 269 and :attr:`deferred_printers`.
264 270
265 271 Users should use these dictionaries to register functions that will be
266 272 used to compute the format data for their objects (if those objects don't
267 273 have the special print methods). The easiest way of using these
268 274 dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name`
269 275 methods.
270 276
271 277 If no function/callable is found to compute the format data, ``None`` is
272 278 returned and this format type is not used.
273 279 """
274 280
275 281 format_type = Unicode('text/plain')
276 282 _return_type = string_types
277 283
278 284 enabled = Bool(True, config=True)
279 285
280 286 print_method = ObjectName('__repr__')
281 287
282 288 # The singleton printers.
283 289 # Maps the IDs of the builtin singleton objects to the format functions.
284 290 singleton_printers = Dict(config=True)
285 291
286 292 # The type-specific printers.
287 293 # Map type objects to the format functions.
288 294 type_printers = Dict(config=True)
289 295
290 296 # The deferred-import type-specific printers.
291 297 # Map (modulename, classname) pairs to the format functions.
292 298 deferred_printers = Dict(config=True)
293 299
294 300 @warn_format_error
295 301 def __call__(self, obj):
296 302 """Compute the format for an object."""
297 303 if self.enabled:
298 304 # lookup registered printer
299 305 try:
300 306 printer = self.lookup(obj)
301 307 except KeyError:
302 308 pass
303 309 else:
304 310 return printer(obj)
305 311 # Finally look for special method names
306 312 method = pretty._safe_getattr(obj, self.print_method, None)
307 313 if method is not None:
308 314 return method()
309 315 return None
310 316 else:
311 317 return None
312 318
313 319 def __contains__(self, typ):
314 320 """map in to lookup_by_type"""
315 321 try:
316 322 self.lookup_by_type(typ)
317 323 except KeyError:
318 324 return False
319 325 else:
320 326 return True
321 327
322 328 def lookup(self, obj):
323 329 """Look up the formatter for a given instance.
324 330
325 331 Parameters
326 332 ----------
327 333 obj : object instance
328 334
329 335 Returns
330 336 -------
331 337 f : callable
332 338 The registered formatting callable for the type.
333 339
334 340 Raises
335 341 ------
336 342 KeyError if the type has not been registered.
337 343 """
338 344 # look for singleton first
339 345 obj_id = id(obj)
340 346 if obj_id in self.singleton_printers:
341 347 return self.singleton_printers[obj_id]
342 348 # then lookup by type
343 349 return self.lookup_by_type(_get_type(obj))
344 350
345 351 def lookup_by_type(self, typ):
346 352 """Look up the registered formatter for a type.
347 353
348 354 Parameters
349 355 ----------
350 356 typ : type or '__module__.__name__' string for a type
351 357
352 358 Returns
353 359 -------
354 360 f : callable
355 361 The registered formatting callable for the type.
356 362
357 363 Raises
358 364 ------
359 365 KeyError if the type has not been registered.
360 366 """
361 367 if isinstance(typ, string_types):
362 368 typ_key = tuple(typ.rsplit('.',1))
363 369 if typ_key not in self.deferred_printers:
364 370 # We may have it cached in the type map. We will have to
365 371 # iterate over all of the types to check.
366 372 for cls in self.type_printers:
367 373 if _mod_name_key(cls) == typ_key:
368 374 return self.type_printers[cls]
369 375 else:
370 376 return self.deferred_printers[typ_key]
371 377 else:
372 378 for cls in pretty._get_mro(typ):
373 379 if cls in self.type_printers or self._in_deferred_types(cls):
374 380 return self.type_printers[cls]
375 381
376 382 # If we have reached here, the lookup failed.
377 383 raise KeyError("No registered printer for {0!r}".format(typ))
378 384
379 385 def for_type(self, typ, func=None):
380 386 """Add a format function for a given type.
381 387
382 388 Parameters
383 389 -----------
384 390 typ : type or '__module__.__name__' string for a type
385 391 The class of the object that will be formatted using `func`.
386 392 func : callable
387 393 A callable for computing the format data.
388 394 `func` will be called with the object to be formatted,
389 395 and will return the raw data in this formatter's format.
390 396 Subclasses may use a different call signature for the
391 397 `func` argument.
392 398
393 399 If `func` is None or not specified, there will be no change,
394 400 only returning the current value.
395 401
396 402 Returns
397 403 -------
398 404 oldfunc : callable
399 405 The currently registered callable.
400 406 If you are registering a new formatter,
401 407 this will be the previous value (to enable restoring later).
402 408 """
403 409 # if string given, interpret as 'pkg.module.class_name'
404 410 if isinstance(typ, string_types):
405 411 type_module, type_name = typ.rsplit('.', 1)
406 412 return self.for_type_by_name(type_module, type_name, func)
407 413
408 414 try:
409 415 oldfunc = self.lookup_by_type(typ)
410 416 except KeyError:
411 417 oldfunc = None
412 418
413 419 if func is not None:
414 420 self.type_printers[typ] = func
415 421
416 422 return oldfunc
417 423
418 424 def for_type_by_name(self, type_module, type_name, func=None):
419 425 """Add a format function for a type specified by the full dotted
420 426 module and name of the type, rather than the type of the object.
421 427
422 428 Parameters
423 429 ----------
424 430 type_module : str
425 431 The full dotted name of the module the type is defined in, like
426 432 ``numpy``.
427 433 type_name : str
428 434 The name of the type (the class name), like ``dtype``
429 435 func : callable
430 436 A callable for computing the format data.
431 437 `func` will be called with the object to be formatted,
432 438 and will return the raw data in this formatter's format.
433 439 Subclasses may use a different call signature for the
434 440 `func` argument.
435 441
436 442 If `func` is None or unspecified, there will be no change,
437 443 only returning the current value.
438 444
439 445 Returns
440 446 -------
441 447 oldfunc : callable
442 448 The currently registered callable.
443 449 If you are registering a new formatter,
444 450 this will be the previous value (to enable restoring later).
445 451 """
446 452 key = (type_module, type_name)
447 453
448 454 try:
449 455 oldfunc = self.lookup_by_type("%s.%s" % key)
450 456 except KeyError:
451 457 oldfunc = None
452 458
453 459 if func is not None:
454 460 self.deferred_printers[key] = func
455 461 return oldfunc
456 462
457 463 def pop(self, typ, default=_raise_key_error):
458 464 """Pop a formatter for the given type.
459 465
460 466 Parameters
461 467 ----------
462 468 typ : type or '__module__.__name__' string for a type
463 469 default : object
464 470 value to be returned if no formatter is registered for typ.
465 471
466 472 Returns
467 473 -------
468 474 obj : object
469 475 The last registered object for the type.
470 476
471 477 Raises
472 478 ------
473 479 KeyError if the type is not registered and default is not specified.
474 480 """
475 481
476 482 if isinstance(typ, string_types):
477 483 typ_key = tuple(typ.rsplit('.',1))
478 484 if typ_key not in self.deferred_printers:
479 485 # We may have it cached in the type map. We will have to
480 486 # iterate over all of the types to check.
481 487 for cls in self.type_printers:
482 488 if _mod_name_key(cls) == typ_key:
483 489 old = self.type_printers.pop(cls)
484 490 break
485 491 else:
486 492 old = default
487 493 else:
488 494 old = self.deferred_printers.pop(typ_key)
489 495 else:
490 496 if typ in self.type_printers:
491 497 old = self.type_printers.pop(typ)
492 498 else:
493 499 old = self.deferred_printers.pop(_mod_name_key(typ), default)
494 500 if old is _raise_key_error:
495 501 raise KeyError("No registered value for {0!r}".format(typ))
496 502 return old
497 503
498 504 def _in_deferred_types(self, cls):
499 505 """
500 506 Check if the given class is specified in the deferred type registry.
501 507
502 508 Successful matches will be moved to the regular type registry for future use.
503 509 """
504 510 mod = getattr(cls, '__module__', None)
505 511 name = getattr(cls, '__name__', None)
506 512 key = (mod, name)
507 513 if key in self.deferred_printers:
508 514 # Move the printer over to the regular registry.
509 515 printer = self.deferred_printers.pop(key)
510 516 self.type_printers[cls] = printer
511 517 return True
512 518 return False
513 519
514 520
515 521 class PlainTextFormatter(BaseFormatter):
516 522 """The default pretty-printer.
517 523
518 524 This uses :mod:`IPython.lib.pretty` to compute the format data of
519 525 the object. If the object cannot be pretty printed, :func:`repr` is used.
520 526 See the documentation of :mod:`IPython.lib.pretty` for details on
521 527 how to write pretty printers. Here is a simple example::
522 528
523 529 def dtype_pprinter(obj, p, cycle):
524 530 if cycle:
525 531 return p.text('dtype(...)')
526 532 if hasattr(obj, 'fields'):
527 533 if obj.fields is None:
528 534 p.text(repr(obj))
529 535 else:
530 536 p.begin_group(7, 'dtype([')
531 537 for i, field in enumerate(obj.descr):
532 538 if i > 0:
533 539 p.text(',')
534 540 p.breakable()
535 541 p.pretty(field)
536 542 p.end_group(7, '])')
537 543 """
538 544
539 545 # The format type of data returned.
540 546 format_type = Unicode('text/plain')
541 547
542 548 # This subclass ignores this attribute as it always need to return
543 549 # something.
544 550 enabled = Bool(True, config=False)
545 551
546 552 # Look for a _repr_pretty_ methods to use for pretty printing.
547 553 print_method = ObjectName('_repr_pretty_')
548 554
549 555 # Whether to pretty-print or not.
550 556 pprint = Bool(True, config=True)
551 557
552 558 # Whether to be verbose or not.
553 559 verbose = Bool(False, config=True)
554 560
555 561 # The maximum width.
556 562 max_width = Integer(79, config=True)
557 563
558 564 # The newline character.
559 565 newline = Unicode('\n', config=True)
560 566
561 567 # format-string for pprinting floats
562 568 float_format = Unicode('%r')
563 569 # setter for float precision, either int or direct format-string
564 570 float_precision = CUnicode('', config=True)
565 571
566 572 def _float_precision_changed(self, name, old, new):
567 573 """float_precision changed, set float_format accordingly.
568 574
569 575 float_precision can be set by int or str.
570 576 This will set float_format, after interpreting input.
571 577 If numpy has been imported, numpy print precision will also be set.
572 578
573 579 integer `n` sets format to '%.nf', otherwise, format set directly.
574 580
575 581 An empty string returns to defaults (repr for float, 8 for numpy).
576 582
577 583 This parameter can be set via the '%precision' magic.
578 584 """
579 585
580 586 if '%' in new:
581 587 # got explicit format string
582 588 fmt = new
583 589 try:
584 590 fmt%3.14159
585 591 except Exception:
586 592 raise ValueError("Precision must be int or format string, not %r"%new)
587 593 elif new:
588 594 # otherwise, should be an int
589 595 try:
590 596 i = int(new)
591 597 assert i >= 0
592 598 except ValueError:
593 599 raise ValueError("Precision must be int or format string, not %r"%new)
594 600 except AssertionError:
595 601 raise ValueError("int precision must be non-negative, not %r"%i)
596 602
597 603 fmt = '%%.%if'%i
598 604 if 'numpy' in sys.modules:
599 605 # set numpy precision if it has been imported
600 606 import numpy
601 607 numpy.set_printoptions(precision=i)
602 608 else:
603 609 # default back to repr
604 610 fmt = '%r'
605 611 if 'numpy' in sys.modules:
606 612 import numpy
607 613 # numpy default is 8
608 614 numpy.set_printoptions(precision=8)
609 615 self.float_format = fmt
610 616
611 617 # Use the default pretty printers from IPython.lib.pretty.
612 618 def _singleton_printers_default(self):
613 619 return pretty._singleton_pprinters.copy()
614 620
615 621 def _type_printers_default(self):
616 622 d = pretty._type_pprinters.copy()
617 623 d[float] = lambda obj,p,cycle: p.text(self.float_format%obj)
618 624 return d
619 625
620 626 def _deferred_printers_default(self):
621 627 return pretty._deferred_type_pprinters.copy()
622 628
623 629 #### FormatterABC interface ####
624 630
625 631 @warn_format_error
626 632 def __call__(self, obj):
627 633 """Compute the pretty representation of the object."""
628 634 if not self.pprint:
629 635 return pretty._safe_repr(obj)
630 636 else:
631 637 # This uses use StringIO, as cStringIO doesn't handle unicode.
632 638 stream = StringIO()
633 639 # self.newline.encode() is a quick fix for issue gh-597. We need to
634 640 # ensure that stream does not get a mix of unicode and bytestrings,
635 641 # or it will cause trouble.
636 642 printer = pretty.RepresentationPrinter(stream, self.verbose,
637 643 self.max_width, unicode_to_str(self.newline),
638 644 singleton_pprinters=self.singleton_printers,
639 645 type_pprinters=self.type_printers,
640 646 deferred_pprinters=self.deferred_printers)
641 647 printer.pretty(obj)
642 648 printer.flush()
643 649 return stream.getvalue()
644 650
645 651
646 652 class HTMLFormatter(BaseFormatter):
647 653 """An HTML formatter.
648 654
649 655 To define the callables that compute the HTML representation of your
650 656 objects, define a :meth:`_repr_html_` method or use the :meth:`for_type`
651 657 or :meth:`for_type_by_name` methods to register functions that handle
652 658 this.
653 659
654 660 The return value of this formatter should be a valid HTML snippet that
655 661 could be injected into an existing DOM. It should *not* include the
656 662 ```<html>`` or ```<body>`` tags.
657 663 """
658 664 format_type = Unicode('text/html')
659 665
660 666 print_method = ObjectName('_repr_html_')
661 667
662 668
663 669 class SVGFormatter(BaseFormatter):
664 670 """An SVG formatter.
665 671
666 672 To define the callables that compute the SVG representation of your
667 673 objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type`
668 674 or :meth:`for_type_by_name` methods to register functions that handle
669 675 this.
670 676
671 677 The return value of this formatter should be valid SVG enclosed in
672 678 ```<svg>``` tags, that could be injected into an existing DOM. It should
673 679 *not* include the ```<html>`` or ```<body>`` tags.
674 680 """
675 681 format_type = Unicode('image/svg+xml')
676 682
677 683 print_method = ObjectName('_repr_svg_')
678 684
679 685
680 686 class PNGFormatter(BaseFormatter):
681 687 """A PNG formatter.
682 688
683 689 To define the callables that compute the PNG representation of your
684 690 objects, define a :meth:`_repr_png_` method or use the :meth:`for_type`
685 691 or :meth:`for_type_by_name` methods to register functions that handle
686 692 this.
687 693
688 694 The return value of this formatter should be raw PNG data, *not*
689 695 base64 encoded.
690 696 """
691 697 format_type = Unicode('image/png')
692 698
693 699 print_method = ObjectName('_repr_png_')
694 700
695 701 _return_type = (bytes, unicode_type)
696 702
697 703
698 704 class JPEGFormatter(BaseFormatter):
699 705 """A JPEG formatter.
700 706
701 707 To define the callables that compute the JPEG representation of your
702 708 objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type`
703 709 or :meth:`for_type_by_name` methods to register functions that handle
704 710 this.
705 711
706 712 The return value of this formatter should be raw JPEG data, *not*
707 713 base64 encoded.
708 714 """
709 715 format_type = Unicode('image/jpeg')
710 716
711 717 print_method = ObjectName('_repr_jpeg_')
712 718
713 719 _return_type = (bytes, unicode_type)
714 720
715 721
716 722 class LatexFormatter(BaseFormatter):
717 723 """A LaTeX formatter.
718 724
719 725 To define the callables that compute the LaTeX representation of your
720 726 objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type`
721 727 or :meth:`for_type_by_name` methods to register functions that handle
722 728 this.
723 729
724 730 The return value of this formatter should be a valid LaTeX equation,
725 731 enclosed in either ```$```, ```$$``` or another LaTeX equation
726 732 environment.
727 733 """
728 734 format_type = Unicode('text/latex')
729 735
730 736 print_method = ObjectName('_repr_latex_')
731 737
732 738
733 739 class JSONFormatter(BaseFormatter):
734 740 """A JSON string formatter.
735 741
736 742 To define the callables that compute the JSON string representation of
737 743 your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type`
738 744 or :meth:`for_type_by_name` methods to register functions that handle
739 745 this.
740 746
741 747 The return value of this formatter should be a valid JSON string.
742 748 """
743 749 format_type = Unicode('application/json')
744 750
745 751 print_method = ObjectName('_repr_json_')
746 752
747 753
748 754 class JavascriptFormatter(BaseFormatter):
749 755 """A Javascript formatter.
750 756
751 757 To define the callables that compute the Javascript representation of
752 758 your objects, define a :meth:`_repr_javascript_` method or use the
753 759 :meth:`for_type` or :meth:`for_type_by_name` methods to register functions
754 760 that handle this.
755 761
756 762 The return value of this formatter should be valid Javascript code and
757 763 should *not* be enclosed in ```<script>``` tags.
758 764 """
759 765 format_type = Unicode('application/javascript')
760 766
761 767 print_method = ObjectName('_repr_javascript_')
762 768
763 769 FormatterABC.register(BaseFormatter)
764 770 FormatterABC.register(PlainTextFormatter)
765 771 FormatterABC.register(HTMLFormatter)
766 772 FormatterABC.register(SVGFormatter)
767 773 FormatterABC.register(PNGFormatter)
768 774 FormatterABC.register(JPEGFormatter)
769 775 FormatterABC.register(LatexFormatter)
770 776 FormatterABC.register(JSONFormatter)
771 777 FormatterABC.register(JavascriptFormatter)
772 778
773 779
774 780 def format_display_data(obj, include=None, exclude=None):
775 781 """Return a format data dict for an object.
776 782
777 783 By default all format types will be computed.
778 784
779 785 The following MIME types are currently implemented:
780 786
781 787 * text/plain
782 788 * text/html
783 789 * text/latex
784 790 * application/json
785 791 * application/javascript
786 792 * image/png
787 793 * image/jpeg
788 794 * image/svg+xml
789 795
790 796 Parameters
791 797 ----------
792 798 obj : object
793 799 The Python object whose format data will be computed.
794 800
795 801 Returns
796 802 -------
797 803 format_dict : dict
798 804 A dictionary of key/value pairs, one or each format that was
799 805 generated for the object. The keys are the format types, which
800 806 will usually be MIME type strings and the values and JSON'able
801 807 data structure containing the raw data for the representation in
802 808 that format.
803 809 include : list or tuple, optional
804 810 A list of format type strings (MIME types) to include in the
805 811 format data dict. If this is set *only* the format types included
806 812 in this list will be computed.
807 813 exclude : list or tuple, optional
808 814 A list of format type string (MIME types) to exclue in the format
809 815 data dict. If this is set all format types will be computed,
810 816 except for those included in this argument.
811 817 """
812 818 from IPython.core.interactiveshell import InteractiveShell
813 819
814 820 InteractiveShell.instance().display_formatter.format(
815 821 obj,
816 822 include,
817 823 exclude
818 824 )
819 825
@@ -1,282 +1,282 b''
1 1 """Tests for the Formatters."""
2 2
3 3 from math import pi
4 4
5 5 try:
6 6 import numpy
7 7 except:
8 8 numpy = None
9 9 import nose.tools as nt
10 10
11 11 from IPython.core.formatters import PlainTextFormatter, HTMLFormatter, _mod_name_key
12 12 from IPython.utils.io import capture_output
13 13
14 14 class A(object):
15 15 def __repr__(self):
16 16 return 'A()'
17 17
18 18 class B(A):
19 19 def __repr__(self):
20 20 return 'B()'
21 21
22 22 class C:
23 23 pass
24 24
25 25 class BadPretty(object):
26 26 _repr_pretty_ = None
27 27
28 28 class GoodPretty(object):
29 29 def _repr_pretty_(self, pp, cycle):
30 30 pp.text('foo')
31 31
32 32 def __repr__(self):
33 33 return 'GoodPretty()'
34 34
35 35 def foo_printer(obj, pp, cycle):
36 36 pp.text('foo')
37 37
38 38 def test_pretty():
39 39 f = PlainTextFormatter()
40 40 f.for_type(A, foo_printer)
41 41 nt.assert_equal(f(A()), 'foo')
42 42 nt.assert_equal(f(B()), 'foo')
43 43 nt.assert_equal(f(GoodPretty()), 'foo')
44 44 # Just don't raise an exception for the following:
45 45 f(BadPretty())
46 46
47 47 f.pprint = False
48 48 nt.assert_equal(f(A()), 'A()')
49 49 nt.assert_equal(f(B()), 'B()')
50 50 nt.assert_equal(f(GoodPretty()), 'GoodPretty()')
51 51
52 52
53 53 def test_deferred():
54 54 f = PlainTextFormatter()
55 55
56 56 def test_precision():
57 57 """test various values for float_precision."""
58 58 f = PlainTextFormatter()
59 59 nt.assert_equal(f(pi), repr(pi))
60 60 f.float_precision = 0
61 61 if numpy:
62 62 po = numpy.get_printoptions()
63 63 nt.assert_equal(po['precision'], 0)
64 64 nt.assert_equal(f(pi), '3')
65 65 f.float_precision = 2
66 66 if numpy:
67 67 po = numpy.get_printoptions()
68 68 nt.assert_equal(po['precision'], 2)
69 69 nt.assert_equal(f(pi), '3.14')
70 70 f.float_precision = '%g'
71 71 if numpy:
72 72 po = numpy.get_printoptions()
73 73 nt.assert_equal(po['precision'], 2)
74 74 nt.assert_equal(f(pi), '3.14159')
75 75 f.float_precision = '%e'
76 76 nt.assert_equal(f(pi), '3.141593e+00')
77 77 f.float_precision = ''
78 78 if numpy:
79 79 po = numpy.get_printoptions()
80 80 nt.assert_equal(po['precision'], 8)
81 81 nt.assert_equal(f(pi), repr(pi))
82 82
83 83 def test_bad_precision():
84 84 """test various invalid values for float_precision."""
85 85 f = PlainTextFormatter()
86 86 def set_fp(p):
87 87 f.float_precision=p
88 88 nt.assert_raises(ValueError, set_fp, '%')
89 89 nt.assert_raises(ValueError, set_fp, '%.3f%i')
90 90 nt.assert_raises(ValueError, set_fp, 'foo')
91 91 nt.assert_raises(ValueError, set_fp, -1)
92 92
93 93 def test_for_type():
94 94 f = PlainTextFormatter()
95 95
96 96 # initial return, None
97 97 nt.assert_is(f.for_type(C, foo_printer), None)
98 98 # no func queries
99 99 nt.assert_is(f.for_type(C), foo_printer)
100 100 # shouldn't change anything
101 101 nt.assert_is(f.for_type(C), foo_printer)
102 102 # None should do the same
103 103 nt.assert_is(f.for_type(C, None), foo_printer)
104 104 nt.assert_is(f.for_type(C, None), foo_printer)
105 105
106 106 def test_for_type_string():
107 107 f = PlainTextFormatter()
108 108
109 109 mod = C.__module__
110 110
111 111 type_str = '%s.%s' % (C.__module__, 'C')
112 112
113 113 # initial return, None
114 114 nt.assert_is(f.for_type(type_str, foo_printer), None)
115 115 # no func queries
116 116 nt.assert_is(f.for_type(type_str), foo_printer)
117 117 nt.assert_in(_mod_name_key(C), f.deferred_printers)
118 118 nt.assert_is(f.for_type(C), foo_printer)
119 119 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)
120 120 nt.assert_in(C, f.type_printers)
121 121
122 122 def test_for_type_by_name():
123 123 f = PlainTextFormatter()
124 124
125 125 mod = C.__module__
126 126
127 127 # initial return, None
128 128 nt.assert_is(f.for_type_by_name(mod, 'C', foo_printer), None)
129 129 # no func queries
130 130 nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer)
131 131 # shouldn't change anything
132 132 nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer)
133 133 # None should do the same
134 134 nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer)
135 135 nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer)
136 136
137 137 def test_lookup():
138 138 f = PlainTextFormatter()
139 139
140 140 f.for_type(C, foo_printer)
141 141 nt.assert_is(f.lookup(C()), foo_printer)
142 142 with nt.assert_raises(KeyError):
143 143 f.lookup(A())
144 144
145 145 def test_lookup_string():
146 146 f = PlainTextFormatter()
147 147 type_str = '%s.%s' % (C.__module__, 'C')
148 148
149 149 f.for_type(type_str, foo_printer)
150 150 nt.assert_is(f.lookup(C()), foo_printer)
151 151 # should move from deferred to imported dict
152 152 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)
153 153 nt.assert_in(C, f.type_printers)
154 154
155 155 def test_lookup_by_type():
156 156 f = PlainTextFormatter()
157 157 f.for_type(C, foo_printer)
158 158 nt.assert_is(f.lookup_by_type(C), foo_printer)
159 159 type_str = '%s.%s' % (C.__module__, 'C')
160 160 with nt.assert_raises(KeyError):
161 161 f.lookup_by_type(A)
162 162
163 163 def test_lookup_by_type_string():
164 164 f = PlainTextFormatter()
165 165 type_str = '%s.%s' % (C.__module__, 'C')
166 166 f.for_type(type_str, foo_printer)
167 167
168 168 # verify insertion
169 169 nt.assert_in(_mod_name_key(C), f.deferred_printers)
170 170 nt.assert_not_in(C, f.type_printers)
171 171
172 172 nt.assert_is(f.lookup_by_type(type_str), foo_printer)
173 173 # lookup by string doesn't cause import
174 174 nt.assert_in(_mod_name_key(C), f.deferred_printers)
175 175 nt.assert_not_in(C, f.type_printers)
176 176
177 177 nt.assert_is(f.lookup_by_type(C), foo_printer)
178 178 # should move from deferred to imported dict
179 179 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)
180 180 nt.assert_in(C, f.type_printers)
181 181
182 182 def test_in_formatter():
183 183 f = PlainTextFormatter()
184 184 f.for_type(C, foo_printer)
185 185 type_str = '%s.%s' % (C.__module__, 'C')
186 186 nt.assert_in(C, f)
187 187 nt.assert_in(type_str, f)
188 188
189 189 def test_string_in_formatter():
190 190 f = PlainTextFormatter()
191 191 type_str = '%s.%s' % (C.__module__, 'C')
192 192 f.for_type(type_str, foo_printer)
193 193 nt.assert_in(type_str, f)
194 194 nt.assert_in(C, f)
195 195
196 196 def test_pop():
197 197 f = PlainTextFormatter()
198 198 f.for_type(C, foo_printer)
199 199 nt.assert_is(f.lookup_by_type(C), foo_printer)
200 200 nt.assert_is(f.pop(C, None), foo_printer)
201 201 f.for_type(C, foo_printer)
202 202 nt.assert_is(f.pop(C), foo_printer)
203 203 with nt.assert_raises(KeyError):
204 204 f.lookup_by_type(C)
205 205 with nt.assert_raises(KeyError):
206 206 f.pop(C)
207 207 with nt.assert_raises(KeyError):
208 208 f.pop(A)
209 209 nt.assert_is(f.pop(A, None), None)
210 210
211 211 def test_pop_string():
212 212 f = PlainTextFormatter()
213 213 type_str = '%s.%s' % (C.__module__, 'C')
214 214
215 215 with nt.assert_raises(KeyError):
216 216 f.pop(type_str)
217 217
218 218 f.for_type(type_str, foo_printer)
219 219 f.pop(type_str)
220 220 with nt.assert_raises(KeyError):
221 221 f.lookup_by_type(C)
222 222 with nt.assert_raises(KeyError):
223 223 f.pop(type_str)
224 224
225 225 f.for_type(C, foo_printer)
226 226 nt.assert_is(f.pop(type_str, None), foo_printer)
227 227 with nt.assert_raises(KeyError):
228 228 f.lookup_by_type(C)
229 229 with nt.assert_raises(KeyError):
230 230 f.pop(type_str)
231 231 nt.assert_is(f.pop(type_str, None), None)
232 232
233 233
234 234 def test_warn_error_method():
235 235 f = HTMLFormatter()
236 236 class BadHTML(object):
237 237 def _repr_html_(self):
238 238 return 1/0
239 239 bad = BadHTML()
240 240 with capture_output() as captured:
241 241 result = f(bad)
242 242 nt.assert_is(result, None)
243 nt.assert_in("WARNING", captured.stderr)
243 nt.assert_in("FormatterWarning", captured.stderr)
244 244 nt.assert_in("text/html", captured.stderr)
245 245 nt.assert_in("zero", captured.stderr)
246 246
247 247 def test_nowarn_notimplemented():
248 248 f = HTMLFormatter()
249 249 class HTMLNotImplemented(object):
250 250 def _repr_html_(self):
251 251 raise NotImplementedError
252 252 return 1/0
253 253 h = HTMLNotImplemented()
254 254 with capture_output() as captured:
255 255 result = f(h)
256 256 nt.assert_is(result, None)
257 nt.assert_not_in("WARNING", captured.stderr)
257 nt.assert_not_in("FormatterWarning", captured.stderr)
258 258
259 259 def test_warn_error_for_type():
260 260 f = HTMLFormatter()
261 261 f.for_type(int, lambda i: name_error)
262 262 with capture_output() as captured:
263 263 result = f(5)
264 264 nt.assert_is(result, None)
265 nt.assert_in("WARNING", captured.stderr)
265 nt.assert_in("FormatterWarning", captured.stderr)
266 266 nt.assert_in("text/html", captured.stderr)
267 267 nt.assert_in("name_error", captured.stderr)
268 268
269 269 def test_warn_error_pretty_method():
270 270 f = PlainTextFormatter()
271 271 class BadPretty(object):
272 272 def _repr_pretty_(self):
273 273 return "hello"
274 274 bad = BadPretty()
275 275 with capture_output() as captured:
276 276 result = f(bad)
277 277 nt.assert_is(result, None)
278 nt.assert_in("WARNING", captured.stderr)
278 nt.assert_in("FormatterWarning", captured.stderr)
279 279 nt.assert_in("text/plain", captured.stderr)
280 280 nt.assert_in("argument", captured.stderr)
281 281
282 282
General Comments 0
You need to be logged in to leave comments. Login now