Created
March 24, 2020 10:20
-
-
Save victorusachev/191480ac8e4247ed25154c1c2cf8c14b to your computer and use it in GitHub Desktop.
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
| from itertools import groupby | |
| from operator import itemgetter | |
| def get_subclasses(cls: type): | |
| for subclass in cls.__subclasses__(): | |
| yield subclass | |
| yield from get_subclasses(subclass) | |
| def get_notifications(cls: type, prefixes: list): | |
| classes = map( | |
| lambda c: (c.__module__, c.__qualname__), | |
| filter( | |
| lambda c: not any(map(c.__name__.startswith, prefixes)), | |
| get_subclasses(cls), | |
| ) | |
| ) | |
| return sorted(classes) | |
| def format_for_import(classes, single_line=False): | |
| if single_line: | |
| for module, cls in classes: | |
| yield f"from {module} import {cls}" | |
| else: | |
| for module, group in groupby(classes, itemgetter(0)): | |
| classes = map(itemgetter(1), group) | |
| yield f"from {module} import ({', '.join(classes)})" | |
| if __name__ == '__main__': | |
| from .notification.base import AbstractNotification | |
| notifications = get_notifications(AbstractNotification, ['_', 'Abstract', 'Base']) | |
| for statement in format_for_import(notifications, True): | |
| print(f'{statement}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment