Skip to content

Instantly share code, notes, and snippets.

@matteoferla
Created February 6, 2021 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matteoferla/b65afc3fd6a6d61bfe27c9382ecc4934 to your computer and use it in GitHub Desktop.
Save matteoferla/b65afc3fd6a6d61bfe27c9382ecc4934 to your computer and use it in GitHub Desktop.
Print out what the attributes are for copy pasting for Sphinx documentation
"""
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