eradiate.scenes.spectra._core.SpectrumFactory#
- class eradiate.scenes.spectra._core.SpectrumFactory(registry=NOTHING)[source]#
Bases:
Factory
- alias(type_id, alias_id, overwrite_id=False)#
Register a new alias to a registered type.
- Parameters:
type_id – ID of the aliased type.
alias_id – Created alias ID.
- Raises:
Added in version 22.2.0.
- convert(value=<class 'dessinemoi._core._Missing'>, *, allowed_cls=None)#
Convert a dictionary to one of the types supported by the factory.
Note
All arguments, except
self
andvalue
, are keyword-only.- Parameters:
value – Value to attempt conversion of. If
value
is a dictionary, the method tries to convert it to a registered type based on thetype
. Ifvalue
is not a dictionary, it is returned without change. Ifvalue
is unset, the method returns a callable which can later be used for conversion.allowed_cls – Types to restrict conversion to. If set,
value
will be checked and an exception will be raised if it does not have one of the allowed types.
- Returns:
Created object if
value
is a dictionary;value
otherwise.- Raises:
TypeError – If
allowed_cls
is specified andvalue.type
refers to a disallowed type ortype(value)
is disallowed.
Changed in version 21.3.0: Made all args keyword-only except for
value
.
- converter(quantity)[source]#
Generate a converter wrapping
SpectrumFactory.convert()
to handle defaults for shortened spectrum definitions. The produced converter processes a parametervalue
as follows:if
value
is an int, a float or apint.Quantity
, the converter calls itself using a dictionary{"type": "uniform", "quantity": quantity, "value": value}
;if
value
is a dictionary, it adds a"quantity": quantity
entry for the following values of the"type"
entry:"uniform"
;"interpolated"
;"multi_delta"
;
otherwise, it forwards
value
toSpectrumFactory.convert()
.
- Parameters:
quantity (
str
orPhysicalQuantity
) – Quantity specifier (converted byPhysicalQuantity
). SeePhysicalQuantity.spectrum()
for suitable values.- Returns:
callable()
– Generated converter.
- create(type_id, allowed_cls=None, construct=None, args=None, kwargs=None)#
Create a new instance of a registered type.
- Parameters:
type_id – ID of the type to be instantiated.
allowed_cls – If not
None
, one or several types to which creation shall be restricted. Iftype_id
does not reference one of these allowed types, an exception will be raised.construct – If not
None
, attempt instantiation using a class method constructor instead of the default constructor.args – A sequence of arguments to pass to the constructor of the created type.
kwargs – A mapping of keyword arguments to passed to the constructor of the created type.
- Returns:
Created object.
- Raises:
ValueError – If
type_id
does not reference a registered type.TypeError – If the requested type is not allowed.
Changed in version 21.2.0: Added
construct
keyword argument.
- get_type(type_id)#
Return the type corresponding to the requested type ID. Lazy types will be loaded.
- Parameters:
type_id – ID of a registered type.
- Returns:
Corresponding type.
Added in version 22.1.1.
- register(cls=<class 'dessinemoi._core._Missing'>, *, type_id=None, dict_constructor=None, aliases=None, overwrite_id=False, allow_lazy=True)#
If parameter
cls
is passed, registercls
to the factory. Otherwise, i.e. if this method is used as a decorator, register the decorated class to the factory.Note
All arguments, except
cls
, are keyword-only.- Parameters:
cls – If set, type to register to the factory. If unset, this function returns a callable which can be used to register classes. In practice, this parameter is unset when the method is used as a class decorator. A
LazyType
instance or a string convertible toLazyType
may also be passed.type_id – Identifier string used to register
cls
. Required ifcls
is a lazy type or if it does not specify its identifier itself.dict_constructor – Class method to be used for dictionary-based construction. If
None
, the default constructor is used.aliases – If
True
, a given type can be registered multiple times under different IDs.overwrite_id – If
True
, existing IDs can be overwritten.allow_lazy – If
False
, force eager loading of lazy types.
- Raises:
ValueError – If
allow_aliases
isFalse
andcls
is already registered.ValueError – If
allow_id_overwrite
isFalse
andtype_id
is already used to reference a type in the registry.
Changed in version 21.3.0: Made keyword-only.
Changed in version 21.3.0: Added
dict_constructor
argument.Changed in version 22.1.0: Added
allow_lazy
argument. AcceptLazyType
and strings forcls
.Changed in version 22.2.0: Renamed
allow_id_overwrite
tooverwrite_id
. Removedallow_aliases
, replaced byaliases
.
- register_lazy_batch(specs, cls_prefix='')#
Register multiple lazy types at once.
- Parameters:
specs (
list
oftuple[str
,str
,dict]
) – A list of(cls, type_id, kwargs)
tuples wherecls
is the name of the target type (relative to cls_prefix),type_id
is the ID for the registered type andkwargs
is a dictionary containing keyword arguments for theFactory.register()
method.cls_prefix (
str
) – A prefix relative to which lazy type names are expressed.
- property registered_types#
List of currently registered types, without duplicates.
Added in version 21.3.0.
- registry#
Dictionary holding the factory registry.
Changed in version 21.3.0: Changed type from
Dict[str, Type]
toDict[str, FactoryRegistryEntry]
.