def as_interface(obj, cls=None, methods=None, required=None):
    if not cls and not methods:
        raise TypeError('a class or collection of method names are required')

    if isinstance(cls, type) and isinstance(obj, cls):
        return obj

    interface = Set(methods or [m for m in dir(cls) if not m.startswith('_')])
    implemented = Set(dir(obj))

    complies = operator.ge
    if isinstance(required, type):
        required = interface
    elif not required:
        required = Set()
        complies = operator.gt
    else:
        required = Set(required)

    if complies(implemented.intersection(interface), required):
        return obj

    # No dict duck typing here.
    if not type(obj) is dict:
        qualifier = complies is operator.gt and 'any of' or 'all of'
        raise TypeError("%r does not implement %s: %s" % (
            obj, qualifier, ', '.join(interface)))

    class AnonymousInterface(object):
        """A callable-holding shell."""

    if cls:
        AnonymousInterface.__name__ = 'Anonymous' + cls.__name__
    found = Set()

    for method, impl in dictlike_iteritems(obj):
        if method not in interface:
            raise TypeError("%r: unknown in this interface" % method)
        if not callable(impl):
            raise TypeError("%r=%r is not callable" % (method, impl))
        setattr(AnonymousInterface, method, staticmethod(impl))
        found.add(method)

    if complies(found, required):
        return AnonymousInterface

    raise TypeError("dictionary does not contain required keys %s" %
                    ', '.join(required - found))
Returns an ID that says what this instance is cached as remotely, or C{None} if it's not.
dataset/ETHPy150Open ralfonso/theory/theory/model/mpdutil.py/as_interface--crossover with--dataset/ETHPy150Open nlloyd/SubliminalCollaborator/libs/twisted/spread/pb.py/Broker.cachedRemotelyAs