##// END OF EJS Templates
tweak formatter.for_type...
MinRK -
Show More
@@ -46,6 +46,7 b' else:'
46 # The main DisplayFormatter class
46 # The main DisplayFormatter class
47 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
48
48
49 _current = object()
49
50
50 class DisplayFormatter(Configurable):
51 class DisplayFormatter(Configurable):
51
52
@@ -286,28 +287,41 b' class BaseFormatter(Configurable):'
286 else:
287 else:
287 return None
288 return None
288
289
289 def for_type(self, typ, func):
290 def for_type(self, typ, func=_current):
290 """Add a format function for a given type.
291 """Add a format function for a given type.
291
292
292 Parameters
293 Parameters
293 -----------
294 -----------
294 typ : class
295 typ : class
295 The class of the object that will be formatted using `func`.
296 The class of the object that will be formatted using `func`.
296 func : callable
297 func : callable
297 The callable that will be called to compute the format data. The
298 A callable for computing the format data.
298 call signature of this function is simple, it must take the
299 `func` will be called with the object to be formatted,
299 object to be formatted and return the raw data for the given
300 and will return the raw data in this formatter's format.
300 format. Subclasses may use a different call signature for the
301 Subclasses may use a different call signature for the
301 `func` argument.
302 `func` argument.
303
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,
308 only returning the current value.
309
310 Returns
311 -------
312 oldfunc : callable
313 The currently registered callable.
314 If you are registering a new formatter,
315 this will be the previous value (to enable restoring later).
302 """
316 """
303 oldfunc = self.type_printers.get(typ, None)
317 oldfunc = self.type_printers.get(typ, None)
304 if func is not None:
318 if func is None:
305 # To support easy restoration of old printers, we need to ignore
319 self.type_printers.pop(typ, None)
306 # Nones.
320 elif func is not _current:
307 self.type_printers[typ] = func
321 self.type_printers[typ] = func
308 return oldfunc
322 return oldfunc
309
323
310 def for_type_by_name(self, type_module, type_name, func):
324 def for_type_by_name(self, type_module, type_name, func=_current):
311 """Add a format function for a type specified by the full dotted
325 """Add a format function for a type specified by the full dotted
312 module and name of the type, rather than the type of the object.
326 module and name of the type, rather than the type of the object.
313
327
@@ -319,17 +333,30 b' class BaseFormatter(Configurable):'
319 type_name : str
333 type_name : str
320 The name of the type (the class name), like ``dtype``
334 The name of the type (the class name), like ``dtype``
321 func : callable
335 func : callable
322 The callable that will be called to compute the format data. The
336 A callable for computing the format data.
323 call signature of this function is simple, it must take the
337 `func` will be called with the object to be formatted,
324 object to be formatted and return the raw data for the given
338 and will return the raw data in this formatter's format.
325 format. Subclasses may use a different call signature for the
339 Subclasses may use a different call signature for the
326 `func` argument.
340 `func` argument.
341
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,
346 only returning the current value.
347
348 Returns
349 -------
350 oldfunc : callable
351 The currently registered callable.
352 If you are registering a new formatter,
353 this will be the previous value (to enable restoring later).
327 """
354 """
328 key = (type_module, type_name)
355 key = (type_module, type_name)
329 oldfunc = self.deferred_printers.get(key, None)
356 oldfunc = self.deferred_printers.get(key, None)
330 if func is not None:
357 if func is None:
331 # To support easy restoration of old printers, we need to ignore
358 self.deferred_printers.pop(key, None)
332 # Nones.
359 elif func is not _current:
333 self.deferred_printers[key] = func
360 self.deferred_printers[key] = func
334 return oldfunc
361 return oldfunc
335
362
General Comments 0
You need to be logged in to leave comments. Login now