pydrobert.speech.alias

Functionality to do with alias factories

class pydrobert.speech.alias.AliasedFactory[source]

Bases: ABC

An abstract interface for initialing concrete subclasses with aliases

aliases = {}

class aliases for from_alias()

classmethod from_alias(alias, *args, **kwargs)[source]

Factory method for initializing a subclass that goes by an alias

All subclasses of this class have the class attribute aliases. This method matches alias to an element in some subclass’ aliases and initializes it. Aliases of this class are included in the search. Alias conflicts are resolved by always trying to initialize the last registered subclass that matches the alias.

Parameters:
  • alias (str) – Alias of the subclass

  • *args – Positional arguments to initialize the subclass

  • **kwargs – Keyword arguments to initialize the subclass

Raises:

ValueError – Alias can’t be found

pydrobert.speech.alias.alias_factory_subclass_from_arg(factory_class, arg)[source]

Boilerplate for getting an instance of an AliasedFactory

Rather than an instance itself, a function could receive the arguments to initialize an AliasedFactory with AliasedFactory.from_alias(). This function uses the following strategy to try and do so

  1. If arg is an instance of factory_class, return arg

  2. If arg is a str, use it as the alias

    1. Copy arg to a dictionary

    2. Pop the key 'alias' and treat the rest as keyword arguments

    3. If the key 'alias' is not found, try 'name'

This function is intentionally limited in order to work nicely with JSON config files.