def func2md(self, func, clsname=None, names=None, depth=3):
        """Takes a function (or method) and documents it.

        Args:
            clsname (str, optional): class name to prepend to funcname.
            depth (int, optional): number of ### to append to function name

        """
        section = "#" * depth
        if names is None:
            names = [func.__name__]

        funcname = ", ".join(names)
        escfuncname = ", ".join(["`%s`" % funcname if funcname.startswith("_") else funcname for funcname in names])
        header = "%s%s" % ("%s." % clsname if clsname else "", escfuncname)

        path = self.get_src_path(func)
        doc = self.doc2md(func)

        args, kwargs = [], []
        spec = getargspec(func)
        vargsname, kwargsname = spec.varargs, spec.keywords
        vargs = list(make_iter(spec.args)) if spec.args else []
        defaults = list(make_iter(spec.defaults)) if spec.defaults else []

        while vargs:
            if vargs and vargs[0] == "self":
                args.append(vargs.pop(0))
            elif len(vargs) > len(defaults):
                args.append(vargs.pop(0))
            else:
                default = defaults.pop(0)
                if isinstance(default, str):
                    default = "\"%s\"" % default
                else:
                    default = "%s" % str(default)

                kwargs.append((vargs.pop(0), default))

        if args:
            args = ", ".join("%s" % arg for arg in args)
        if kwargs:
            kwargs = ", ".join("%s=%s" % kwarg for kwarg in kwargs)
            if args:
                kwargs = ", " + kwargs
        if vargsname:
            vargsname = "*%s" % vargsname
            if args or kwargs:
                vargsname = ", " + vargsname
        if kwargsname:
            kwargsname = "**%s" % kwargsname
            if args or kwargs or vargsname:
                kwargsname = ", " + kwargsname

        _FUNCDEF = "{funcname}({args}{kwargs}{vargs}{vkwargs})"
        funcdef = _FUNCDEF.format(funcname=funcname,
                                  args=args or "",
                                  kwargs=kwargs or "",
                                  vargs=vargsname or "",
                                  vkwargs=kwargsname or "")

        # split the function definition if it is too long
        lmax = 90
        if len(funcdef) > lmax:
            # wrap in the args list
            split = funcdef.split("(", 1)
            # we gradually build the string again
            rest = split[1]
            args = rest.split(", ")

            funcname = "(".join(split[:1]) + "("
            lline = len(funcname)
            parts = []
            for arg in args:
                larg = len(arg)
                if larg > lmax - 5:
                    # not much to do if arg is so long
                    parts.append(arg)
                elif lline + larg > lmax:
                    # the next arg is too long, break the line
                    parts.append("\\\n    " + arg)
                    lline = 0
                else:
                    parts.append(arg)
                lline += len(parts[-1])
            funcdef = funcname + ", ".join(parts)

        # build the signature
        string = FUNC_TEMPLATE.format(section=section,
                                      header=header,
                                      funcdef=funcdef,
                                      path=path,
                                      doc=doc if doc else "*No documentation found.*")
        return string
def func2md ( self , func , clsname = None , names = None , depth = 3 ) : section = "#" * depth if names is None : names = [ func . __name__ ] funcname = ", " . join ( names ) escfuncname = ", " . join ( [ "`%s`" % funcname if funcname . startswith ( "_" ) else funcname for funcname in names ] ) header = "%s%s" % ( "%s." % clsname if clsname else "" , escfuncname ) path = self . get_src_path ( func ) doc = self . doc2md ( func ) args , kwargs = [ ] , [ ] spec = getargspec ( func ) vargsname , kwargsname = spec . varargs , spec . keywords vargs = list ( make_iter ( spec . args ) ) if spec . args else [ ] defaults = list ( make_iter ( spec . defaults ) ) if spec . defaults else [ ] while vargs : if vargs and vargs [ 0 ] == "self" : args . append ( vargs . pop ( 0 ) ) elif len ( vargs ) > len ( defaults ) : args . append ( vargs . pop ( 0 ) ) else : default = defaults . pop ( 0 ) if isinstance ( default , str ) : default = "\"%s\"" % default else : default = "%s" % str ( default ) kwargs . append ( ( vargs . pop ( 0 ) , default ) ) if args : args = ", " . join ( "%s" % arg for arg in args ) if kwargs : kwargs = ", " . join ( "%s=%s" % kwarg for kwarg in kwargs ) if args : kwargs = ", " + kwargs if vargsname : vargsname = "*%s" % vargsname if args or kwargs : vargsname = ", " + vargsname if kwargsname : kwargsname = "**%s" % kwargsname if args or kwargs or vargsname : kwargsname = ", " + kwargsname _FUNCDEF = "{funcname}({args}{kwargs}{vargs}{vkwargs})" funcdef = _FUNCDEF . format ( funcname = funcname , args = args or "" , kwargs = kwargs or "" , vargs = vargsname or "" , vkwargs = kwargsname or "" ) lmax = 90 if len ( funcdef ) > lmax : split = funcdef . split ( "(" , 1 ) rest = split [ 1 ] args = rest . split ( ", " ) funcname = "(" . join ( split [ : 1 ] ) + "(" lline = len ( funcname ) parts = [ ] for arg in args : larg = len ( arg ) if larg > lmax - 5 : parts . append ( arg ) elif lline + larg > lmax : parts . append ( "\\\n    " + arg ) lline = 0 else : parts . append ( arg ) lline += len ( parts [ - 1 ] ) funcdef = funcname + ", " . join ( parts ) string = FUNC_TEMPLATE . format ( section = section , header = header , funcdef = funcdef , path = path , doc = doc if doc else "*No documentation found.*" ) return string
Takes a function (or method) and documents it.

        Args:
            clsname (str, optional): class name to prepend to funcname.
            depth (int, optional): number of ### to append to function name