##// END OF EJS Templates
tweak formatter.for_type...
MinRK -
Show More
@@ -46,6 +46,7 b' else:'
46 46 # The main DisplayFormatter class
47 47 #-----------------------------------------------------------------------------
48 48
49 _current = object()
49 50
50 51 class DisplayFormatter(Configurable):
51 52
@@ -286,7 +287,7 b' class BaseFormatter(Configurable):'
286 287 else:
287 288 return None
288 289
289 def for_type(self, typ, func):
290 def for_type(self, typ, func=_current):
290 291 """Add a format function for a given type.
291 292
292 293 Parameters
@@ -294,20 +295,33 b' class BaseFormatter(Configurable):'
294 295 typ : class
295 296 The class of the object that will be formatted using `func`.
296 297 func : callable
297 The callable that will be called to compute the format data. The
298 call signature of this function is simple, it must take the
299 object to be formatted and return the raw data for the given
300 format. Subclasses may use a different call signature for the
298 A callable for computing the format data.
299 `func` will be called with the object to be formatted,
300 and will return the raw data in this formatter's format.
301 Subclasses may use a different call signature for the
301 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 317 oldfunc = self.type_printers.get(typ, None)
304 if func is not None:
305 # To support easy restoration of old printers, we need to ignore
306 # Nones.
318 if func is None:
319 self.type_printers.pop(typ, None)
320 elif func is not _current:
307 321 self.type_printers[typ] = func
308 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 325 """Add a format function for a type specified by the full dotted
312 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 333 type_name : str
320 334 The name of the type (the class name), like ``dtype``
321 335 func : callable
322 The callable that will be called to compute the format data. The
323 call signature of this function is simple, it must take the
324 object to be formatted and return the raw data for the given
325 format. Subclasses may use a different call signature for the
336 A callable for computing the format data.
337 `func` will be called with the object to be formatted,
338 and will return the raw data in this formatter's format.
339 Subclasses may use a different call signature for the
326 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 355 key = (type_module, type_name)
329 356 oldfunc = self.deferred_printers.get(key, None)
330 if func is not None:
331 # To support easy restoration of old printers, we need to ignore
332 # Nones.
357 if func is None:
358 self.deferred_printers.pop(key, None)
359 elif func is not _current:
333 360 self.deferred_printers[key] = func
334 361 return oldfunc
335 362
General Comments 0
You need to be logged in to leave comments. Login now