Skip to content

Commit e634a8f

Browse files
committed
nbdev3
1 parent 955e930 commit e634a8f

File tree

6 files changed

+87
-137
lines changed

6 files changed

+87
-137
lines changed

fasttransform/cast.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_cast.ipynb.
44

5-
# %% auto 0
5+
# %% auto #0
66
__all__ = ['retain_meta', 'default_set_meta', 'cast', 'retain_type', 'retain_types', 'explode_types']
77

8-
# %% ../nbs/00_cast.ipynb 1
8+
# %% ../nbs/00_cast.ipynb #4af968d5
99
from typing import Any
1010

1111
from plum import dispatch, Function
@@ -15,14 +15,14 @@
1515
from fastcore.foundation import *
1616
from fastcore.utils import *
1717

18-
# %% ../nbs/00_cast.ipynb 6
18+
# %% ../nbs/00_cast.ipynb #8fc704ac
1919
def retain_meta(x, res, as_copy=False):
2020
"Call `res.set_meta(x)`, if it exists"
2121
if hasattr(res,'set_meta'): res.set_meta(x, as_copy=as_copy)
2222
return res
2323

2424

25-
# %% ../nbs/00_cast.ipynb 7
25+
# %% ../nbs/00_cast.ipynb #5a973173
2626
def default_set_meta(self, x, as_copy=False):
2727
"Copy over `_meta` from `x` to `res`, if it's missing"
2828
if hasattr(x, '_meta') and not hasattr(self, '_meta'):
@@ -32,7 +32,7 @@ def default_set_meta(self, x, as_copy=False):
3232
return self
3333

3434

35-
# %% ../nbs/00_cast.ipynb 8
35+
# %% ../nbs/00_cast.ipynb #05108cf6
3636
def cast(x, typ):
3737
"cast `x` to type `typ` (may also change `x` inplace)"
3838
res = typ._before_cast(x) if hasattr(typ, '_before_cast') else x
@@ -43,7 +43,7 @@ def cast(x, typ):
4343
except: res = typ(res)
4444
return retain_meta(x, res)
4545

46-
# %% ../nbs/00_cast.ipynb 15
46+
# %% ../nbs/00_cast.ipynb #1364fbd1
4747
def retain_type(new, old, ret_type=Any,as_copy=False):
4848
"Cast `new` to `ret_type` if given, or `old`'s type if `new` is a superclass of `old`. No conversion is done if `ret_type=None`"
4949
if new is None: return new
@@ -54,7 +54,7 @@ def retain_type(new, old, ret_type=Any,as_copy=False):
5454
if ret_type is NoneType or isinstance(new,ret_type): return new
5555
return retain_meta(old, cast(new, ret_type), as_copy=as_copy)
5656

57-
# %% ../nbs/00_cast.ipynb 40
57+
# %% ../nbs/00_cast.ipynb #f3cd995c
5858
def retain_types(new, old=None, typs=None):
5959
"Cast each item of `new` to type of matching item in `old` if it's a superclass"
6060
if not is_listy(new):
@@ -69,7 +69,7 @@ def retain_types(new, old=None, typs=None):
6969
return t(L(new, old, typs).map_zip(retain_types, cycled=True))
7070

7171

72-
# %% ../nbs/00_cast.ipynb 42
72+
# %% ../nbs/00_cast.ipynb #c6f1ec26
7373
def explode_types(o):
7474
"Return the type of `o`, potentially in nested dictionaries for thing that are listy"
7575
if not is_listy(o): return type(o)

fasttransform/transform.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_transform.ipynb.
44

5-
# %% auto 0
5+
# %% auto #0
66
__all__ = ['Sig', 'Transform', 'InplaceTransform', 'DisplayedTransform', 'ItemTransform', 'get_func', 'Func', 'compose_tfms',
77
'mk_transform', 'gather_attrs', 'gather_attr_names', 'Pipeline']
88

9-
# %% ../nbs/01_transform.ipynb 1
9+
# %% ../nbs/01_transform.ipynb #4af968d5
1010
from typing import Any
1111
import inspect
1212

