1414 'only' , 'nested_attr' , 'nested_setdefault' , 'nested_callable' , 'nested_idx' , 'set_nested_idx' , 'val2idx' ,
1515 'uniqueify' , 'loop_first_last' , 'loop_first' , 'loop_last' , 'first_match' , 'last_match' , 'fastuple' , 'bind' ,
1616 'mapt' , 'map_ex' , 'compose' , 'maps' , 'partialler' , 'instantiate' , 'using_attr' , 'copy_func' , 'patch_to' ,
17- 'patch' , 'patch_property' , 'compile_re' , 'ImportEnum' , 'StrEnum' , 'str_enum' , 'Stateful ' , 'NotStr ' ,
18- 'PrettyString' , 'even_mults' , 'num_cpus' , 'add_props' , 'typed' , 'exec_new' , 'exec_import' , 'str2bool' , 'lt ' ,
19- 'gt' , 'le' , 'ge' , 'eq' , 'ne' , 'add' , 'sub' , 'mul' , 'truediv' , 'is_' , 'is_not' , 'mod' ]
17+ 'patch' , 'patch_property' , 'compile_re' , 'ImportEnum' , 'StrEnum' , 'str_enum' , 'ValEnum ' , 'Stateful ' ,
18+ 'NotStr' , ' PrettyString' , 'even_mults' , 'num_cpus' , 'add_props' , 'typed' , 'exec_new' , 'exec_import' ,
19+ 'str2bool' , 'lt' , ' gt' , 'le' , 'ge' , 'eq' , 'ne' , 'add' , 'sub' , 'mul' , 'truediv' , 'is_' , 'is_not' , 'mod' ]
2020
2121# %% ../nbs/01_basics.ipynb 1
2222from .imports import *
@@ -1038,7 +1038,12 @@ def str_enum(name, *vals):
10381038 "Simplified creation of `StrEnum` types"
10391039 return StrEnum (name , {o :o for o in vals })
10401040
1041- # %% ../nbs/01_basics.ipynb 415
1041+ # %% ../nbs/01_basics.ipynb 414
1042+ class ValEnum (str ,ImportEnum ):
1043+ "An `ImportEnum` that stringifies using values"
1044+ def __str__ (self ): return self .value
1045+
1046+ # %% ../nbs/01_basics.ipynb 417
10421047class Stateful :
10431048 "A base class/mixin for objects that should not serialize all their state"
10441049 _stateattrs = ()
@@ -1058,7 +1063,7 @@ def _init_state(self):
10581063 "Override for custom init and deserialization logic"
10591064 self ._state = {}
10601065
1061- # %% ../nbs/01_basics.ipynb 421
1066+ # %% ../nbs/01_basics.ipynb 423
10621067class NotStr (GetAttr ):
10631068 "Behaves like a `str`, but isn't an instance of one"
10641069 _default = 's'
@@ -1075,37 +1080,37 @@ def __bool__(self): return bool(self.s)
10751080 def __contains__ (self , b ): return b in self .s
10761081 def __iter__ (self ): return iter (self .s )
10771082
1078- # %% ../nbs/01_basics.ipynb 423
1083+ # %% ../nbs/01_basics.ipynb 425
10791084class PrettyString (str ):
10801085 "Little hack to get strings to show properly in Jupyter."
10811086 def __repr__ (self ): return self
10821087
1083- # %% ../nbs/01_basics.ipynb 429
1088+ # %% ../nbs/01_basics.ipynb 431
10841089def even_mults (start , stop , n ):
10851090 "Build log-stepped array from `start` to `stop` in `n` steps."
10861091 if n == 1 : return stop
10871092 mult = stop / start
10881093 step = mult ** (1 / (n - 1 ))
10891094 return [start * (step ** i ) for i in range (n )]
10901095
1091- # %% ../nbs/01_basics.ipynb 431
1096+ # %% ../nbs/01_basics.ipynb 433
10921097def num_cpus ():
10931098 "Get number of cpus"
10941099 try : return len (os .sched_getaffinity (0 ))
10951100 except AttributeError : return os .cpu_count ()
10961101
10971102defaults .cpus = num_cpus ()
10981103
1099- # %% ../nbs/01_basics.ipynb 433
1104+ # %% ../nbs/01_basics.ipynb 435
11001105def add_props (f , g = None , n = 2 ):
11011106 "Create properties passing each of `range(n)` to f"
11021107 if g is None : return (property (partial (f ,i )) for i in range (n ))
11031108 return (property (partial (f ,i ), partial (g ,i )) for i in range (n ))
11041109
1105- # %% ../nbs/01_basics.ipynb 436
1110+ # %% ../nbs/01_basics.ipynb 438
11061111def _typeerr (arg , val , typ ): return TypeError (f"{ arg } =={ val } not { typ } " )
11071112
1108- # %% ../nbs/01_basics.ipynb 437
1113+ # %% ../nbs/01_basics.ipynb 439
11091114def typed (f ):
11101115 "Decorator to check param and return types at runtime"
11111116 names = f .__code__ .co_varnames
@@ -1122,21 +1127,21 @@ def _f(*args,**kwargs):
11221127 return res
11231128 return functools .update_wrapper (_f , f )
11241129
1125- # %% ../nbs/01_basics.ipynb 445
1130+ # %% ../nbs/01_basics.ipynb 447
11261131def exec_new (code ):
11271132 "Execute `code` in a new environment and return it"
11281133 pkg = None if __name__ == '__main__' else Path ().cwd ().name
11291134 g = {'__name__' : __name__ , '__package__' : pkg }
11301135 exec (code , g )
11311136 return g
11321137
1133- # %% ../nbs/01_basics.ipynb 447
1138+ # %% ../nbs/01_basics.ipynb 449
11341139def exec_import (mod , sym ):
11351140 "Import `sym` from `mod` in a new environment"
11361141# pref = '' if __name__=='__main__' or mod[0]=='.' else '.'
11371142 return exec_new (f'from { mod } import { sym } ' )
11381143
1139- # %% ../nbs/01_basics.ipynb 448
1144+ # %% ../nbs/01_basics.ipynb 450
11401145def str2bool (s ):
11411146 "Case-insensitive convert string `s` too a bool (`y`,`yes`,`t`,`true`,`on`,`1`->`True`)"
11421147 if not isinstance (s ,str ): return bool (s )
0 commit comments