def add(self, provider: typing.Type['Interface'], alias: typing.Optional[Alias], paths: typing.Set[Path]): """Push package to lazy loading stack. Args: provider: implementation class alias: provider alias paths: search paths to be explored when attempting to load """ references = {Reference(provider)} if alias: references.add(alias) for ref in references: if ref in self.provider: if provider == self.provider[ref]: continue raise error.Unexpected(f'Provider reference collision ({ref})') self.paths.update(paths) if isabstract(provider): return for ref in references: LOGGER.debug( 'Registering provider %s as `%s` with %d more search paths %s', provider.__name__, ref, len(paths), paths, ) self.provider[ref] = provider
def load( cls, package: typing.Optional[str] = None, path: typing.Optional[typing.Union[str, pathlib.Path]] = None, **modules, ) -> 'Descriptor': """Setup the descriptor based on provider package and/or individual modules. Either package is provided and all individual modules without dot in their names are considered as relative to that package or each module must be specified absolutely. Args: path: Path to load from. package: Base package to be considered as a root for all component modules. **modules: Component module mappings. Returns: Project descriptor. """ builder = cls.Builder() if any(c not in builder for c in modules): raise error.Unexpected('Unexpected project component') package = f'{package.rstrip(".")}.' if package else '' for component, setter in builder: mod = modules.get(component) or component if '.' not in mod: mod = package + mod try: setter(compmod.load(mod, path)) except ModuleNotFoundError as err: LOGGER.warning('Project %s error: %s', component, err) return builder.build()
def set(self, command: 'Command') -> None: """Set a command to be used by the builder. Args: command: Command to be used. """ if self._command: raise error.Unexpected('Command already provided') self._command = command
def offset(self, gid: uuid.UUID) -> int: """Get the offset of given node in the persistent node list. Args: gid: Id of node to be looked up for its offset. Returns: Offset of given node. """ if gid not in self._nodes: raise error.Unexpected(f'Unknown node ({gid})') return self._nodes.index(gid)
def __call__( self, lower: typing.Optional[kindmod.Native] = None, upper: typing.Optional[kindmod.Native] = None) -> frame.Query: query = self.query if self.ordinal is not None: if lower: query = query.where(self.ordinal >= lower) if upper: query = query.where(self.ordinal < upper) elif lower or upper: raise error.Unexpected( 'Bounds provided but source not ordinal') return query
def set_state(self, state: bytes) -> None: """Set new internal state of the actor. Note this doesn't change the setting of the actor hyper-parameters. Args: state: bytes to be used as internal state. """ if not state: return if not self.is_stateful(): raise error.Unexpected('State provided but actor stateless') LOGGER.debug('Setting %s state (%d bytes)', self, len(state)) params = self.get_params() # keep the original hyper-params with io.BytesIO(state) as bio: self.__dict__.update(joblib.load(bio)) self.set_params(**params) # restore the original hyper-params
def setup(component: typing.Any) -> None: """Component module setup handler. Args: component: Component instance to be registered. """ caller = inspect.getmodule(inspect.currentframe().f_back) if caller and caller.__name__ != module: LOGGER.warning('Ignoring setup from unexpected component of %s', caller.__name__) return LOGGER.debug('Component setup using %s', component) nonlocal result if result: raise error.Unexpected('Repeated call to component setup') result = component
def __new__( mcs, name, bases, namespace, default: typing.Optional[typing.Tuple[str, typing.Mapping[ str, typing.Any]]] = None, **kwargs, ): cls = super().__new__(mcs, name, bases, namespace, **kwargs) if default: if not isabstract(cls): raise error.Unexpected( 'Defaults provided but class not abstract') DEFAULTS[cls] = default return cls
def __init_subclass__(cls, alias: typing.Optional[str] = None, path: typing.Optional[typing.Iterable[str]] = None): """Register the provider based on its optional reference. Normally would be implemented using the Meta.__init__ but it needs the Interface class to exist. Args: alias: Optional reference to register the provider as (in addition to its qualified name). path: Optional search path for additional packages to get imported when attempting to load. """ super().__init_subclass__() if alias: if isabstract(cls): raise error.Unexpected( f'Provider reference ({alias}) illegal on abstract class') alias = Alias(alias) path = {Registry.Path(p, explicit=True) for p in path or []} for parent in (p for p in cls.__mro__ if issubclass(p, Interface) and p is not Interface): REGISTRY[parent].add(cls, alias, path)
def compose(self, left: topology.Composable) -> pipeline.Segment: """Compose the source segment track. Returns: Source segment track. """ if not isinstance(left, topology.Origin): raise error.Unexpected('Source not origin') apply: view.Path = view.Path(node.Worker(self._apply, 0, 1)) train: view.Path = view.Path(node.Worker(self._train, 0, 1)) label: typing.Optional[view.Path] = None if self._label: train_tail = node.Future() label_tail = node.Future() extract = node.Worker(self._label, 1, 2) extract[0].subscribe(train.publisher) train_tail[0].subscribe(extract[0]) label_tail[0].subscribe(extract[1]) train = train.extend(tail=train_tail) label = train.extend(tail=label_tail) return pipeline.Segment(apply, train, label)