@@ -18,25 +18,25 @@
1818

1919
from .cast import *
2020

21-
# %% ../nbs/01_transform.ipynb 6
21+
# %% ../nbs/01_transform.ipynb #83a170a6
2222
def _get_name(o):
2323
if hasattr(o,'__qualname__'): return o.__qualname__
2424
if hasattr(o,'__name__'): return o.__name__
2525
return o.__class__.__name__
2626

27-
# %% ../nbs/01_transform.ipynb 7
27+
# %% ../nbs/01_transform.ipynb #1e0369fe
2828
def _is_tuple(o): return isinstance(o, tuple) and not hasattr(o, '_fields')
2929

30-
# %% ../nbs/01_transform.ipynb 8
30+
# %% ../nbs/01_transform.ipynb #d6cd8ac7
3131
_tfm_methods = 'encodes','decodes','setups'
3232
def _is_tfm_method(n, f): return n in _tfm_methods and callable(f)
3333

34-
# %% ../nbs/01_transform.ipynb 9
34+
# %% ../nbs/01_transform.ipynb #7635ad80
3535
def _has_self_arg(f) -> bool:
3636
try: return f.__code__.co_varnames[0] == 'self'
3737
except (AttributeError, IndexError): return False
3838

39-
# %% ../nbs/01_transform.ipynb 10
39+
# %% ../nbs/01_transform.ipynb #4d3d57e2-bd3c-431e-bfbb-b3043d4a8a7c
4040
def _merge_funcs(*fs):
4141
"Merge multiple plum Functions by combining their methods"
4242
fs = fs[::-1] # overwrite old implementations with new ones
@@ -45,14 +45,14 @@ def _merge_funcs(*fs):
4545
for m in f.methods: res.dispatch(m.implementation)
4646
return res
4747

48-
# %% ../nbs/01_transform.ipynb 12
48+
# %% ../nbs/01_transform.ipynb #ea6f80d3
4949
class _TfmDict(dict):
5050
def __setitem__(self, k, v):
5151
if not _is_tfm_method(k, v): return super().__setitem__(k,v)
5252
if k not in self: super().__setitem__(k, Function(v))
5353
self[k].dispatch(v)
5454

55-
# %% ../nbs/01_transform.ipynb 13
55+
# %% ../nbs/01_transform.ipynb #3c9b4601
5656
class _TfmMeta(type):
5757
@classmethod
5858
def __prepare__(cls, name, bases): return _TfmDict()
@@ -82,7 +82,7 @@ def __new__(cls, name, bases, namespace):
8282
new_cls.__signature__ = inspect.signature(new_cls.__init__)
8383
return new_cls
8484

85-
# %% ../nbs/01_transform.ipynb 14
85+
# %% ../nbs/01_transform.ipynb #26e38f6d
8686
class Transform(metaclass=_TfmMeta):
8787
"Delegates (`__call__`,`decode`,`setup`) to (<code>encodes</code>,<code>decodes</code>,<code>setups</code>) if `split_idx` matches"
8888
split_idx,init_enc,order,train_setup = None,None,0,None
@@ -137,21 +137,21 @@ def _do_call(self, nm, *args, **kwargs):
137137

138138
add_docs(Transform, decode="Delegate to decodes to undo transform", setup="Delegate to setups to set up transform")
139139

140-
# %% ../nbs/01_transform.ipynb 156
140+
# %% ../nbs/01_transform.ipynb #a494d4a5
141141
class InplaceTransform(Transform):
142142
"A `Transform` that modifies in-place and just returns whatever it's passed"
143143
def _call(self, fn, *args, split_idx=None, **kwargs):
144144
super()._call(fn,*args, split_idx=split_idx, **kwargs)
145145
return args[0]
146146

147-
# %% ../nbs/01_transform.ipynb 160
147+
# %% ../nbs/01_transform.ipynb #d92cfd60
148148
class DisplayedTransform(Transform):
149149
"A transform with a `__repr__` that shows its attrs"
150150

