Skip to content

Commit ec5751c

Browse files
committed
fixes #548
1 parent b7434c4 commit ec5751c

File tree

7 files changed

+126
-57
lines changed

7 files changed

+126
-57
lines changed

fastcore/_modidx.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@
3131
'fastcore.basics.NS.__getitem__': ('basics.html#ns.__getitem__', 'fastcore/basics.py'),
3232
'fastcore.basics.NS.__iter__': ('basics.html#ns.__iter__', 'fastcore/basics.py'),
3333
'fastcore.basics.NS.__setitem__': ('basics.html#ns.__setitem__', 'fastcore/basics.py'),
34+
'fastcore.basics.NotStr': ('basics.html#notstr', 'fastcore/basics.py'),
35+
'fastcore.basics.NotStr.__add__': ('basics.html#notstr.__add__', 'fastcore/basics.py'),
36+
'fastcore.basics.NotStr.__bool__': ('basics.html#notstr.__bool__', 'fastcore/basics.py'),
37+
'fastcore.basics.NotStr.__contains__': ('basics.html#notstr.__contains__', 'fastcore/basics.py'),
38+
'fastcore.basics.NotStr.__eq__': ('basics.html#notstr.__eq__', 'fastcore/basics.py'),
39+
'fastcore.basics.NotStr.__hash__': ('basics.html#notstr.__hash__', 'fastcore/basics.py'),
40+
'fastcore.basics.NotStr.__init__': ('basics.html#notstr.__init__', 'fastcore/basics.py'),
41+
'fastcore.basics.NotStr.__iter__': ('basics.html#notstr.__iter__', 'fastcore/basics.py'),
42+
'fastcore.basics.NotStr.__len__': ('basics.html#notstr.__len__', 'fastcore/basics.py'),
43+
'fastcore.basics.NotStr.__lt__': ('basics.html#notstr.__lt__', 'fastcore/basics.py'),
44+
'fastcore.basics.NotStr.__mul__': ('basics.html#notstr.__mul__', 'fastcore/basics.py'),
45+
'fastcore.basics.NotStr.__repr__': ('basics.html#notstr.__repr__', 'fastcore/basics.py'),
46+
'fastcore.basics.NotStr.__str__': ('basics.html#notstr.__str__', 'fastcore/basics.py'),
3447
'fastcore.basics.NullType': ('basics.html#nulltype', 'fastcore/basics.py'),
3548
'fastcore.basics.NullType.__bool__': ('basics.html#nulltype.__bool__', 'fastcore/basics.py'),
3649
'fastcore.basics.NullType.__call__': ('basics.html#nulltype.__call__', 'fastcore/basics.py'),
@@ -520,6 +533,8 @@
520533
'fastcore.xdg.xdg_data_home': ('xdg.html#xdg_data_home', 'fastcore/xdg.py'),
521534
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
522535
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
536+
'fastcore.xml': { 'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
537+
'fastcore.xml.xt': ('xml.html#xt', 'fastcore/xml.py')},
523538
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),
524539
'fastcore.xtras.ContextManagers.__enter__': ('xtras.html#contextmanagers.__enter__', 'fastcore/xtras.py'),
525540
'fastcore.xtras.ContextManagers.__exit__': ('xtras.html#contextmanagers.__exit__', 'fastcore/xtras.py'),

fastcore/basics.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
'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', 'PrettyString',
18-
'even_mults', 'num_cpus', 'add_props', 'typed', 'exec_new', 'exec_import', 'str2bool', 'lt', 'gt', 'le',
19-
'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not']
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']
2020

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

10361036
# %% ../nbs/01_basics.ipynb 418
1037+
class NotStr(GetAttr):
1038+
"Behaves like a `str`, but isn't an instance of one"
1039+
_default = 's'
1040+
def __init__(self, s): self.s = s
1041+
def __repr__(self): return repr(self.s)
1042+
def __str__(self): return self.s
1043+
def __add__(self, b): return NotStr(self.s+str(b))
1044+
def __mul__(self, b): return NotStr(self.s*b)
1045+
def __len__(self): return len(self.s)
1046+
def __eq__(self, b): return self.s==b.s if isinstance(b, NotStr) else b
1047+
def __lt__(self, b): return self.s<b
1048+
def __hash__(self): return hash(self.s)
1049+
def __bool__(self): return bool(self.s)
1050+
def __contains__(self, b): return b in self.s
1051+
def __iter__(self): return iter(self.s)
1052+
1053+
# %% ../nbs/01_basics.ipynb 420
10371054
class PrettyString(str):
10381055
"Little hack to get strings to show properly in Jupyter."
10391056
def __repr__(self): return self
10401057

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

1049-
# %% ../nbs/01_basics.ipynb 426
1066+
# %% ../nbs/01_basics.ipynb 428
10501067
def num_cpus():
10511068
"Get number of cpus"
10521069
try: return len(os.sched_getaffinity(0))
10531070
except AttributeError: return os.cpu_count()
10541071

10551072
defaults.cpus = num_cpus()
10561073

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

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

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

1083-
# %% ../nbs/01_basics.ipynb 440
1100+
# %% ../nbs/01_basics.ipynb 442
10841101
def exec_new(code):
10851102
"Execute `code` in a new environment and return it"
10861103
pkg = None if __name__=='__main__' else Path().cwd().name
10871104
g = {'__name__': __name__, '__package__': pkg}
10881105
exec(code, g)
10891106
return g
10901107

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

1097-
# %% ../nbs/01_basics.ipynb 443
1114+
# %% ../nbs/01_basics.ipynb 445
10981115
def str2bool(s):
10991116
"Case-insensitive convert string `s` too a bool (`y`,`yes`,`t`,`true`,`on`,`1`->`True`)"
11001117
if not isinstance(s,str): return bool(s)

fastcore/docments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _tokens(s):
5959
s = get_source(s)
6060
return tokenize(BytesIO(s.encode('utf-8')).readline)
6161

62-
_clean_re = re.compile('^\s*#(.*)\s*$')
62+
_clean_re = re.compile(r'^\s*#(.*)\s*$')
6363
def _clean_comment(s):
6464
res = _clean_re.findall(s)
6565
return res[0] if res else None

fastcore/script.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def bool_arg(v):
2929
# %% ../nbs/08_script.ipynb 18
3030
def clean_type_str(x:str):
3131
x = str(x)
32-
x = re.sub("(enum |class|function|__main__\.|\ at.*)", '', x)
33-
x = re.sub("(<|>|'|\ )", '', x) # spl characters
32+
x = re.sub(r"(enum |class|function|__main__\.|\ at.*)", '', x)
33+
x = re.sub(r"(<|>|'|\ )", '', x) # spl characters
3434
return x
3535

3636
# %% ../nbs/08_script.ipynb 21

0 commit comments

Comments
 (0)