From 7b1eaeae0e20c714e9d6442332e5b2cc2d5d77c0 Mon Sep 17 00:00:00 2001 From: Jeremy Howard Date: Wed, 10 Jul 2024 07:43:45 +1000 Subject: [PATCH] fixes #578 --- fastcore/__init__.py | 2 +- fastcore/xtras.py | 20 +++--- nbs/03_xtras.ipynb | 152 ++++++++++++++++++++++++++++++++----------- settings.ini | 2 +- 4 files changed, 127 insertions(+), 49 deletions(-) diff --git a/fastcore/__init__.py b/fastcore/__init__.py index 32091f90..fa7995c5 100644 --- a/fastcore/__init__.py +++ b/fastcore/__init__.py @@ -1 +1 @@ -__version__ = "1.5.49" +__version__ = "1.5.50" diff --git a/fastcore/xtras.py b/fastcore/xtras.py index bfcc29ba..cf35bb99 100644 --- a/fastcore/xtras.py +++ b/fastcore/xtras.py @@ -653,23 +653,27 @@ def nullable_dc(cls): return dataclass(cls) # %% ../nbs/03_xtras.ipynb 173 -def make_nullable(cls): +def make_nullable(clas): from dataclasses import dataclass, fields, MISSING - if hasattr(cls, '_nullable'): return - cls._nullable = True + if hasattr(clas, '_nullable'): return + clas._nullable = True - @patch - def __init__(self:cls, *args, **kwargs): - flds = fields(cls) + original_init = clas.__init__ + def __init__(self, *args, **kwargs): + flds = fields(clas) dargs = {k.name:v for k,v in zip(flds, args)} for f in flds: nm = f.name if nm not in dargs and nm not in kwargs and f.default is None and f.default_factory is MISSING: kwargs[nm] = None - self._orig___init__(*args, **kwargs) + original_init(self, *args, **kwargs) - for f in fields(cls): + clas.__init__ = __init__ + + for f in fields(clas): if f.default is MISSING and f.default_factory is MISSING: f.default = None + + return clas # %% ../nbs/03_xtras.ipynb 177 def mk_dataclass(cls): diff --git a/nbs/03_xtras.ipynb b/nbs/03_xtras.ipynb index 4bcfbd65..989e4021 100644 --- a/nbs/03_xtras.ipynb +++ b/nbs/03_xtras.ipynb @@ -1188,7 +1188,7 @@ { "data": { "text/plain": [ - "Path('/home/lgvaz/git/fastcore/fastcore')" + "Path('/Users/jhoward/Documents/GitHub/fastcore/fastcore')" ] }, "execution_count": null, @@ -1255,7 +1255,7 @@ { "data": { "text/plain": [ - "Path('05_transform.ipynb')" + "Path('000_tour.ipynb')" ] }, "execution_count": null, @@ -1289,7 +1289,7 @@ { "data": { "text/plain": [ - "(Path('../fastcore/all.py'), Path('05_transform.ipynb'))" + "(Path('../fastcore/shutil.py'), Path('000_tour.ipynb'))" ] }, "execution_count": null, @@ -1434,14 +1434,24 @@ "text/markdown": [ "---\n", "\n", - "### ReindexCollection\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L380){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", - "> ReindexCollection (coll, idxs=None, cache=None, tfm=)\n", + "#### ReindexCollection\n", "\n", - "Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`" + "> ReindexCollection (coll, idxs=None, cache=None, tfm=)\n", + "\n", + "*Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`*" ], "text/plain": [ - "" + "---\n", + "\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L380){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "#### ReindexCollection\n", + "\n", + "> ReindexCollection (coll, idxs=None, cache=None, tfm=)\n", + "\n", + "*Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`*" ] }, "execution_count": null, @@ -1507,14 +1517,24 @@ "text/markdown": [ "---\n", "\n", - "#### ReindexCollection.reindex\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L391){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "###### ReindexCollection.reindex\n", "\n", "> ReindexCollection.reindex (idxs)\n", "\n", - "Replace `self.idxs` with idxs" + "*Replace `self.idxs` with idxs*" ], "text/plain": [ - "" + "---\n", + "\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L391){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "###### ReindexCollection.reindex\n", + "\n", + "> ReindexCollection.reindex (idxs)\n", + "\n", + "*Replace `self.idxs` with idxs*" ] }, "execution_count": null, @@ -1597,14 +1617,24 @@ "text/markdown": [ "---\n", "\n", - "#### ReindexCollection.cache_clear\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L395){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "##### ReindexCollection.cache_clear\n", "\n", "> ReindexCollection.cache_clear ()\n", "\n", - "Clear LRU cache" + "*Clear LRU cache*" ], "text/plain": [ - "" + "---\n", + "\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L395){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "##### ReindexCollection.cache_clear\n", + "\n", + "> ReindexCollection.cache_clear ()\n", + "\n", + "*Clear LRU cache*" ] }, "execution_count": null, @@ -1652,14 +1682,24 @@ "text/markdown": [ "---\n", "\n", - "#### ReindexCollection.shuffle\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L392){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "##### ReindexCollection.shuffle\n", "\n", "> ReindexCollection.shuffle ()\n", "\n", - "Randomly shuffle indices" + "*Randomly shuffle indices*" ], "text/plain": [ - "" + "---\n", + "\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L392){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "##### ReindexCollection.shuffle\n", + "\n", + "> ReindexCollection.shuffle ()\n", + "\n", + "*Randomly shuffle indices*" ] }, "execution_count": null, @@ -1686,7 +1726,7 @@ { "data": { "text/plain": [ - "['d', 'f', 'g', 'a', 'h', 'e', 'b', 'c']" + "['h', 'g', 'a', 'd', 'b', 'e', 'f', 'c']" ] }, "execution_count": null, @@ -2102,14 +2142,24 @@ "text/markdown": [ "---\n", "\n", - "### EventTimer\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L502){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "#### EventTimer\n", "\n", "> EventTimer (store=5, span=60)\n", "\n", - "An event timer with history of `store` items of time `span`" + "*An event timer with history of `store` items of time `span`*" ], "text/plain": [ - "" + "---\n", + "\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L502){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "#### EventTimer\n", + "\n", + "> EventTimer (store=5, span=60)\n", + "\n", + "*An event timer with history of `store` items of time `span`*" ] }, "execution_count": null, @@ -2137,8 +2187,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Num Events: 3, Freq/sec: 242.1\n", - "Most recent: ▇▁▂▃▁ 358.8 267.5 296.5 315.5 266.4\n" + "Num Events: 3, Freq/sec: 256.2\n", + "Most recent: ▁▃▇▅▂ 223.9 260.6 307.1 279.5 252.0\n" ] } ], @@ -2217,14 +2267,24 @@ "text/markdown": [ "---\n", "\n", - "### PartialFormatter\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L534){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "#### PartialFormatter\n", "\n", "> PartialFormatter ()\n", "\n", - "A `string.Formatter` that doesn't error on missing fields, and tracks missing fields and unused args" + "*A `string.Formatter` that doesn't error on missing fields, and tracks missing fields and unused args*" ], "text/plain": [ - "" + "---\n", + "\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L534){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "#### PartialFormatter\n", + "\n", + "> PartialFormatter ()\n", + "\n", + "*A `string.Formatter` that doesn't error on missing fields, and tracks missing fields and unused args*" ] }, "execution_count": null, @@ -2290,7 +2350,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "2000-01-01 12:00:00 UTC is 2000-01-01 17:30:00+05:30 local time\n" + "2000-01-01 12:00:00 UTC is 2000-01-01 22:00:00+10:00 local time\n" ] } ], @@ -2320,7 +2380,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "2000-01-01 12:00:00 local is 2000-01-01 06:30:00+00:00 UTC time\n" + "2000-01-01 12:00:00 local is 2000-01-01 02:00:00+00:00 UTC time\n" ] } ], @@ -2423,14 +2483,24 @@ "text/markdown": [ "---\n", "\n", - "### ContextManagers\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L591){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "#### ContextManagers\n", "\n", "> ContextManagers (mgrs)\n", "\n", - "Wrapper for `contextlib.ExitStack` which enters a collection of context managers" + "*Wrapper for `contextlib.ExitStack` which enters a collection of context managers*" ], "text/plain": [ - "" + "---\n", + "\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/xtras.py#L591){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "\n", + "#### ContextManagers\n", + "\n", + "> ContextManagers (mgrs)\n", + "\n", + "*Wrapper for `contextlib.ExitStack` which enters a collection of context managers*" ] }, "execution_count": null, @@ -2643,23 +2713,27 @@ "outputs": [], "source": [ "#| export\n", - "def make_nullable(cls):\n", + "def make_nullable(clas):\n", " from dataclasses import dataclass, fields, MISSING\n", - " if hasattr(cls, '_nullable'): return\n", - " cls._nullable = True\n", + " if hasattr(clas, '_nullable'): return\n", + " clas._nullable = True\n", "\n", - " @patch\n", - " def __init__(self:cls, *args, **kwargs):\n", - " flds = fields(cls)\n", + " original_init = clas.__init__\n", + " def __init__(self, *args, **kwargs):\n", + " flds = fields(clas)\n", " dargs = {k.name:v for k,v in zip(flds, args)}\n", " for f in flds:\n", " nm = f.name\n", " if nm not in dargs and nm not in kwargs and f.default is None and f.default_factory is MISSING:\n", " kwargs[nm] = None\n", - " self._orig___init__(*args, **kwargs)\n", + " original_init(self, *args, **kwargs)\n", + " \n", + " clas.__init__ = __init__\n", + "\n", + " for f in fields(clas):\n", + " if f.default is MISSING and f.default_factory is MISSING: f.default = None\n", " \n", - " for f in fields(cls):\n", - " if f.default is MISSING and f.default_factory is MISSING: f.default = None" + " return clas" ] }, { diff --git a/settings.ini b/settings.ini index 44cbd07d..49337b0e 100644 --- a/settings.ini +++ b/settings.ini @@ -8,7 +8,7 @@ author = Jeremy Howard and Sylvain Gugger author_email = infos@fast.ai copyright = fast.ai branch = master -version = 1.5.49 +version = 1.5.50 min_python = 3.7 audience = Developers language = English