Skip to content

Commit

Permalink
simplify modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Rens Dimmendaal committed Jan 27, 2025
1 parent 4bcb615 commit 080c623
Show file tree
Hide file tree
Showing 11 changed files with 800 additions and 918 deletions.
2 changes: 1 addition & 1 deletion fasttransform/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.1"

from .cast import *
from .transform import *
from .pipeline import *
83 changes: 42 additions & 41 deletions fasttransform/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,12 @@
'doc_host': 'https://AnswerDotAI.github.io',
'git_url': 'https://github.com/AnswerDotAI/fasttransform',
'lib_path': 'fasttransform'},
'syms': { 'fasttransform.dispatch': { 'fasttransform.dispatch.cast': ('dispatch.html#cast', 'fasttransform/dispatch.py'),
'fasttransform.dispatch.default_set_meta': ( 'dispatch.html#default_set_meta',
'fasttransform/dispatch.py'),
'fasttransform.dispatch.explode_types': ( 'dispatch.html#explode_types',
'fasttransform/dispatch.py'),
'fasttransform.dispatch.get_name': ('dispatch.html#get_name', 'fasttransform/dispatch.py'),
'fasttransform.dispatch.is_tuple': ('dispatch.html#is_tuple', 'fasttransform/dispatch.py'),
'fasttransform.dispatch.merge_funcs': ('dispatch.html#merge_funcs', 'fasttransform/dispatch.py'),
'fasttransform.dispatch.retain_meta': ('dispatch.html#retain_meta', 'fasttransform/dispatch.py'),
'fasttransform.dispatch.retain_type': ('dispatch.html#retain_type', 'fasttransform/dispatch.py'),
'fasttransform.dispatch.retain_types': ('dispatch.html#retain_types', 'fasttransform/dispatch.py')},
'fasttransform.pipeline': { 'fasttransform.pipeline.Pipeline': ('pipeline.html#pipeline', 'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.__call__': ( 'pipeline.html#pipeline.__call__',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.__dir__': ( 'pipeline.html#pipeline.__dir__',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.__getattr__': ( 'pipeline.html#pipeline.__getattr__',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.__getitem__': ( 'pipeline.html#pipeline.__getitem__',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.__init__': ( 'pipeline.html#pipeline.__init__',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.__repr__': ( 'pipeline.html#pipeline.__repr__',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.__setstate__': ( 'pipeline.html#pipeline.__setstate__',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline._is_showable': ( 'pipeline.html#pipeline._is_showable',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.add': ('pipeline.html#pipeline.add', 'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.decode': ( 'pipeline.html#pipeline.decode',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.setup': ( 'pipeline.html#pipeline.setup',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.Pipeline.show': ( 'pipeline.html#pipeline.show',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.compose_tfms': ('pipeline.html#compose_tfms', 'fasttransform/pipeline.py'),
'fasttransform.pipeline.gather_attr_names': ( 'pipeline.html#gather_attr_names',
'fasttransform/pipeline.py'),
'fasttransform.pipeline.gather_attrs': ('pipeline.html#gather_attrs', 'fasttransform/pipeline.py'),
'fasttransform.pipeline.mk_transform': ('pipeline.html#mk_transform', 'fasttransform/pipeline.py')},
'syms': { 'fasttransform.cast': { 'fasttransform.cast.cast': ('cast.html#cast', 'fasttransform/cast.py'),
'fasttransform.cast.default_set_meta': ('cast.html#default_set_meta', 'fasttransform/cast.py'),
'fasttransform.cast.explode_types': ('cast.html#explode_types', 'fasttransform/cast.py'),
'fasttransform.cast.retain_meta': ('cast.html#retain_meta', 'fasttransform/cast.py'),
'fasttransform.cast.retain_type': ('cast.html#retain_type', 'fasttransform/cast.py'),
'fasttransform.cast.retain_types': ('cast.html#retain_types', 'fasttransform/cast.py')},
'fasttransform.transform': { 'fasttransform.transform.DisplayedTransform': ( 'transform.html#displayedtransform',
'fasttransform/transform.py'),
'fasttransform.transform.DisplayedTransform.name': ( 'transform.html#displayedtransform.name',
Expand All @@ -69,6 +35,31 @@
'fasttransform/transform.py'),
'fasttransform.transform.ItemTransform.decode': ( 'transform.html#itemtransform.decode',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline': ('transform.html#pipeline', 'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.__call__': ( 'transform.html#pipeline.__call__',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.__dir__': ( 'transform.html#pipeline.__dir__',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.__getattr__': ( 'transform.html#pipeline.__getattr__',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.__getitem__': ( 'transform.html#pipeline.__getitem__',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.__init__': ( 'transform.html#pipeline.__init__',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.__repr__': ( 'transform.html#pipeline.__repr__',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.__setstate__': ( 'transform.html#pipeline.__setstate__',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline._is_showable': ( 'transform.html#pipeline._is_showable',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.add': ( 'transform.html#pipeline.add',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.decode': ( 'transform.html#pipeline.decode',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.setup': ( 'transform.html#pipeline.setup',
'fasttransform/transform.py'),
'fasttransform.transform.Pipeline.show': ( 'transform.html#pipeline.show',
'fasttransform/transform.py'),
'fasttransform.transform.Transform': ('transform.html#transform', 'fasttransform/transform.py'),
'fasttransform.transform.Transform.__call__': ( 'transform.html#transform.__call__',
'fasttransform/transform.py'),
Expand Down Expand Up @@ -105,4 +96,14 @@
'fasttransform.transform._is_tfm_method': ( 'transform.html#_is_tfm_method',
'fasttransform/transform.py'),
'fasttransform.transform._is_tuple': ('transform.html#_is_tuple', 'fasttransform/transform.py'),
'fasttransform.transform.get_func': ('transform.html#get_func', 'fasttransform/transform.py')}}}
'fasttransform.transform._merge_funcs': ( 'transform.html#_merge_funcs',
'fasttransform/transform.py'),
'fasttransform.transform.compose_tfms': ( 'transform.html#compose_tfms',
'fasttransform/transform.py'),
'fasttransform.transform.gather_attr_names': ( 'transform.html#gather_attr_names',
'fasttransform/transform.py'),
'fasttransform.transform.gather_attrs': ( 'transform.html#gather_attrs',
'fasttransform/transform.py'),
'fasttransform.transform.get_func': ('transform.html#get_func', 'fasttransform/transform.py'),
'fasttransform.transform.mk_transform': ( 'transform.html#mk_transform',
'fasttransform/transform.py')}}}
41 changes: 11 additions & 30 deletions fasttransform/dispatch.py → fasttransform/cast.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Dispatch utility functions for compatibility with other libraries"""
"""Type casting utility functions"""

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

# %% auto 0
__all__ = ['get_name', 'is_tuple', 'retain_meta', 'default_set_meta', 'cast', 'retain_type', 'retain_types', 'explode_types',
'merge_funcs']
__all__ = ['retain_meta', 'default_set_meta', 'cast', 'retain_type', 'retain_types', 'explode_types']

# %% ../nbs/00_dispatch.ipynb 1
# %% ../nbs/00_cast.ipynb 1
from typing import Any

from plum import dispatch, Function
Expand All @@ -16,23 +15,14 @@
from fastcore.foundation import *
from fastcore.utils import *

# %% ../nbs/00_dispatch.ipynb 7
def get_name(o):
if hasattr(o,'__qualname__'): return o.__qualname__
if hasattr(o,'__name__'): return o.__name__
return o.__class__.__name__

# %% ../nbs/00_dispatch.ipynb 8
def is_tuple(o): return isinstance(o, tuple) and not hasattr(o, '_fields')

# %% ../nbs/00_dispatch.ipynb 11
# %% ../nbs/00_cast.ipynb 6
def retain_meta(x, res, as_copy=False):
"Call `res.set_meta(x)`, if it exists"
if hasattr(res,'set_meta'): res.set_meta(x, as_copy=as_copy)
return res


# %% ../nbs/00_dispatch.ipynb 12
# %% ../nbs/00_cast.ipynb 7
def default_set_meta(self, x, as_copy=False):
"Copy over `_meta` from `x` to `res`, if it's missing"
if hasattr(x, '_meta') and not hasattr(self, '_meta'):
Expand All @@ -42,8 +32,7 @@ def default_set_meta(self, x, as_copy=False):
return self


# %% ../nbs/00_dispatch.ipynb 13
@dispatch
# %% ../nbs/00_cast.ipynb 8
def cast(x, typ):
"cast `x` to type `typ` (may also change `x` inplace)"
res = typ._before_cast(x) if hasattr(typ, '_before_cast') else x
Expand All @@ -54,8 +43,9 @@ def cast(x, typ):
except: res = typ(res)
return retain_meta(x, res)

# %% ../nbs/00_dispatch.ipynb 20
# %% ../nbs/00_cast.ipynb 15
def retain_type(new, old, ret_type=Any,as_copy=False):
"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`"
if new is None: return new
if ret_type is NoneType: return new
if ret_type is Any:
Expand All @@ -64,7 +54,7 @@ def retain_type(new, old, ret_type=Any,as_copy=False):
if ret_type is NoneType or isinstance(new,ret_type): return new
return retain_meta(old, cast(new, ret_type), as_copy=as_copy)

# %% ../nbs/00_dispatch.ipynb 42
# %% ../nbs/00_cast.ipynb 40
def retain_types(new, old=None, typs=None):
"Cast each item of `new` to type of matching item in `old` if it's a superclass"
if not is_listy(new):
Expand All @@ -79,17 +69,8 @@ def retain_types(new, old=None, typs=None):
return t(L(new, old, typs).map_zip(retain_types, cycled=True))


# %% ../nbs/00_dispatch.ipynb 44
# %% ../nbs/00_cast.ipynb 42
def explode_types(o):
"Return the type of `o`, potentially in nested dictionaries for thing that are listy"
if not is_listy(o): return type(o)
return {type(o): [explode_types(o_) for o_ in o]}

# %% ../nbs/00_dispatch.ipynb 47
def merge_funcs(*fs):
"Merge multiple plum Functions by combining their methods"
fs = fs[::-1] # overwrite old implementations with new ones
res = Function(fs[-1].methods[0].implementation)
for f in fs:
for m in f.methods: res.dispatch(m.implementation)
return res
100 changes: 0 additions & 100 deletions fasttransform/pipeline.py

This file was deleted.

Loading

0 comments on commit 080c623

Please sign in to comment.