PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/summarize.py

https://github.com/plaes/numpy
Python | 171 lines | 160 code | 4 blank | 7 comment | 0 complexity | 94944e5accfb4252f77269d591a8b635 MD5 | raw file
  1. #!/usr/bin/env python
  2. """
  3. summarize.py
  4. Show a summary about which Numpy functions are documented and which are not.
  5. """
  6. import os, glob, re, sys, inspect, optparse
  7. sys.path.append(os.path.join(os.path.dirname(__file__), 'sphinxext'))
  8. from sphinxext.phantom_import import import_phantom_module
  9. from sphinxext.autosummary_generate import get_documented
  10. CUR_DIR = os.path.dirname(__file__)
  11. SOURCE_DIR = os.path.join(CUR_DIR, 'source', 'reference')
  12. SKIP_LIST = """
  13. # --- aliases:
  14. alltrue sometrue bitwise_not cumproduct
  15. row_stack column_stack product rank
  16. # -- skipped:
  17. core lib f2py dual doc emath ma rec char distutils oldnumeric numarray
  18. testing version matlib
  19. add_docstring add_newdoc add_newdocs fastCopyAndTranspose pkgload
  20. conjugate disp
  21. int0 object0 unicode0 uint0 string_ string0 void0
  22. flagsobj
  23. setup setupscons PackageLoader
  24. lib.scimath.arccos lib.scimath.arcsin lib.scimath.arccosh lib.scimath.arcsinh
  25. lib.scimath.arctanh lib.scimath.log lib.scimath.log2 lib.scimath.log10
  26. lib.scimath.logn lib.scimath.power lib.scimath.sqrt
  27. # --- numpy.random:
  28. random random.info random.mtrand random.ranf random.sample random.random
  29. # --- numpy.fft:
  30. fft fft.Tester fft.bench fft.fftpack fft.fftpack_lite fft.helper
  31. fft.refft fft.refft2 fft.refftn fft.irefft fft.irefft2 fft.irefftn
  32. fft.info fft.test
  33. # --- numpy.linalg:
  34. linalg linalg.Tester
  35. linalg.bench linalg.info linalg.lapack_lite linalg.linalg linalg.test
  36. # --- numpy.ctypeslib:
  37. ctypeslib ctypeslib.test
  38. """.split()
  39. def main():
  40. p = optparse.OptionParser(__doc__)
  41. p.add_option("-c", "--columns", action="store", type="int", dest="cols",
  42. default=3, help="Maximum number of columns")
  43. options, args = p.parse_args()
  44. if len(args) != 0:
  45. p.error('Wrong number of arguments')
  46. # prepare
  47. fn = os.path.join(CUR_DIR, 'dump.xml')
  48. if os.path.isfile(fn):
  49. import_phantom_module(fn)
  50. # check
  51. documented, undocumented = check_numpy()
  52. # report
  53. in_sections = {}
  54. for name, locations in documented.iteritems():
  55. for (filename, section, keyword, toctree) in locations:
  56. in_sections.setdefault((filename, section, keyword), []).append(name)
  57. print "Documented"
  58. print "==========\n"
  59. last_filename = None
  60. for (filename, section, keyword), names in sorted(in_sections.items()):
  61. if filename != last_filename:
  62. print "--- %s\n" % filename
  63. last_filename = filename
  64. print " ** ", section
  65. print format_in_columns(sorted(names), options.cols)
  66. print "\n"
  67. print ""
  68. print "Undocumented"
  69. print "============\n"
  70. print format_in_columns(sorted(undocumented.keys()), options.cols)
  71. def check_numpy():
  72. documented = get_documented(glob.glob(SOURCE_DIR + '/*.rst'))
  73. undocumented = {}
  74. import numpy, numpy.fft, numpy.linalg, numpy.random
  75. for mod in [numpy, numpy.fft, numpy.linalg, numpy.random,
  76. numpy.ctypeslib, numpy.emath, numpy.ma]:
  77. undocumented.update(get_undocumented(documented, mod, skip=SKIP_LIST))
  78. for d in (documented, undocumented):
  79. for k in d.keys():
  80. if k.startswith('numpy.'):
  81. d[k[6:]] = d[k]
  82. del d[k]
  83. return documented, undocumented
  84. def get_undocumented(documented, module, module_name=None, skip=[]):
  85. """
  86. Find out which items in Numpy are not documented.
  87. Returns
  88. -------
  89. undocumented : dict of bool
  90. Dictionary containing True for each documented item name
  91. and False for each undocumented one.
  92. """
  93. undocumented = {}
  94. if module_name is None:
  95. module_name = module.__name__
  96. for name in dir(module):
  97. obj = getattr(module, name)
  98. if name.startswith('_'): continue
  99. full_name = '.'.join([module_name, name])
  100. if full_name in skip: continue
  101. if full_name.startswith('numpy.') and full_name[6:] in skip: continue
  102. if not (inspect.ismodule(obj) or callable(obj) or inspect.isclass(obj)):
  103. continue
  104. if full_name not in documented:
  105. undocumented[full_name] = True
  106. return undocumented
  107. def format_in_columns(lst, max_columns):
  108. """
  109. Format a list containing strings to a string containing the items
  110. in columns.
  111. """
  112. lst = map(str, lst)
  113. col_len = max(map(len, lst)) + 2
  114. ncols = 80//col_len
  115. if ncols > max_columns:
  116. ncols = max_columns
  117. if ncols <= 0:
  118. ncols = 1
  119. if len(lst) % ncols == 0:
  120. nrows = len(lst)//ncols
  121. else:
  122. nrows = 1 + len(lst)//ncols
  123. fmt = ' %%-%ds ' % (col_len-2)
  124. lines = []
  125. for n in range(nrows):
  126. lines.append("".join([fmt % x for x in lst[n::nrows]]))
  127. return "\n".join(lines)
  128. if __name__ == "__main__": main()