From ec5751ced7112bf1257ce43fecde07730cde9020 Mon Sep 17 00:00:00 2001 From: Jeremy Howard Date: Fri, 10 May 2024 04:59:47 +1000 Subject: [PATCH] fixes #548 --- fastcore/_modidx.py | 15 ++++++ fastcore/basics.py | 39 ++++++++++---- fastcore/docments.py | 2 +- fastcore/script.py | 4 +- nbs/01_basics.ipynb | 117 +++++++++++++++++++++++++++--------------- nbs/06_docments.ipynb | 2 +- nbs/08_script.ipynb | 4 +- 7 files changed, 126 insertions(+), 57 deletions(-) diff --git a/fastcore/_modidx.py b/fastcore/_modidx.py index 90be6bc3..812f8142 100644 --- a/fastcore/_modidx.py +++ b/fastcore/_modidx.py @@ -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'), @@ -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'), diff --git a/fastcore/basics.py b/fastcore/basics.py index aa3f3622..d6a3a6c8 100644 --- a/fastcore/basics.py +++ b/fastcore/basics.py @@ -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 * @@ -1034,11 +1034,28 @@ 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`True`)" if not isinstance(s,str): return bool(s) diff --git a/fastcore/docments.py b/fastcore/docments.py index 9c47bd10..48036852 100644 --- a/fastcore/docments.py +++ b/fastcore/docments.py @@ -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 diff --git a/fastcore/script.py b/fastcore/script.py index cb545791..a9a0f1ed 100644 --- a/fastcore/script.py +++ b/fastcore/script.py @@ -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 diff --git a/nbs/01_basics.ipynb b/nbs/01_basics.ipynb index 94cea0fc..753ebee6 100644 --- a/nbs/01_basics.ipynb +++ b/nbs/01_basics.ipynb @@ -2745,24 +2745,24 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L485){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L492){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### GetAttr\n", "\n", "> GetAttr ()\n", "\n", - "Inherit from this to have all attr accesses in `self._xtra` passed down to `self.default`" + "*Inherit from this to have all attr accesses in `self._xtra` passed down to `self.default`*" ], "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L485){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L492){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### GetAttr\n", "\n", "> GetAttr ()\n", "\n", - "Inherit from this to have all attr accesses in `self._xtra` passed down to `self.default`" + "*Inherit from this to have all attr accesses in `self._xtra` passed down to `self.default`*" ] }, "execution_count": null, @@ -4239,24 +4239,24 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L767){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L784){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### fastuple\n", "\n", "> fastuple (x=None, *rest)\n", "\n", - "A `tuple` with elementwise ops and more friendly __init__ behavior" + "*A `tuple` with elementwise ops and more friendly __init__ behavior*" ], "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L767){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L784){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### fastuple\n", "\n", "> fastuple (x=None, *rest)\n", "\n", - "A `tuple` with elementwise ops and more friendly __init__ behavior" + "*A `tuple` with elementwise ops and more friendly __init__ behavior*" ] }, "execution_count": null, @@ -4324,24 +4324,24 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L786){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L803){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "##### fastuple.add\n", "\n", "> fastuple.add (*args)\n", "\n", - "`+` is already defined in `tuple` for concat, so use `add` instead" + "*`+` is already defined in `tuple` for concat, so use `add` instead*" ], "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L786){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L803){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "##### fastuple.add\n", "\n", "> fastuple.add (*args)\n", "\n", - "`+` is already defined in `tuple` for concat, so use `add` instead" + "*`+` is already defined in `tuple` for concat, so use `add` instead*" ] }, "execution_count": null, @@ -4374,24 +4374,24 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L782){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L799){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "##### fastuple.mul\n", "\n", "> fastuple.mul (*args)\n", "\n", - "`*` is already defined in `tuple` for replicating, so use `mul` instead" + "*`*` is already defined in `tuple` for replicating, so use `mul` instead*" ], "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L782){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L799){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "##### fastuple.mul\n", "\n", "> fastuple.mul (*args)\n", "\n", - "`*` is already defined in `tuple` for replicating, so use `mul` instead" + "*`*` is already defined in `tuple` for replicating, so use `mul` instead*" ] }, "execution_count": null, @@ -4535,24 +4535,24 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L813){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L830){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "### bind\n", "\n", "> bind (func, *pargs, **pkwargs)\n", "\n", - "Same as `partial`, except you can use `arg0` `arg1` etc param placeholders" + "*Same as `partial`, except you can use `arg0` `arg1` etc param placeholders*" ], "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L813){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L830){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "### bind\n", "\n", "> bind (func, *pargs, **pkwargs)\n", "\n", - "Same as `partial`, except you can use `arg0` `arg1` etc param placeholders" + "*Same as `partial`, except you can use `arg0` `arg1` etc param placeholders*" ] }, "execution_count": null, @@ -5508,26 +5508,26 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L977){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L999){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### ImportEnum\n", "\n", - "> ImportEnum (value, names=None, module=None, qualname=None, type=None,\n", - "> start=1)\n", + "> ImportEnum (new_class_name, names, module=None, qualname=None, type=None,\n", + "> start=1, boundary=None)\n", "\n", - "An `Enum` that can have its values imported" + "*An `Enum` that can have its values imported*" ], "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L977){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L999){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### ImportEnum\n", "\n", - "> ImportEnum (value, names=None, module=None, qualname=None, type=None,\n", - "> start=1)\n", + "> ImportEnum (new_class_name, names, module=None, qualname=None, type=None,\n", + "> start=1, boundary=None)\n", "\n", - "An `Enum` that can have its values imported" + "*An `Enum` that can have its values imported*" ] }, "execution_count": null, @@ -5573,26 +5573,26 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L985){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1007){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### StrEnum\n", "\n", - "> StrEnum (value, names=None, module=None, qualname=None, type=None,\n", - "> start=1)\n", + "> StrEnum (new_class_name, names, module=None, qualname=None, type=None,\n", + "> start=1, boundary=None)\n", "\n", - "An `ImportEnum` that behaves like a `str`" + "*An `ImportEnum` that behaves like a `str`*" ], "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L985){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1007){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### StrEnum\n", "\n", - "> StrEnum (value, names=None, module=None, qualname=None, type=None,\n", - "> start=1)\n", + "> StrEnum (new_class_name, names, module=None, qualname=None, type=None,\n", + "> start=1, boundary=None)\n", "\n", - "An `ImportEnum` that behaves like a `str`" + "*An `ImportEnum` that behaves like a `str`*" ] }, "execution_count": null, @@ -5674,24 +5674,24 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L995){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1017){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### Stateful\n", "\n", "> Stateful (*args, **kwargs)\n", "\n", - "A base class/mixin for objects that should not serialize all their state" + "*A base class/mixin for objects that should not serialize all their state*" ], "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L995){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1017){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### Stateful\n", "\n", "> Stateful (*args, **kwargs)\n", "\n", - "A base class/mixin for objects that should not serialize all their state" + "*A base class/mixin for objects that should not serialize all their state*" ] }, "execution_count": null, @@ -5777,6 +5777,43 @@ "print(reader.readline())" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "class NotStr(GetAttr):\n", + " \"Behaves like a `str`, but isn't an instance of one\"\n", + " _default = 's'\n", + " def __init__(self, s): self.s = s\n", + " def __repr__(self): return repr(self.s)\n", + " def __str__(self): return self.s\n", + " def __add__(self, b): return NotStr(self.s+str(b))\n", + " def __mul__(self, b): return NotStr(self.s*b)\n", + " def __len__(self): return len(self.s)\n", + " def __eq__(self, b): return self.s==b.s if isinstance(b, NotStr) else b\n", + " def __lt__(self, b): return self.s|'|\\ )\", '', x) # spl characters\n", + " x = re.sub(r\"(enum |class|function|__main__\\.|\\ at.*)\", '', x)\n", + " x = re.sub(r\"(<|>|'|\\ )\", '', x) # spl characters\n", " return x" ] },