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