Skip to content

declearn.model.api.register_vector_type

Decorate a Vector subclass to make it buildable with Vector.build.

Decorating a Vector subclass with this has three effects:

  • Add the class to registered type (in the "Vector" group). See declearn.utils.register_type for details.
  • Make instances of that class JSON-serializable, embarking the wrapped data by using the pack and unpack methods of the class. See declearn.utils.add_json_support.
  • Make the subclass buildable through Vector.build(coefs), based on the analysis of wrapped coefficients' type.

Parameters:

Name Type Description Default
v_type Type[Any]

Type of wrapped data that is to trigger using cls.

required
*types Type[Any]

Additional v_type alternatives for wrapped data.

()
name Optional[str]

Optional name under which to register the type, shared by register_type and add_json_support. If None, use cls.__name__.

None

Returns:

Name Type Description
register func(cls) -

A closure that performs the registration operations. Hence register_vector_type is designed to be used as a class decorator.

Source code in declearn/model/api/_vector.py
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
def register_vector_type(
    v_type: Type[Any],
    *types: Type[Any],
    name: Optional[str] = None,
) -> Callable[[Type[Vector]], Type[Vector]]:
    """Decorate a Vector subclass to make it buildable with `Vector.build`.

    Decorating a Vector subclass with this has three effects:

    * Add the class to registered type (in the "Vector" group).
      See `declearn.utils.register_type` for details.
    * Make instances of that class JSON-serializable, embarking
      the wrapped data by using the `pack` and `unpack` methods
      of the class. See `declearn.utils.add_json_support`.
    * Make the subclass buildable through `Vector.build(coefs)`,
      based on the analysis of wrapped coefficients' type.

    Parameters
    ----------
    v_type: type
        Type of wrapped data that is to trigger using `cls`.
    *types: type
        Additional `v_type` alternatives for wrapped data.
    name: str or None, default=None
        Optional name under which to register the type, shared
        by `register_type` and `add_json_support`.
        If None, use `cls.__name__`.

    Returns
    -------
    register: func(cls) -> cls
        A closure that performs the registration operations.
        Hence `register_vector_type` is designed to be used
        as a class decorator.
    """
    v_types = (v_type, *types)

    # Set up a registration function.
    def register(cls: Type[Vector]) -> Type[Vector]:
        nonlocal name, v_types
        if name is None:
            name = cls.__name__
        # Register the Vector type. Note: this type-checks cls.
        register_type(cls, name=name, group="Vector")
        # Add support for JSON (de)serialization, relying on (un)pack.
        add_json_support(cls, cls.pack, cls.unpack, name=name)
        # Make the subclass buildable through `Vector.build(coefs)`.
        for v_type in v_types:
            VECTOR_TYPES[v_type] = cls
        return cls

    # Return the former, enabling decoration syntax for `register_vector_type`.
    return register