44__all__ = ['working_directory' , 'add_docs' , 'docs' , 'coll_repr' , 'is_bool' , 'mask2idxs' , 'cycle' , 'zip_cycle' , 'is_indexer' ,
55 'CollBase' , 'L' , 'save_config_file' , 'read_config_file' , 'Config' ]
66
7- # %% ../nbs/02_foundation.ipynb 1
7+ # %% ../nbs/02_foundation.ipynb
88from .imports import *
99from .basics import *
1010from functools import lru_cache
1313from configparser import ConfigParser
1414import random ,pickle ,inspect
1515
16- # %% ../nbs/02_foundation.ipynb 5
16+ # %% ../nbs/02_foundation.ipynb
1717@contextmanager
1818def working_directory (path ):
1919 "Change working directory to `path` and return to previous on exit."
@@ -22,7 +22,7 @@ def working_directory(path):
2222 try : yield
2323 finally : os .chdir (prev_cwd )
2424
25- # %% ../nbs/02_foundation.ipynb 6
25+ # %% ../nbs/02_foundation.ipynb
2626def add_docs (cls , cls_doc = None , ** docs ):
2727 "Copy values from `docs` to `cls` docstrings, and confirm all public methods are documented"
2828 if cls_doc is not None : cls .__doc__ = cls_doc
@@ -36,24 +36,24 @@ def add_docs(cls, cls_doc=None, **docs):
3636 assert not nodoc , f"Missing docs: { nodoc } "
3737 assert cls .__doc__ is not None , f"Missing class docs: { cls } "
3838
39- # %% ../nbs/02_foundation.ipynb 16
39+ # %% ../nbs/02_foundation.ipynb
4040def docs (cls ):
4141 "Decorator version of `add_docs`, using `_docs` dict"
4242 add_docs (cls , ** cls ._docs )
4343 return cls
4444
45- # %% ../nbs/02_foundation.ipynb 23
45+ # %% ../nbs/02_foundation.ipynb
4646def coll_repr (c , max_n = 10 ):
4747 "String repr of up to `max_n` items of (possibly lazy) collection `c`"
4848 return f'(#{ len (c )} ) [' + ',' .join (itertools .islice (map (repr ,c ), max_n )) + (
4949 '...' if len (c )> max_n else '' ) + ']'
5050
51- # %% ../nbs/02_foundation.ipynb 28
51+ # %% ../nbs/02_foundation.ipynb
5252def is_bool (x ):
5353 "Check whether `x` is a bool or None"
5454 return isinstance (x ,(bool ,NoneType )) or risinstance ('bool_' , x )
5555
56- # %% ../nbs/02_foundation.ipynb 29
56+ # %% ../nbs/02_foundation.ipynb
5757def mask2idxs (mask ):
5858 "Convert bool mask or index list to index `L`"
5959 if isinstance (mask ,slice ): return mask
@@ -64,23 +64,23 @@ def mask2idxs(mask):
6464 if is_bool (it ): return [i for i ,m in enumerate (mask ) if m ]
6565 return [int (i ) for i in mask ]
6666
67- # %% ../nbs/02_foundation.ipynb 31
67+ # %% ../nbs/02_foundation.ipynb
6868def cycle (o ):
6969 "Like `itertools.cycle` except creates list of `None`s if `o` is empty"
7070 o = listify (o )
7171 return itertools .cycle (o ) if o is not None and len (o ) > 0 else itertools .cycle ([None ])
7272
73- # %% ../nbs/02_foundation.ipynb 33
73+ # %% ../nbs/02_foundation.ipynb
7474def zip_cycle (x , * args ):
7575 "Like `itertools.zip_longest` but `cycle`s through elements of all but first argument"
7676 return zip (x , * map (cycle ,args ))
7777
78- # %% ../nbs/02_foundation.ipynb 35
78+ # %% ../nbs/02_foundation.ipynb
7979def is_indexer (idx ):
8080 "Test whether `idx` will index a single item in a list"
8181 return isinstance (idx ,int ) or not getattr (idx ,'ndim' ,1 )
8282
83- # %% ../nbs/02_foundation.ipynb 41
83+ # %% ../nbs/02_foundation.ipynb
8484class CollBase :
8585 "Base class for composing a list of `items`"
8686 def __init__ (self , items ): self .items = items
@@ -91,13 +91,13 @@ def __delitem__(self, i): del(self.items[i])
9191 def __repr__ (self ): return self .items .__repr__ ()
9292 def __iter__ (self ): return self .items .__iter__ ()
9393
94- # %% ../nbs/02_foundation.ipynb 45
94+ # %% ../nbs/02_foundation.ipynb
9595class _L_Meta (type ):
9696 def __call__ (cls , x = None , * args , ** kwargs ):
9797 if not args and not kwargs and x is not None and isinstance (x ,cls ): return x
9898 return super ().__call__ (x , * args , ** kwargs )
9999
100- # %% ../nbs/02_foundation.ipynb 46
100+ # %% ../nbs/02_foundation.ipynb
101101class L (GetAttr , CollBase , metaclass = _L_Meta ):
102102 "Behaves like a list of `items` but can also index with list of indices or masks"
103103 _default = 'items'
@@ -194,7 +194,7 @@ def sum(self): return self.reduce(operator.add, 0)
194194 def product (self ): return self .reduce (operator .mul , 1 )
195195 def setattrs (self , attr , val ): [setattr (o ,attr ,val ) for o in self ]
196196
197- # %% ../nbs/02_foundation.ipynb 47
197+ # %% ../nbs/02_foundation.ipynb
198198add_docs (L ,
199199 __getitem__ = "Retrieve `idx` (can be list of indices, or mask, or int) items" ,
200200 range = "Class Method: Same as `range`, but returns `L`. Can pass collection for `a`, to use `len(a)`" ,
@@ -227,29 +227,29 @@ def setattrs(self, attr, val): [setattr(o,attr,val) for o in self]
227227 setattrs = "Call `setattr` on all items"
228228 )
229229
230- # %% ../nbs/02_foundation.ipynb 48
230+ # %% ../nbs/02_foundation.ipynb
231231# Here we are fixing the signature of L. What happens is that the __call__ method on the MetaClass of L shadows the __init__
232232# giving the wrong signature (https://stackoverflow.com/questions/49740290/call-from-metaclass-shadows-signature-of-init).
233233def _f (items = None , * rest , use_list = False , match = None ): ...
234234L .__signature__ = inspect .signature (_f )
235235
236- # %% ../nbs/02_foundation.ipynb 49
236+ # %% ../nbs/02_foundation.ipynb
237237Sequence .register (L );
238238
239- # %% ../nbs/02_foundation.ipynb 129
239+ # %% ../nbs/02_foundation.ipynb
240240def save_config_file (file , d , ** kwargs ):
241241 "Write settings dict to a new config file, or overwrite the existing one."
242242 config = ConfigParser (** kwargs )
243243 config ['DEFAULT' ] = d
244244 config .write (open (file , 'w' ))
245245
246- # %% ../nbs/02_foundation.ipynb 130
246+ # %% ../nbs/02_foundation.ipynb
247247def read_config_file (file , ** kwargs ):
248248 config = ConfigParser (** kwargs )
249249 config .read (file , encoding = 'utf8' )
250250 return config ['DEFAULT' ]
251251
252- # %% ../nbs/02_foundation.ipynb 133
252+ # %% ../nbs/02_foundation.ipynb
253253class Config :
254254 "Reading and writing `ConfigParser` ini files"
255255 def __init__ (self , cfg_path , cfg_name , create = None , save = True , extra_files = None , types = None ):
0 commit comments