22
33# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/04_docments.ipynb.
44
5- # %% auto 0
5+ # %% auto # 0
66__all__ = ['empty' , 'docstring' , 'parse_docstring' , 'isdataclass' , 'get_dataclass_source' , 'get_source' , 'get_name' , 'qual_name' ,
77 'docments' , 'sig_source' , 'extract_docstrings' , 'DocmentTbl' , 'DocmentList' , 'DocmentText' , 'sig2str' ,
88 'ShowDocRenderer' , 'MarkdownRenderer' ]
99
10- # %% ../nbs/04_docments.ipynb
10+ # %% ../nbs/04_docments.ipynb #4c662acc
1111import re ,ast ,inspect
1212from tokenize import tokenize ,COMMENT
1313from ast import parse ,FunctionDef ,AsyncFunctionDef ,AnnAssign
2222from textwrap import fill
2323from inspect import isclass ,getdoc ,signature
2424
25- # %% ../nbs/04_docments.ipynb
25+ # %% ../nbs/04_docments.ipynb #53e21187
2626def docstring (sym ):
2727 "Get docstring for `sym` for functions ad classes"
2828 if isinstance (sym , str ): return sym
2929 res = getdoc (sym )
3030 if not res and isclass (sym ): res = getdoc (sym .__init__ )
3131 return res or ""
3232
33- # %% ../nbs/04_docments.ipynb
33+ # %% ../nbs/04_docments.ipynb #1e0cf854
3434def parse_docstring (sym ):
3535 "Parse a numpy-style docstring in `sym`"
3636 return AttrDict (** docscrape .NumpyDocString (docstring (sym )))
3737
38- # %% ../nbs/04_docments.ipynb
38+ # %% ../nbs/04_docments.ipynb #b1a586d1
3939def isdataclass (s ):
4040 "Check if `s` is a dataclass but not a dataclass' instance"
4141 return is_dataclass (s ) and isclass (s )
4242
43- # %% ../nbs/04_docments.ipynb
43+ # %% ../nbs/04_docments.ipynb #6549c4b2
4444def get_dataclass_source (s ):
4545 "Get source code for dataclass `s`"
4646 return getsource (s ) if not getattr (s , "__module__" ) == '__main__' else ""
4747
48- # %% ../nbs/04_docments.ipynb
48+ # %% ../nbs/04_docments.ipynb #cac59989
4949def get_source (s ):
5050 "Get source code for string, function object or dataclass `s`"
5151 if isinstance (s ,str ): return s
5252 return getsource (s ) if isfunction (s ) or ismethod (s ) else get_dataclass_source (s ) if isdataclass (s ) else None
5353
54- # %% ../nbs/04_docments.ipynb
54+ # %% ../nbs/04_docments.ipynb #91c0d15f
5555def _parses (s ):
5656 "Parse Python code in string, function object or dataclass `s`"
5757 return parse (dedent (get_source (s ) or '' ))
@@ -87,10 +87,10 @@ def _param_locs(s, returns=True, args_kwargs=False):
8787 return res
8888 return None
8989
90- # %% ../nbs/04_docments.ipynb
90+ # %% ../nbs/04_docments.ipynb #93930c1f
9191empty = Parameter .empty
9292
93- # %% ../nbs/04_docments.ipynb
93+ # %% ../nbs/04_docments.ipynb #6434e80e
9494def _get_comment (line , arg , comments , parms ):
9595 if line in comments : return comments [line ].strip ()
9696 line -= 1
@@ -108,7 +108,7 @@ def _get_full(p, docs, eval_str=False):
108108 elif eval_str : anno = None
109109 return AttrDict (docment = docs .get (p .name ), anno = anno , default = p .default )
110110
111- # %% ../nbs/04_docments.ipynb
111+ # %% ../nbs/04_docments.ipynb #1b4d817c
112112def _merge_doc (dm , npdoc ):
113113 if not npdoc : return dm
114114 if not isinstance (dm , dict ): return dm or '\n ' .join (npdoc .desc )
@@ -122,14 +122,14 @@ def _merge_docs(dms, npdocs):
122122 if 'return' in dms : params ['return' ] = _merge_doc (dms ['return' ], npdocs ['Returns' ])
123123 return params
124124
125- # %% ../nbs/04_docments.ipynb
125+ # %% ../nbs/04_docments.ipynb #40e3f274
126126def _get_property_name (p ):
127127 "Get the name of property `p`"
128128 if hasattr (p , 'fget' ):
129129 return p .fget .func .__qualname__ if hasattr (p .fget , 'func' ) else p .fget .__qualname__
130130 else : return next (iter (re .findall (r'\'(.*)\'' , str (p )))).split ('.' )[- 1 ]
131131
132- # %% ../nbs/04_docments.ipynb
132+ # %% ../nbs/04_docments.ipynb #f5efac13
133133def get_name (obj ):
134134 "Get the name of `obj`"
135135 if hasattr (obj , '__name__' ): return obj .__name__
@@ -138,14 +138,14 @@ def get_name(obj):
138138 elif type (obj )== property : return _get_property_name (obj )
139139 else : return str (obj ).split ('.' )[- 1 ]
140140
141- # %% ../nbs/04_docments.ipynb
141+ # %% ../nbs/04_docments.ipynb #5e273f22
142142def qual_name (obj ):
143143 "Get the qualified name of `obj`"
144144 if hasattr (obj ,'__qualname__' ): return obj .__qualname__
145145 if ismethod (obj ): return f"{ get_name (obj .__self__ )} .{ get_name (fn )} "
146146 return get_name (obj )
147147
148- # %% ../nbs/04_docments.ipynb
148+ # %% ../nbs/04_docments.ipynb #9b62ab20
149149def docments (s , full = False , eval_str = False , returns = True , args_kwargs = False ):
150150 "Get docments for `s`"
151151 if isclass (s ) and not is_dataclass (s ): s = s .__init__
@@ -166,7 +166,7 @@ def docments(s, full=False, eval_str=False, returns=True, args_kwargs=False):
166166 else : res ['return' ] = docs .get ('return' )
167167 return AttrDict (_merge_docs (res , nps ))
168168
169- # %% ../nbs/04_docments.ipynb
169+ # %% ../nbs/04_docments.ipynb #40cdbeb2
170170def sig_source (obj ):
171171 "Full source of signature line(s) for a function or class."
172172 src = inspect .getsource (obj )
@@ -175,14 +175,14 @@ def sig_source(obj):
175175 if body_start == 1 : return src .splitlines ()[0 ]
176176 return '\n ' .join (src .splitlines ()[:body_start - 1 ])
177177
178- # %% ../nbs/04_docments.ipynb
178+ # %% ../nbs/04_docments.ipynb #6f0d6ea4
179179def _get_params (node ):
180180 params = [a .arg for a in node .args .args ]
181181 if node .args .vararg : params .append (f"*{ node .args .vararg .arg } " )
182182 if node .args .kwarg : params .append (f"**{ node .args .kwarg .arg } " )
183183 return ", " .join (params )
184184
185- # %% ../nbs/04_docments.ipynb
185+ # %% ../nbs/04_docments.ipynb #180b4c2d
186186class _DocstringExtractor (ast .NodeVisitor ):
187187 def __init__ (self ): self .docstrings ,self .cls ,self .cls_init = {},None ,None
188188
@@ -213,31 +213,31 @@ def visit_Module(self, node):
213213 if module_doc : self .docstrings ['_module' ] = (module_doc , "" )
214214 self .generic_visit (node )
215215
216- # %% ../nbs/04_docments.ipynb
216+ # %% ../nbs/04_docments.ipynb #b1d612e9
217217def extract_docstrings (code ):
218218 "Create a dict from function/class/method names to tuples of docstrings and param lists"
219219 extractor = _DocstringExtractor ()
220220 extractor .visit (ast .parse (code ))
221221 return extractor .docstrings
222222
223- # %% ../nbs/04_docments.ipynb
223+ # %% ../nbs/04_docments.ipynb #e4e1b815
224224def _non_empty_keys (d :dict ): return L ([k for k ,v in d .items () if v != inspect ._empty ])
225225def _bold (s ): return f'**{ s } **' if s .strip () else s
226226
227- # %% ../nbs/04_docments.ipynb
227+ # %% ../nbs/04_docments.ipynb #ac070254
228228def _escape_markdown (s ):
229229 for c in '|^' : s = re .sub (rf'\\?\{ c } ' , rf'\{ c } ' , s )
230230 return s .replace ('\n ' , '<br>' )
231231
232- # %% ../nbs/04_docments.ipynb
232+ # %% ../nbs/04_docments.ipynb #ced78f56
233233def _maybe_nm (o ):
234234 if (o == inspect ._empty ): return ''
235235 else : return o .__name__ if hasattr (o , '__name__' ) else str (o )
236236
237- # %% ../nbs/04_docments.ipynb
237+ # %% ../nbs/04_docments.ipynb #fe6d83f1
238238def _list2row (l :list ): return '| ' + ' | ' .join ([_maybe_nm (o ) for o in l ]) + ' |'
239239
240- # %% ../nbs/04_docments.ipynb
240+ # %% ../nbs/04_docments.ipynb #44ac2e4f
241241class _DocmentBase :
242242 def __init__ (self , obj ):
243243 self .obj ,self .dm = obj , docments (obj , full = True )
@@ -246,7 +246,7 @@ def __init__(self, obj):
246246 @property
247247 def has_docment (self ): return any (v .get ('docment' ) for v in self .dm .values ())
248248
249- # %% ../nbs/04_docments.ipynb
249+ # %% ../nbs/04_docments.ipynb #b45f731d
250250class DocmentTbl (_DocmentBase ):
251251 _map = {'anno' :'Type' , 'default' :'Default' , 'docment' :'Details' }
252252
@@ -303,7 +303,7 @@ def __eq__(self,other): return self.__str__() == str(other).strip()
303303 __str__ = _repr_markdown_
304304 __repr__ = basic_repr ()
305305
306- # %% ../nbs/04_docments.ipynb
306+ # %% ../nbs/04_docments.ipynb #5af61ab1
307307class DocmentList (_DocmentBase ):
308308 def _fmt (self , nm , p ):
309309 anno ,default ,doc = _maybe_nm (p .get ('anno' ,'' )), p .get ('default' ,empty ), p .get ('docment' ,'' )
@@ -314,7 +314,7 @@ def _fmt(self, nm, p):
314314 def _repr_markdown_ (self ): return '\n ' .join (self ._fmt (k ,v ) for k ,v in self .dm .items ())
315315 __repr__ = __str__ = _repr_markdown_
316316
317- # %% ../nbs/04_docments.ipynb
317+ # %% ../nbs/04_docments.ipynb #4550820f
318318class DocmentText (_DocmentBase ):
319319 def __init__ (self , obj , maxline = 110 , docstring = True ):
320320 super ().__init__ (obj )
@@ -351,17 +351,17 @@ def __str__(self):
351351 __repr__ = __str__
352352 def _repr_markdown_ (self ): return f"```python\n { self } \n ```"
353353
354- # %% ../nbs/04_docments.ipynb
354+ # %% ../nbs/04_docments.ipynb #d44a7fe3
355355def sig2str (func , maxline = 110 ):
356356 "Generate function signature with docments as comments"
357357 return DocmentText (func , maxline = maxline , docstring = False )
358358
359- # %% ../nbs/04_docments.ipynb
359+ # %% ../nbs/04_docments.ipynb #07287425
360360def _docstring (sym ):
361361 npdoc = parse_docstring (sym )
362362 return '\n \n ' .join ([npdoc ['Summary' ], npdoc ['Extended' ]]).strip ()
363363
364- # %% ../nbs/04_docments.ipynb
364+ # %% ../nbs/04_docments.ipynb #9de89cb6
365365def _fullname (o ):
366366 module ,name = getattr (o , "__module__" , None ),qual_name (o )
367367 return name if module is None or module in ('__main__' ,'builtins' ) else module + '.' + name
@@ -382,7 +382,7 @@ def __init__(self, sym, name:str|None=None, title_level:int=3, maxline:int=110):
382382
383383 __repr__ = basic_repr ()
384384
385- # %% ../nbs/04_docments.ipynb
385+ # %% ../nbs/04_docments.ipynb #e569d885
386386def _f_name (o ): return f'<function { o .__name__ } >' if isinstance (o , FunctionType ) else None
387387def _fmt_anno (o ): return inspect .formatannotation (o ).strip ("'" ).replace (' ' ,'' )
388388
@@ -395,12 +395,12 @@ def _show_param(param):
395395 if default is not inspect ._empty : res += f'={ _f_name (default ) or repr (default )} '
396396 return res
397397
398- # %% ../nbs/04_docments.ipynb
398+ # %% ../nbs/04_docments.ipynb #59797eb7
399399def _ital_first (s :str ):
400400 "Surround first line with * for markdown italics, preserving leading spaces"
401401 return re .sub (r'^(\s*)(.+)' , r'\1*\2*' , s , count = 1 )
402402
403- # %% ../nbs/04_docments.ipynb
403+ # %% ../nbs/04_docments.ipynb #9184b175
404404class MarkdownRenderer (ShowDocRenderer ):
405405 "Markdown renderer for `show_doc`"
406406 def _repr_markdown_ (self ):
0 commit comments