python - What's the pythonic way to assign default values to be printed by help()? -
python - What's the pythonic way to assign default values to be printed by help()? -
i have written next script load , assign default values fields help()
searches for:
import os os.chdir(os.path.dirname(os.path.abspath(__file__ ))) def _get_authors(): path = os.path.dirname(os.path.abspath(__file__ )) open('%s\authors' % path, 'r') authors: homecoming ', '.join([author author in ''.join(authors.readlines()).splitlines()]) def _get_readme(): path = os.path.dirname(os.path.abspath(__file__ )) open('%s\readme' % path, 'r') f: homecoming ''.join(f.readlines()) def _get_copyright(): import datetime start = 2011 end = datetime.date.today().year if start == end: years = str(start) else: years = "%d - %d" % (start, end) homecoming "copyright %s, %s" % (years, __maintainer__) __doc__ = _get_readme() __author__ = _get_authors() __maintainer__ = "omer katz" __email__ = "omer.drow@gmail.com" __copyright__ = _get_copyright() __license__ = "bsd" __version__ = "0.1.0" __status__ = "pre-alpha" def _document_api(): def _document(obj): if not getattr(obj, '__author__', none): setattr(obj, '__author__', __maintainer__) if not getattr(obj, '__maintainer__', none): setattr(obj, '__maintainer__', __maintainer__) if not getattr(obj, '__email__', none): setattr(obj, '__email__', __email__) if not getattr(obj, '__copyright__', none): setattr(obj, '__copyright__', __copyright__) if not getattr(obj, '__license__', none): setattr(obj, '__license__', __license__) if not getattr(obj, '__version__', none): setattr(obj, '__version__', __copyright__) if not getattr(obj, '__status__', none): setattr(obj, '__status__', __license__) def _document_functions(module): inspect import isfunction functions = [getattr(module, function) function in dir(module) if isfunction(getattr(module, function)) , function != '_'] function in functions: _document(function) def _document_classes(module): inspect import isclass classes = [getattr(module, klass) klass in dir(module) if isclass(getattr(module, klass)) , klass != '_'] klass in classes: _document_functions(klass) _document(klass) pkgutil import walk_packages django.utils.importlib import import_module packages = [package _, package, __ in walk_packages([os.path.dirname(os.path.abspath(__file__ ))])] bundle in packages: module = import_module('hammerhead.%s' % package) _document_functions(module) _document_classes(module) _document(module) _document_api()
is there more pythonic way it? idea? help()
able print out right metadata if not provided.
any comments on code appreciated.
the pythonic way (imho) add together docstrings functions, methods , classes need documentation, , create single header (in __init__.py
file module) sets rest of values module.
__author__
, __email__
, etc defined , used modules anyways. (see pydoc.)
python docstring
Comments
Post a Comment