Last active
February 27, 2023 16:02
-
-
Save rochacbruno/14d815b69782c11ff8be3d55e602b375 to your computer and use it in GitHub Desktop.
List of Python Protocols https://docs.python.org/3/reference/datamodel.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
'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__'] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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