151151
@property
152152
def name(self): return f"{super().name} -- {getattr(self,'__stored_args__',{})}\n"
153153

154-
# %% ../nbs/01_transform.ipynb 166
154+
# %% ../nbs/01_transform.ipynb #09e4c9ce
155155
class ItemTransform(Transform):
156156
"A transform that always take tuples as items"
157157
_retain = True
@@ -165,21 +165,21 @@ def _call1(self, x, name, **kwargs):
165165
return retain_type(y, x, Any)
166166

167167

168-
# %% ../nbs/01_transform.ipynb 175
168+
# %% ../nbs/01_transform.ipynb #2bce2ab9
169169
def get_func(t, name, *args, **kwargs):
170170
"Get the `t.name` (potentially partial-ized with `args` and `kwargs`) or `noop` if not defined"
171171
f = nested_callable(t, name)
172172
return f if not (args or kwargs) else partial(f, *args, **kwargs)
173173

174-
# %% ../nbs/01_transform.ipynb 179
174+
# %% ../nbs/01_transform.ipynb #c7704028
175175
class Func():
176176
"Basic wrapper around a `name` with `args` and `kwargs` to call on a given type"
177177
def __init__(self, name, *args, **kwargs): self.name,self.args,self.kwargs = name,args,kwargs
178178
def __repr__(self): return f'sig: {self.name}({self.args}, {self.kwargs})'
179179
def _get(self, t): return get_func(t, self.name, *self.args, **self.kwargs)
180180
def __call__(self,t): return mapped(self._get, t)
181181

182-
# %% ../nbs/01_transform.ipynb 182
182+
# %% ../nbs/01_transform.ipynb #cc174bb3
183183
class _Sig():
184184
def __getattr__(self,k):
185185
def _inner(*args, **kwargs): return Func(k, *args, **kwargs)
@@ -188,7 +188,7 @@ def _inner(*args, **kwargs): return Func(k, *args, **kwargs)
188188
Sig = _Sig()
189189

190190

191-
# %% ../nbs/01_transform.ipynb 188
191+
# %% ../nbs/01_transform.ipynb #63fa926a
192192
def compose_tfms(x, tfms, is_enc=True, reverse=False, **kwargs):
193193
"Apply all `func_nm` attribute of `tfms` on `x`, maybe in `reverse` order"
194194
if reverse: tfms = reversed(tfms)
@@ -198,13 +198,13 @@ def compose_tfms(x, tfms, is_enc=True, reverse=False, **kwargs):
198198
return x
199199

200200

201-
# %% ../nbs/01_transform.ipynb 193
201+
# %% ../nbs/01_transform.ipynb #3da3898b
202202
def mk_transform(f):
203203
"Convert function `f` to `Transform` if it isn't already one"
204204
f = instantiate(f)
205205
return f if isinstance(f,(Transform,Pipeline)) else Transform(f)
206206

207-
# %% ../nbs/01_transform.ipynb 194
207+
# %% ../nbs/01_transform.ipynb #7456b063
208208
def gather_attrs(o, k, nm):
209209
"Used in __getattr__ to collect all attrs `k` from `self.{nm}`"
210210
if k.startswith('_') or k==nm: raise AttributeError(k)
@@ -213,12 +213,12 @@ def gather_attrs(o, k, nm):
213213
if not res: raise AttributeError(k)
214214
return res[0] if len(res)==1 else L(res)
215215

216-
# %% ../nbs/01_transform.ipynb 195
216+
# %% ../nbs/01_transform.ipynb #f9965510
217217
def gather_attr_names(o, nm):
218218
"Used in __dir__ to collect all attrs `k` from `self.{nm}`"
219219
return L(getattr(o,nm)).map(dir).concat().unique()
220220

221-
# %% ../nbs/01_transform.ipynb 196
221+
# %% ../nbs/01_transform.ipynb #a1928a2e
222222
class Pipeline:
223223
"A pipeline of composed (for encode/decode) transforms, setup with types"
224224
def __init__(self, funcs=None, split_idx=None):

0 commit comments

Comments
 (0)