Created
February 25, 2014 10:16
-
-
Save nicksnell/9206327 to your computer and use it in GitHub Desktop.
Utility to pull all the callable methods from a given Django model. Tries to ignore default internal methods.
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 yaml | |
| DEFAULT_IGNORE = [ | |
| '__unicode__', '__module__', '_meta', 'MultipleObjectsReturned', '_base_manager', | |
| 'objects', 'DoesNotExist', '__doc__', '_default_manager', '_curried', | |
| ] | |
| def analyze_model(model, ignore=[], default_ignore=True): | |
| """Analyze a Django Model to return a list of methods along with | |
| the methods docstring""" | |
| if default_ignore: | |
| ignore.extend(DEFAULT_IGNORE) | |
| model_keys = model.__dict__.keys() | |
| methods = [key for key in model_keys if not key in ignore] | |
| results = {} | |
| for method in methods: | |
| try: | |
| model_method = getattr(model, method) | |
| except AttributeError: | |
| continue | |
| if callable(model_method) and not model_method.__name__ == '_curried': | |
| results[method] = model_method.__doc__ | |
| return yaml.dump({model.__name__: results}, default_flow_style=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment