Skip to content

Commit

Permalink
fixes #548
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed May 9, 2024
1 parent b7434c4 commit ec5751c
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 57 deletions.
15 changes: 15 additions & 0 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
'fastcore.basics.NS.__getitem__': ('basics.html#ns.__getitem__', 'fastcore/basics.py'),
'fastcore.basics.NS.__iter__': ('basics.html#ns.__iter__', 'fastcore/basics.py'),
'fastcore.basics.NS.__setitem__': ('basics.html#ns.__setitem__', 'fastcore/basics.py'),
'fastcore.basics.NotStr': ('basics.html#notstr', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__add__': ('basics.html#notstr.__add__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__bool__': ('basics.html#notstr.__bool__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__contains__': ('basics.html#notstr.__contains__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__eq__': ('basics.html#notstr.__eq__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__hash__': ('basics.html#notstr.__hash__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__init__': ('basics.html#notstr.__init__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__iter__': ('basics.html#notstr.__iter__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__len__': ('basics.html#notstr.__len__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__lt__': ('basics.html#notstr.__lt__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__mul__': ('basics.html#notstr.__mul__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__repr__': ('basics.html#notstr.__repr__', 'fastcore/basics.py'),
'fastcore.basics.NotStr.__str__': ('basics.html#notstr.__str__', 'fastcore/basics.py'),
'fastcore.basics.NullType': ('basics.html#nulltype', 'fastcore/basics.py'),
'fastcore.basics.NullType.__bool__': ('basics.html#nulltype.__bool__', 'fastcore/basics.py'),
'fastcore.basics.NullType.__call__': ('basics.html#nulltype.__call__', 'fastcore/basics.py'),
Expand Down Expand Up @@ -520,6 +533,8 @@
'fastcore.xdg.xdg_data_home': ('xdg.html#xdg_data_home', 'fastcore/xdg.py'),
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
'fastcore.xml': { 'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
'fastcore.xml.xt': ('xml.html#xt', 'fastcore/xml.py')},
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),
'fastcore.xtras.ContextManagers.__enter__': ('xtras.html#contextmanagers.__enter__', 'fastcore/xtras.py'),
'fastcore.xtras.ContextManagers.__exit__': ('xtras.html#contextmanagers.__exit__', 'fastcore/xtras.py'),
Expand Down
39 changes: 28 additions & 11 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
'nested_attr', 'nested_setdefault', 'nested_callable', 'nested_idx', 'set_nested_idx', 'val2idx',
'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'first_match', 'last_match', 'fastuple', 'bind',
'mapt', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'copy_func', 'patch_to',
'patch', 'patch_property', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful', 'PrettyString',
'even_mults', 'num_cpus', 'add_props', 'typed', 'exec_new', 'exec_import', 'str2bool', 'lt', 'gt', 'le',
'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not']
'patch', 'patch_property', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful', 'NotStr',
'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'typed', 'exec_new', 'exec_import', 'str2bool', 'lt',
'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not']

# %% ../nbs/01_basics.ipynb 1
from .imports import *
Expand Down Expand Up @@ -1034,36 +1034,53 @@ def _init_state(self):
self._state = {}

# %% ../nbs/01_basics.ipynb 418
class NotStr(GetAttr):
"Behaves like a `str`, but isn't an instance of one"
_default = 's'
def __init__(self, s): self.s = s
def __repr__(self): return repr(self.s)
def __str__(self): return self.s
def __add__(self, b): return NotStr(self.s+str(b))
def __mul__(self, b): return NotStr(self.s*b)
def __len__(self): return len(self.s)
def __eq__(self, b): return self.s==b.s if isinstance(b, NotStr) else b
def __lt__(self, b): return self.s<b
def __hash__(self): return hash(self.s)
def __bool__(self): return bool(self.s)
def __contains__(self, b): return b in self.s
def __iter__(self): return iter(self.s)

# %% ../nbs/01_basics.ipynb 420
class PrettyString(str):
"Little hack to get strings to show properly in Jupyter."
def __repr__(self): return self

# %% ../nbs/01_basics.ipynb 424
# %% ../nbs/01_basics.ipynb 426
def even_mults(start, stop, n):
"Build log-stepped array from `start` to `stop` in `n` steps."
if n==1: return stop
mult = stop/start
step = mult**(1/(n-1))
return [start*(step**i) for i in range(n)]

# %% ../nbs/01_basics.ipynb 426
# %% ../nbs/01_basics.ipynb 428
def num_cpus():
"Get number of cpus"
try: return len(os.sched_getaffinity(0))
except AttributeError: return os.cpu_count()

defaults.cpus = num_cpus()

# %% ../nbs/01_basics.ipynb 428
# %% ../nbs/01_basics.ipynb 430
def add_props(f, g=None, n=2):
"Create properties passing each of `range(n)` to f"
if g is None: return (property(partial(f,i)) for i in range(n))
return (property(partial(f,i), partial(g,i)) for i in range(n))

# %% ../nbs/01_basics.ipynb 431
# %% ../nbs/01_basics.ipynb 433
def _typeerr(arg, val, typ): return TypeError(f"{arg}=={val} not {typ}")

# %% ../nbs/01_basics.ipynb 432
# %% ../nbs/01_basics.ipynb 434
def typed(f):
"Decorator to check param and return types at runtime"
names = f.__code__.co_varnames
Expand All @@ -1080,21 +1097,21 @@ def _f(*args,**kwargs):
return res
return functools.update_wrapper(_f, f)

# %% ../nbs/01_basics.ipynb 440
# %% ../nbs/01_basics.ipynb 442
def exec_new(code):
"Execute `code` in a new environment and return it"
pkg = None if __name__=='__main__' else Path().cwd().name
g = {'__name__': __name__, '__package__': pkg}
exec(code, g)
return g

# %% ../nbs/01_basics.ipynb 442
# %% ../nbs/01_basics.ipynb 444
def exec_import(mod, sym):
"Import `sym` from `mod` in a new environment"
# pref = '' if __name__=='__main__' or mod[0]=='.' else '.'
return exec_new(f'from {mod} import {sym}')

# %% ../nbs/01_basics.ipynb 443
# %% ../nbs/01_basics.ipynb 445
def str2bool(s):
"Case-insensitive convert string `s` too a bool (`y`,`yes`,`t`,`true`,`on`,`1`->`True`)"
if not isinstance(s,str): return bool(s)
Expand Down
2 changes: 1 addition & 1 deletion fastcore/docments.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _tokens(s):
s = get_source(s)
return tokenize(BytesIO(s.encode('utf-8')).readline)

_clean_re = re.compile('^\s*#(.*)\s*$')
_clean_re = re.compile(r'^\s*#(.*)\s*$')
def _clean_comment(s):
res = _clean_re.findall(s)
return res[0] if res else None
Expand Down
4 changes: 2 additions & 2 deletions fastcore/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def bool_arg(v):
# %% ../nbs/08_script.ipynb 18
def clean_type_str(x:str):
x = str(x)
x = re.sub("(enum |class|function|__main__\.|\ at.*)", '', x)
x = re.sub("(<|>|'|\ )", '', x) # spl characters
x = re.sub(r"(enum |class|function|__main__\.|\ at.*)", '', x)
x = re.sub(r"(<|>|'|\ )", '', x) # spl characters
return x

# %% ../nbs/08_script.ipynb 21
Expand Down
Loading

0 comments on commit ec5751c

Please sign in to comment.