Factory

Sorry, the person who is responsible for the description was eaten by hydras last week.

hydra_slayer.factory.call_meta_factory(factory: Union[Type, Callable[[...], Any]], args: Tuple, kwargs: Mapping)[source]

Creates a new instance from factory.

Parameters
  • factory – factory to create instance from

  • args – positional arguments to be passed into the factory

  • kwargs – keyword arguments to be passed into the factory

Returns

Instance.

Examples

>>> call_meta_factory(int, (42,), {})
42
>>> call_meta_factory(int, ('2A',), {'base': 16})
66
>>> call_meta_factory(lambda x: x, (42,), {})
42
hydra_slayer.factory.default_meta_factory(factory: Union[Type, Callable[[...], Any]], args: Tuple, kwargs: Mapping)[source]

Returns a new instance or a new partial object.

Creates a new instance from factory if factory is class (behaves like call_meta_factory()), else returns a new partial object (behaves like partial_meta_factory()).

Parameters
  • factory – factory to create instance from

  • args – positional arguments to be passed into the factory

  • kwargs – keyword arguments to be passed into the factory

Returns

Instance.

Raises

ValueError – if factory object is not callable.

Examples

>>> default_meta_factory(int, (42,), {})
42
>>> default_meta_factory(int, ('2A',), {'base': 16})
42
>>> get_answer_to_life = default_meta_factory(lambda x: x, (42,), {})
>>> get_answer_to_life()
42
hydra_slayer.factory.metafactory_factory(factory: Union[Type, Callable[[...], Any]], args: Tuple, kwargs: Mapping)[source]

Returns a new instance or a new partial object.

  • _mode_=’auto’

    Creates a new instance from factory if factory is class (like call_meta_factory()), else returns a new partial object (like partial_meta_factory()).

  • _mode_=’call’

    Returns a result of the factory called with the positional arguments args and keyword arguments kwargs.

  • _mode_=’partial’

    Returns a new partial object which when called will behave like factory called with the positional arguments args and keyword arguments kwargs.

Parameters
  • factory – factory to create instance from

  • args – positional arguments to be passed into the factory

  • kwargs – keyword arguments to be passed into the factory

Returns

Instance.

Raises

ValueError – if mode not in list: 'auto', 'call', 'partial'.

Examples

>>> metafactory_factory(int, (42,))
42
>>> metafactory_factory(lambda x: x, (42,))()  # note that additional () are used
42
>>> metafactory_factory(lambda x: x, (42,), {'_mode_': 'call'})
42
>>> metafactory_factory(int, ('2A'), {'base': 16})
42
>>> hex_to_dec = metafactory_factory(int, (), {'_mode_': 'partial', 'base': 16})
>>> hex_to_dec('2A')
42
hydra_slayer.factory.partial_meta_factory(factory: Union[Type, Callable[[...], Any]], args: Tuple, kwargs: Mapping)[source]

Returns a new partial object which when called will behave like func called with the positional arguments args and keyword arguments kwargs.

Parameters
  • factory – factory to create instance from

  • args – positional arguments to be merged into the factory

  • kwargs – keyword arguments to be merged into the factory

Returns

Partial object.

Examples

>>> get_answer_to_life = partial_meta_factory(lambda x: x, (42,), {})
>>> get_answer_to_life()
42
>>> hex_to_dec = partial_meta_factory(int, (), {'base': 16})
>>> hex_to_dec('2A')
42