Skip to content

Instantly share code, notes, and snippets.

@rochacbruno
Last active February 27, 2023 16:02
Show Gist options
  • Save rochacbruno/14d815b69782c11ff8be3d55e602b375 to your computer and use it in GitHub Desktop.
Save rochacbruno/14d815b69782c11ff8be3d55e602b375 to your computer and use it in GitHub Desktop.
{
'AsyncGenerator': ['asend', 'athrow'],
'AsyncIterable': ['__aiter__'],
'AsyncIterator': ['__anext__'],
'Awaitable': ['__await__'],
'ByteString': ['__getitem__', '__len__'],
'Callable': ['__call__'],
'Collection': ['__contains__', '__iter__', '__len__'],
'Container': ['__contains__'],
'Coroutine': ['__await__', 'send', 'throw'],
'Generator': ['send', 'throw'],
'Hashable': ['__hash__'],
'Iterable': ['__iter__'],
'Iterator': ['__next__'],
'Mapping': ['__getitem__', '__iter__', '__len__'],
'MutableMapping': ['__delitem__', '__getitem__', '__iter__', '__len__', '__setitem__'],
'MutableSequence': ['__delitem__', '__getitem__', '__len__', '__setitem__', 'insert'],
'MutableSet': ['__contains__', '__iter__', '__len__', 'add', 'discard'],
'Reversible': ['__iter__', '__reversed__'],
'Sequence': ['__getitem__', '__len__'],
'Set': ['__contains__', '__iter__', '__len__'],
'Sized': ['__len__']
}
import abc
import collections as ct
def get_protocols(source=ct.abc):
"""Return a dict of protocols from `collections.abc`."""
d = {}
for objname in dir(source):
if objname.startswith("_"):
continue
obj = getattr(source, objname)
abmethods = sorted(obj.__abstractmethods__)
if not abmethods:
continue
d[objname] = abmethods
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment