Created
February 6, 2021 13:03
-
-
Save matteoferla/b65afc3fd6a6d61bfe27c9382ecc4934 to your computer and use it in GitHub Desktop.
Print out what the attributes are for copy pasting for Sphinx documentation
This file contains 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
""" | |
In the docstring for a class the variables can be defined with ``:var xxx:``, ``:ivar xxx:`` and ``:cvar xxx:``. | |
This prints a block for a given class instance to make it easier. | |
Dynamic attributes will be marked as class variables. | |
Private attributes will not show. | |
NB. Writing ``#:`` is probably easier and saner, but has to be done during coding... | |
""" | |
def sphinxify_vars(obj): | |
ivars = set(dir(obj)) - set(dir(obj.__class__)) | |
for key in sorted(dir(obj)): | |
if key[0] == '_': | |
continue | |
if getattr(obj, key).__class__.__name__ in ('type', 'builtin_function_or_method', 'method-wrapper', 'method'): | |
continue | |
if key in ivars: | |
print(f':ivar {key}:') | |
else: | |
print(f':cvar {key}:') | |
print(f':vartype {key}: {getattr(obj, key).__class__.__name__}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment