MCPcopy Create free account
hub / github.com/numpy/numpy / upgrade_mapper

Method upgrade_mapper

numpy/lib/_iotools.py:543–581  ·  view source on GitHub ↗

Upgrade the mapper of a StringConverter by adding a new function and its corresponding default. The input function (or sequence of functions) and its associated default value (if any) is inserted in penultimate position of the mapper. The corresponding type

(cls, func, default=None)

Source from the content-addressed store, hash-verified

541
542 @classmethod
543 def upgrade_mapper(cls, func, default=None):
544 """
545 Upgrade the mapper of a StringConverter by adding a new function and
546 its corresponding default.
547
548 The input function (or sequence of functions) and its associated
549 default value (if any) is inserted in penultimate position of the
550 mapper. The corresponding type is estimated from the dtype of the
551 default value.
552
553 Parameters
554 ----------
555 func : var
556 Function, or sequence of functions
557
558 Examples
559 --------
560 >>> import dateutil.parser
561 >>> import datetime
562 >>> dateparser = dateutil.parser.parse
563 >>> defaultdate = datetime.date(2000, 1, 1)
564 >>> StringConverter.upgrade_mapper(dateparser, default=defaultdate)
565 """
566 # Func is a single functions
567 if hasattr(func, '__call__'):
568 cls._mapper.insert(-1, (cls._getsubdtype(default), func, default))
569 return
570 elif hasattr(func, '__iter__'):
571 if isinstance(func[0], (tuple, list)):
572 for _ in func:
573 cls._mapper.insert(-1, _)
574 return
575 if default is None:
576 default = [None] * len(func)
577 else:
578 default = list(default)
579 default.append([None] * (len(func) - len(default)))
580 for fct, dft in zip(func, default):
581 cls._mapper.insert(-1, (cls._getsubdtype(dft), fct, dft))
582
583 @classmethod
584 def _find_map_entry(cls, dtype):

Callers 1

test_upgrademapperMethod · 0.80

Calls 1

_getsubdtypeMethod · 0.80

Tested by 1

test_upgrademapperMethod · 0.64