77__all__ = ['find_var' , 'read_var' , 'update_var' , 'ModuleMaker' , 'decor_id' , 'retr_exports' , 'make_code_cells' , 'relative_import' ,
88 'update_import' ]
99
10- # %% ../nbs/09_API/02_maker.ipynb 4
10+ # %% ../nbs/09_API/02_maker.ipynb 3
1111from .config import *
1212from .imports import *
1313
2222from pprint import pformat
2323from textwrap import TextWrapper
2424
25- # %% ../nbs/09_API/02_maker.ipynb 9
25+ # %% ../nbs/09_API/02_maker.ipynb 8
2626def find_var (lines , varname ):
2727 "Find the line numbers where `varname` is defined in `lines`"
2828 start = first (i for i ,o in enumerate (lines ) if o .startswith (varname ))
@@ -32,7 +32,7 @@ def find_var(lines, varname):
3232 end = first (i for i ,o in enumerate (lines [start + 1 :]) if o [:1 ] not in empty )
3333 return start ,len (lines ) if end is None else (end + start + 1 )
3434
35- # %% ../nbs/09_API/02_maker.ipynb 11
35+ # %% ../nbs/09_API/02_maker.ipynb 10
3636def read_var (code , varname ):
3737 "Eval and return the value of `varname` defined in `code`"
3838 lines = code .splitlines ()
@@ -43,7 +43,7 @@ def read_var(code, varname):
4343 try : return eval ('\n ' .join (res ))
4444 except SyntaxError : raise Exception ('\n ' .join (res )) from None
4545
46- # %% ../nbs/09_API/02_maker.ipynb 13
46+ # %% ../nbs/09_API/02_maker.ipynb 12
4747def update_var (varname , func , fn = None , code = None ):
4848 "Update the definition of `varname` in file `fn`, by calling `func` with the current definition"
4949 if fn :
@@ -59,7 +59,7 @@ def update_var(varname, func, fn=None, code=None):
5959 if fn : fn .write_text (code )
6060 else : return code
6161
62- # %% ../nbs/09_API/02_maker.ipynb 16
62+ # %% ../nbs/09_API/02_maker.ipynb 15
6363class ModuleMaker :
6464 "Helper class to create exported library from notebook source cells"
6565 def __init__ (self , dest , name , nb_path , is_new = True , parse = True ):
@@ -71,12 +71,12 @@ def __init__(self, dest, name, nb_path, is_new=True, parse=True):
7171 self .dest2nb = nb_path .relpath (self .fname .parent )
7272 self .hdr = f"# %% { self .dest2nb } "
7373
74- # %% ../nbs/09_API/02_maker.ipynb 19
74+ # %% ../nbs/09_API/02_maker.ipynb 18
7575def decor_id (d ):
7676 "`id` attr of decorator, regardless of whether called as function or bare"
7777 return d .id if hasattr (d , 'id' ) else nested_attr (d , 'func.id' , '' )
7878
79- # %% ../nbs/09_API/02_maker.ipynb 20
79+ # %% ../nbs/09_API/02_maker.ipynb 19
8080_def_types = ast .FunctionDef ,ast .AsyncFunctionDef ,ast .ClassDef
8181_assign_types = ast .AnnAssign , ast .Assign , ast .AugAssign
8282
@@ -87,7 +87,7 @@ def _all_targets(a): return L(getattr(a,'elts',a))
8787def _filt_dec (x ): return decor_id (x ).startswith ('patch' )
8888def _wants (o ): return isinstance (o ,_def_types ) and not any (L (o .decorator_list ).filter (_filt_dec ))
8989
90- # %% ../nbs/09_API/02_maker.ipynb 21
90+ # %% ../nbs/09_API/02_maker.ipynb 20
9191def retr_exports (trees ):
9292 # include anything mentioned in "_all_", even if otherwise private
9393 # NB: "_all_" can include strings (names), or symbols, so we look for "id" or "value"
@@ -100,17 +100,17 @@ def retr_exports(trees):
100100 exports = (assign_targs .attrgot ('id' )+ syms ).filter (lambda o : o and o [0 ]!= '_' )
101101 return (exports + all_vals ).unique ()
102102
103- # %% ../nbs/09_API/02_maker.ipynb 22
103+ # %% ../nbs/09_API/02_maker.ipynb 21
104104@patch
105105def make_all (self :ModuleMaker , cells ):
106106 "Create `__all__` with all exports in `cells`"
107107 if cells is None : return ''
108108 return retr_exports (L (cells ).map (NbCell .parsed_ ).concat ())
109109
110- # %% ../nbs/09_API/02_maker.ipynb 23
110+ # %% ../nbs/09_API/02_maker.ipynb 22
111111def make_code_cells (* ss ): return dict2nb ({'cells' :L (ss ).map (mk_cell )}).cells
112112
113- # %% ../nbs/09_API/02_maker.ipynb 26
113+ # %% ../nbs/09_API/02_maker.ipynb 25
114114def relative_import (name , fname , level = 0 ):
115115 "Convert a module `name` to a name relative to `fname`"
116116 assert not level
@@ -122,7 +122,7 @@ def relative_import(name, fname, level=0):
122122 if not all (o == '.' for o in res ): res = '.' + res
123123 return res .replace (os .path .sep , "." )
124124
125- # %% ../nbs/09_API/02_maker.ipynb 28
125+ # %% ../nbs/09_API/02_maker.ipynb 27
126126# Based on https://github.com/thonny/thonny/blob/master/thonny/ast_utils.py
127127def _mark_text_ranges (
128128 source : str | bytes , # Source code to add ranges to
@@ -140,7 +140,7 @@ def _mark_text_ranges(
140140 child .end_lineno , child .end_col_offset = child .lineno , child .col_offset + 2
141141 return root .body
142142
143- # %% ../nbs/09_API/02_maker.ipynb 29
143+ # %% ../nbs/09_API/02_maker.ipynb 28
144144def update_import (source , tree , libname , f = relative_import ):
145145 if not tree : return
146146 if sys .version_info < (3 ,8 ): tree = _mark_text_ranges (source )
@@ -160,7 +160,7 @@ def import2relative(cell:NbCell, libname):
160160 src = update_import (cell .source , cell .parsed_ (), libname )
161161 if src : cell .set_source (src )
162162
163- # %% ../nbs/09_API/02_maker.ipynb 31
163+ # %% ../nbs/09_API/02_maker.ipynb 30
164164@patch
165165def _last_future (self :ModuleMaker , cells ):
166166 "Returns the location of a `__future__` in `cells`"
@@ -169,13 +169,13 @@ def _last_future(self:ModuleMaker, cells):
169169 isinstance (t ,ast .ImportFrom ) and t .module == '__future__' for t in tree ))+ 1
170170 except ValueError : return 0
171171
172- # %% ../nbs/09_API/02_maker.ipynb 32
172+ # %% ../nbs/09_API/02_maker.ipynb 31
173173def _import2relative (cells , lib_name = None ):
174174 "Converts `cells` to use `import2relative` based on `lib_name`"
175175 if lib_name is None : lib_name = get_config ().lib_name
176176 for cell in cells : cell .import2relative (lib_name )
177177
178- # %% ../nbs/09_API/02_maker.ipynb 33
178+ # %% ../nbs/09_API/02_maker.ipynb 32
179179@patch
180180def make (self :ModuleMaker , cells , all_cells = None , lib_path = None ):
181181 "Write module containing `cells` with `__all__` generated from `all_cells`"
@@ -198,10 +198,10 @@ def make(self:ModuleMaker, cells, all_cells=None, lib_path=None):
198198 f .write (f"# AUTOGENERATED! DO NOT EDIT! File to edit: { self .dest2nb } ." )
199199 if last_future > 0 : write_cells (cells [:last_future ], self .hdr , f )
200200 if self .parse : f .write (f"\n \n # %% auto 0\n __all__ = { all_str } " )
201- write_cells (cells [last_future :], self .hdr , f , 1 if last_future > 0 else 0 )
201+ write_cells (cells [last_future :], self .hdr , f )
202202 f .write ('\n ' )
203203
204- # %% ../nbs/09_API/02_maker.ipynb 38
204+ # %% ../nbs/09_API/02_maker.ipynb 37
205205@patch
206206def _update_all (self :ModuleMaker , all_cells , alls ):
207207 return pformat (alls + self .make_all (all_cells ), width = 160 )
@@ -212,7 +212,7 @@ def _make_exists(self:ModuleMaker, cells, all_cells=None):
212212 if all_cells and self .parse : update_var ('__all__' , partial (self ._update_all , all_cells ), fn = self .fname )
213213 with self .fname .open ('a' ) as f : write_cells (cells , self .hdr , f )
214214
215- # %% ../nbs/09_API/02_maker.ipynb 44
215+ # %% ../nbs/09_API/02_maker.ipynb 43
216216def _basic_export_nb2 (fname , name , dest = None ):
217217 "A basic exporter to bootstrap nbdev using `ModuleMaker`"
218218 if dest is None : dest = get_config ().path ('lib_path' )
0 commit comments