Skip to content

Commit

Permalink
further prettified docs
Browse files Browse the repository at this point in the history
  • Loading branch information
falexwolf committed Feb 4, 2018
1 parent 3e78c2d commit e406116
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
73 changes: 73 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#

import ast
import os
import sys
import time
import inspect
from pathlib import Path
sys.path.insert(0, os.path.abspath(os.path.pardir))

from sphinx.ext import autosummary, autodoc
from sphinx.ext.autosummary import limited_join

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
Expand Down Expand Up @@ -213,3 +218,71 @@ def modurl(qualname):
from jinja2.defaults import DEFAULT_FILTERS

DEFAULT_FILTERS['modurl'] = modurl


# -- Prettier Autodoc -----------------------------------------------------


def f(string):
frame = sys._getframe(1)
return string.format_map(frame.f_locals)


def unparse(ast_node: ast.expr, plain: bool=False) -> str:
if isinstance(ast_node, ast.Attribute):
if plain:
return ast_node.attr
else:
v = unparse(ast_node.value, plain)
return f('{v}.{ast_node.attr}')
elif isinstance(ast_node, ast.Index):
return unparse(ast_node.value)
elif isinstance(ast_node, ast.Name):
return ast_node.id
elif isinstance(ast_node, ast.Subscript):
v = unparse(ast_node.value, plain)
s = unparse(ast_node.slice, plain)
return f('{v}[{s}]')
elif isinstance(ast_node, ast.Tuple):
return ', '.join(unparse(e) for e in ast_node.elts)
else:
t = type(ast_node)
raise NotImplementedError(f('can’t unparse {t}'))


def mangle_signature(sig: str, max_chars: int=30) -> str:
fn = ast.parse(f('def f{sig}: pass')).body[0]

args_all = [a.arg for a in fn.args.args]
n_a = len(args_all) - len(fn.args.defaults)
args = args_all[:n_a] # type: List[str]
opts = args_all[n_a:] # type: List[str]

# Produce a more compact signature
s = limited_join(', ', args, max_chars=max_chars - 2)
if opts:
if not s:
opts_str = limited_join(', ', opts, max_chars=max_chars - 4)
s = f('[{opts_str}]')
elif len(s) < max_chars - 4 - 2 - 3:
opts_str = limited_join(', ', opts, max_chars=max_chars - len(sig) - 4 - 2)
s += f('[, {opts_str}]')

if False: # fn.returns: # do not show return type in docs
ret = unparse(fn.returns, plain=True)
return f('({s}) -> {ret}')
return f('({s})')


autosummary.mangle_signature = mangle_signature

# TODO: also replace those for individual function pages:
# autodoc.formatargspec
# autodoc.format_annotation


if __name__ == '__main__':
print(mangle_signature('(filename: typing.Union[str, pathlib.Path], delim: int=0) -> anndata.base.AnnData'))
print(mangle_signature('(a, *, b=1) -> int'))
print(mangle_signature('(a, b=1, *c) -> Union[str, pathlib.Path]'))
print(mangle_signature('(a, b=1, *c, d=1)'))
6 changes: 3 additions & 3 deletions scanpy/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import scanpy.api as sc
PP - Preprocessing
Preprocessing: PP
------------------
Filtering of highly-variable genes, batch-effect correction, per-cell (UMI) normalization, preprocessing recipes.
Expand Down Expand Up @@ -37,7 +37,7 @@
pp.recipe_weinreb16
TL - Tools
Tools: TL
----------
**Embeddings**
Expand Down Expand Up @@ -131,7 +131,7 @@
datasets.toggleswitch
PL - Plotting
Plotting: PL
-------------
.. autosummary::
Expand Down
2 changes: 1 addition & 1 deletion scanpy/readwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def read(filename, backed=False, sheet=None, ext=None, delimiter=None,


def read_10x_h5(filename, genome='mm10'):
"""Read hdf5 file with naming conventions of 10X Genomics.
"""Read 10X-Genomics-formatted hdf5 file.
Parameters
----------
Expand Down

0 comments on commit e406116

Please sign in to comment.