Skip to content

Commit

Permalink
fixes #578
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Jul 9, 2024
1 parent c2ec565 commit 7b1eaea
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 49 deletions.
2 changes: 1 addition & 1 deletion fastcore/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.5.49"
__version__ = "1.5.50"
20 changes: 12 additions & 8 deletions fastcore/xtras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
152 changes: 113 additions & 39 deletions nbs/03_xtras.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@
{
"data": {
"text/plain": [
"Path('/home/lgvaz/git/fastcore/fastcore')"
"Path('/Users/jhoward/Documents/GitHub/fastcore/fastcore')"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1255,7 +1255,7 @@
{
"data": {
"text/plain": [
"Path('05_transform.ipynb')"
"Path('000_tour.ipynb')"
]
},
"execution_count": null,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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=<functionnoop>)\n",
"#### ReindexCollection\n",
"\n",
"Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`"
"> ReindexCollection (coll, idxs=None, cache=None, tfm=<function noop>)\n",
"\n",
"*Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`*"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer>"
"---\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=<function noop>)\n",
"\n",
"*Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`*"
]
},
"execution_count": null,
Expand Down Expand Up @@ -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": [
"<nbdev.showdoc.BasicMarkdownRenderer>"
"---\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,
Expand Down Expand Up @@ -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": [
"<nbdev.showdoc.BasicMarkdownRenderer>"
"---\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,
Expand Down Expand Up @@ -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": [
"<nbdev.showdoc.BasicMarkdownRenderer>"
"---\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,
Expand All @@ -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,
Expand Down Expand Up @@ -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": [
"<nbdev.showdoc.BasicMarkdownRenderer>"
"---\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,
Expand Down Expand Up @@ -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"
]
}
],
Expand Down Expand Up @@ -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": [
"<nbdev.showdoc.BasicMarkdownRenderer>"
"---\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,
Expand Down Expand Up @@ -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"
]
}
],
Expand Down Expand Up @@ -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"
]
}
],
Expand Down Expand Up @@ -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": [
"<nbdev.showdoc.BasicMarkdownRenderer>"
"---\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,
Expand Down Expand Up @@ -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"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7b1eaea

Please sign in to comment.