コード例 #1
0
def _find_impl(cls, registry):
    """Returns the best matching implementation from *registry* for type *cls*.

    Where there is no registered implementation for a specific type, its method
    resolution order is used to find a more generic implementation.

    Note: if *registry* does not contain an implementation for the base
    *object* type, this function may return None.

    """
    mro = _compose_mro(cls, registry.keys())
    match = None
    for t in mro:
        if match is not None:
            # If *match* is an implicit ABC but there is another unrelated,
            # equally matching implicit ABC, refuse the temptation to guess.
            if (t in registry and t not in cls.__mro__
                              and match not in cls.__mro__
                              and not issubclass(match, t)):
                raise RuntimeError("Ambiguous dispatch: {} or {}".format(
                    match, t))
            break
        if t in registry:
            match = t
    return registry.get(match)
コード例 #2
0
def _find_impl_multi(clsTuple, registry):
    """Returns the best matching implementation from *registry* for type *cls*.

    Where there is no registered implementation for a specific type, its method
    resolution order is used to find a more generic implementation.

    Note: if *registry* does not contain an implementation for the base
    *object* type, this function may return None.

    """

    types_gen = (
        set(filter(lambda typ : typ is not None, types))
        for types in zip_longest(*registry.allkeys()))
    
    mros = tuple(_compose_mro(cls, types)
                 for cls, types in zip(clsTuple, types_gen))

    MaxMatchLen = 0
    IndexArgstypesList = []
    for argstypes in registry.allkeys():
        if not MaxMatchLen <= len(argstypes) <= len(mros):
            continue
        try:
            index = tuple(mro.index(argtype)
                          for mro, argtype in zip(mros, argstypes))
        except ValueError:
            continue
        
        if MaxMatchLen < len(index):
            MaxMatchLen = len(index)
            IndexArgstypesList.clear()
        IndexArgstypesList.append((index, argstypes))
    IndexArgstypesList.sort(key = itemgetter(0))

    match_indexs, match_typs = IndexArgstypesList.pop(0)
    for i, match_index_typ in enumerate(
        zip(match_indexs, match_typs)):
        match_index, match_typ = match_index_typ
        pivot = 0
        for temp, index_typ in enumerate(IndexArgstypesList):
            index, typ = map(itemgetter(i), index_typ)
            if typ not in clsTuple[i].__mro__\
               and match_typ not in clsTuple[i].__mro__\
               and not issubcluss(match_typ, typ):
                message = "Ambiguous dispatch: {} or {} at positional arguement {}"
                raise RuntimeError(message.format(match_typ, typ, i))
            if pivot == 0 and index > match_index:
                pivot = temp
        IndexArgstypesList = IndexArgstypesList[:pivot]
        
    return registry[match_typs]
コード例 #3
0
ファイル: allot.py プロジェクト: flying-sheep/allot
 def dispatch(self, cls: t.Type) -> t.Generator[t.Callable, None, None]:
     """
     generic_func.dispatch(cls) -> <function implementation>
     Runs the dispatch algorithm to return the best available implementation
     for the given *cls* registered on *generic_func*.
     """
     mro = _compose_mro(cls, self.registry.keys())
     match = None
     for typ in mro:
         if match is not None:
             # If *match* is an implicit ABC but there is another unrelated,
             # equally matching implicit ABC, refuse the temptation to guess.
             if (typ in self.registry and typ not in cls.__mro__
                     and match not in cls.__mro__
                     and not issubclass(match, typ)):
                 raise RuntimeError(f"Ambiguous dispatch: {match} or {typ}")
             yield self.registry.get(match)
         if typ in self.registry:
             match = typ
     yield self.registry.get(match)
コード例 #4
0
 def update_event(self, inp=-1):
     self.set_output_val(0, functools._compose_mro(self.input(0), self.input(1)))