Drop old `wrapt` cruft, add `Fsp.name`
							parent
							
								
									cc5390376c
								
							
						
					
					
						commit
						9d9929fb89
					
				| 
						 | 
					@ -18,8 +18,15 @@
 | 
				
			||||||
FSP (financial signal processing) apis.
 | 
					FSP (financial signal processing) apis.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
'''
 | 
					'''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# TODO: things to figure the heck out:
 | 
				
			||||||
 | 
					# - how to handle non-plottable values (pyqtgraph has facility for this
 | 
				
			||||||
 | 
					#   now in `arrayToQPath()`)
 | 
				
			||||||
 | 
					# - composition of fsps / implicit chaining syntax (we need an issue)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from __future__ import annotations
 | 
					from __future__ import annotations
 | 
				
			||||||
from functools import partial
 | 
					from functools import partial
 | 
				
			||||||
 | 
					from pprint import pformat
 | 
				
			||||||
from typing import (
 | 
					from typing import (
 | 
				
			||||||
    Any,
 | 
					    Any,
 | 
				
			||||||
    Callable,
 | 
					    Callable,
 | 
				
			||||||
| 
						 | 
					@ -30,32 +37,27 @@ from typing import (
 | 
				
			||||||
import numpy as np
 | 
					import numpy as np
 | 
				
			||||||
import tractor
 | 
					import tractor
 | 
				
			||||||
from tractor._portal import NamespacePath
 | 
					from tractor._portal import NamespacePath
 | 
				
			||||||
# import wrapt
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
from ..data._sharedmem import (
 | 
					from ..data._sharedmem import (
 | 
				
			||||||
    ShmArray,
 | 
					    ShmArray,
 | 
				
			||||||
    maybe_open_shm_array,
 | 
					    maybe_open_shm_array,
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					from ..log import get_logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					log = get_logger(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# global fsp registry filled out by @fsp decorator below
 | 
					# global fsp registry filled out by @fsp decorator below
 | 
				
			||||||
_fsp_builtins = {}
 | 
					_fsp_registry = {}
 | 
				
			||||||
    # 'rsi': _rsi,
 | 
					
 | 
				
			||||||
    # 'wma': _wma,
 | 
					 | 
				
			||||||
    # 'vwap': _tina_vwap,
 | 
					 | 
				
			||||||
    # 'dolla_vlm': dolla_vlm,
 | 
					 | 
				
			||||||
# }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _load_builtins() -> dict[tuple, Callable]:
 | 
					def _load_builtins() -> dict[tuple, Callable]:
 | 
				
			||||||
    from ._momo import _rsi, _wma
 | 
					 | 
				
			||||||
    from ._volume import tina_vwap, dolla_vlm
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return _fsp_builtins
 | 
					    # import to implicity trigger registration via ``@fsp``
 | 
				
			||||||
 | 
					    from . import _momo  # noqa
 | 
				
			||||||
 | 
					    from . import _volume  # noqa
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# TODO: things to figure the heck out:
 | 
					    log.info(f'Registered FSP set:\n{pformat(_fsp_registry)}')
 | 
				
			||||||
# - how to handle non-plottable values (pyqtgraph has facility for this
 | 
					    return _fsp_registry
 | 
				
			||||||
#   now in `arrayToQPath()`)
 | 
					 | 
				
			||||||
# - composition of fsps / implicit chaining syntax (we need an issue)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Fsp:
 | 
					class Fsp:
 | 
				
			||||||
| 
						 | 
					@ -81,18 +83,24 @@ class Fsp:
 | 
				
			||||||
        **config,
 | 
					        **config,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ) -> None:
 | 
					    ) -> None:
 | 
				
			||||||
        # if wrapped is not None:
 | 
					
 | 
				
			||||||
        #     self.name = wrapped.__name__
 | 
					        # TODO (maybe):
 | 
				
			||||||
        # TODO: should we make this a wrapt object proxy?
 | 
					        # - type introspection?
 | 
				
			||||||
 | 
					        # - should we make this a wrapt object proxy?
 | 
				
			||||||
        self.func = func
 | 
					        self.func = func
 | 
				
			||||||
        self.__name__ = func.__name__
 | 
					        self.__name__ = func.__name__  # XXX: must have func-object name
 | 
				
			||||||
        self.__module__ = func.__module__
 | 
					
 | 
				
			||||||
        self.ns_path: tuple[str, str] = NamespacePath.from_ref(func)
 | 
					        self.ns_path: tuple[str, str] = NamespacePath.from_ref(func)
 | 
				
			||||||
        _fsp_builtins[self.ns_path] = func
 | 
					 | 
				
			||||||
        self.outputs = outputs
 | 
					        self.outputs = outputs
 | 
				
			||||||
        self.config: dict[str, Any] = config
 | 
					        self.config: dict[str, Any] = config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # @wrapt.decorator
 | 
					        # register with declared set.
 | 
				
			||||||
 | 
					        _fsp_registry[self.ns_path] = func
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @property
 | 
				
			||||||
 | 
					    def name(self) -> str:
 | 
				
			||||||
 | 
					        return self.__name__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __call__(
 | 
					    def __call__(
 | 
				
			||||||
        self,
 | 
					        self,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -104,7 +112,6 @@ class Fsp:
 | 
				
			||||||
        **kwargs
 | 
					        **kwargs
 | 
				
			||||||
    ):
 | 
					    ):
 | 
				
			||||||
        return self.func(*args, **kwargs)
 | 
					        return self.func(*args, **kwargs)
 | 
				
			||||||
        # return wrapped(*args, **kwargs)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def fsp(
 | 
					def fsp(
 | 
				
			||||||
| 
						 | 
					@ -115,13 +122,8 @@ def fsp(
 | 
				
			||||||
    **config,
 | 
					    **config,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
) -> Fsp:
 | 
					) -> Fsp:
 | 
				
			||||||
    # @wrapt.decorator
 | 
					 | 
				
			||||||
    # def wrapper(wrapped, instance, args, kwargs):
 | 
					 | 
				
			||||||
    #     return wrapped(*args, **kwargs)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if wrapped is None:
 | 
					    if wrapped is None:
 | 
				
			||||||
        # return functools.partial(with_optional_arguments,
 | 
					 | 
				
			||||||
        #         myarg1=myarg1, myarg2=myarg2)
 | 
					 | 
				
			||||||
        return partial(
 | 
					        return partial(
 | 
				
			||||||
            Fsp,
 | 
					            Fsp,
 | 
				
			||||||
            outputs=outputs,
 | 
					            outputs=outputs,
 | 
				
			||||||
| 
						 | 
					@ -129,19 +131,12 @@ def fsp(
 | 
				
			||||||
            **config,
 | 
					            **config,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # return wrapper(wrapped)
 | 
					 | 
				
			||||||
    return Fsp(wrapped, outputs=(wrapped.__name__,))
 | 
					    return Fsp(wrapped, outputs=(wrapped.__name__,))
 | 
				
			||||||
        # outputs=outputs,
 | 
					 | 
				
			||||||
        # display_name=display_name,
 | 
					 | 
				
			||||||
        # **config,
 | 
					 | 
				
			||||||
    # )(wrapped)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def maybe_mk_fsp_shm(
 | 
					def maybe_mk_fsp_shm(
 | 
				
			||||||
    sym: str,
 | 
					    sym: str,
 | 
				
			||||||
    target: fsp,
 | 
					    target: fsp,
 | 
				
			||||||
    # field_name: str,
 | 
					 | 
				
			||||||
    # display_name: Optional[str] = None,
 | 
					 | 
				
			||||||
    readonly: bool = True,
 | 
					    readonly: bool = True,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
) -> (ShmArray, bool):
 | 
					) -> (ShmArray, bool):
 | 
				
			||||||
| 
						 | 
					@ -152,22 +147,14 @@ def maybe_mk_fsp_shm(
 | 
				
			||||||
    '''
 | 
					    '''
 | 
				
			||||||
    uid = tractor.current_actor().uid
 | 
					    uid = tractor.current_actor().uid
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # load declared fields from fsp and allocate in
 | 
					    # TODO: load output types from `Fsp`
 | 
				
			||||||
    # shm array.
 | 
					    # - should `index` be a required internal field?
 | 
				
			||||||
    # if not display_name:
 | 
					 | 
				
			||||||
    #     display_name = field_name
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # TODO: load function here and introspect
 | 
					 | 
				
			||||||
    # return stream type(s)
 | 
					 | 
				
			||||||
    display_name = target.__name__
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # TODO: should `index` be a required internal field?
 | 
					 | 
				
			||||||
    fsp_dtype = np.dtype(
 | 
					    fsp_dtype = np.dtype(
 | 
				
			||||||
        [('index', int)] +
 | 
					        [('index', int)] +
 | 
				
			||||||
        [(field_name, float) for field_name in target.outputs]
 | 
					        [(field_name, float) for field_name in target.outputs]
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    key = f'{sym}.fsp.{display_name}.{".".join(uid)}'
 | 
					    key = f'{sym}.fsp.{target.name}.{".".join(uid)}'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    shm, opened = maybe_open_shm_array(
 | 
					    shm, opened = maybe_open_shm_array(
 | 
				
			||||||
        key,
 | 
					        key,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue