Skip to content

Builder

momapy.builder

Classes and functions for building maps and map elements

Classes:

Name Description
Builder

Abstract class for builder objects

Functions:

Name Description
builder_from_object

Create and return a builder object from an object

get_builder_cls

Return the builder class registered for the given class or None if no builder class is registered for that class

get_or_make_builder_cls

Get and return an existing builder class for the given class or make and return a new builder class for it

has_builder_cls

Return true if there is a registered builder class for the given class, and false otherwise

isinstance_or_builder

Return true if the object is an istance of the given classes or of their registered builder classes, and false otherwise

issubclass_or_builder

Return true if the class is a subclass of the given classes or of their registered builder classes, and false otherwise

new_builder_object

Create and return a builder object from an object class or a builder class

object_from_builder

Create and return an object from a builder object

register_builder_cls

Register a builder class

super_or_builder

Return the super class for a given class or its builder class and an object

Builder dataclass

Builder()

Bases: ABC, Monitored

Abstract class for builder objects

Methods:

Name Description
build

Build and return an object from the builder object

from_object

Create and return a builder object from an object

build abstractmethod

build(inside_collections: bool = True, builder_to_object: dict[int, Any] | None = None) -> Any

Build and return an object from the builder object

Source code in src/momapy/builder.py
@abc.abstractmethod
def build(
    self,
    inside_collections: bool = True,
    builder_to_object: dict[int, typing.Any] | None = None,
) -> typing.Any:
    """Build and return an object from the builder object"""
    pass

from_object abstractmethod classmethod

from_object(obj: Any, inside_collections: bool = True, omit_keys: bool = True, object_to_builder: dict[int, Builder] | None = None) -> Self

Create and return a builder object from an object

Source code in src/momapy/builder.py
@classmethod
@abc.abstractmethod
def from_object(
    cls,
    obj: typing.Any,
    inside_collections: bool = True,
    omit_keys: bool = True,
    object_to_builder: dict[int, "Builder"] | None = None,
) -> typing.Self:
    """Create and return a builder object from an object"""
    pass

builder_from_object

builder_from_object(obj: Any, inside_collections=True, omit_keys=True, object_to_builder: dict[int, Builder] | None = None) -> Builder

Create and return a builder object from an object

Source code in src/momapy/builder.py
def builder_from_object(
    obj: typing.Any,
    inside_collections=True,
    omit_keys=True,
    object_to_builder: dict[int, "Builder"] | None = None,
) -> Builder:
    """Create and return a builder object from an object"""
    if object_to_builder is not None:
        builder = object_to_builder.get(id(obj))
        if builder is not None:
            return builder
    else:
        object_to_builder = {}
    cls = get_or_make_builder_cls(type(obj))
    if issubclass(cls, Builder):
        return cls.from_object(
            obj=obj,
            inside_collections=inside_collections,
            omit_keys=omit_keys,
            object_to_builder=object_to_builder,
        )
    if inside_collections:
        if isinstance(obj, (list, tuple, set, frozenset)):
            return type(obj)(
                [
                    builder_from_object(
                        obj=e,
                        inside_collections=inside_collections,
                        omit_keys=omit_keys,
                        object_to_builder=object_to_builder,
                    )
                    for e in obj
                ]
            )
        elif isinstance(obj, (dict, frozendict.frozendict)):
            return type(obj)(
                [
                    (
                        (
                            builder_from_object(
                                obj=k,
                                inside_collections=inside_collections,
                                omit_keys=omit_keys,
                                object_to_builder=object_to_builder,
                            ),
                            builder_from_object(
                                obj=v,
                                inside_collections=inside_collections,
                                omit_keys=omit_keys,
                                object_to_builder=object_to_builder,
                            ),
                        )
                        if not omit_keys
                        else (
                            k,
                            builder_from_object(
                                obj=v,
                                inside_collections=inside_collections,
                                omit_keys=omit_keys,
                                object_to_builder=object_to_builder,
                            ),
                        )
                    )
                    for k, v in obj.items()
                ]
            )
    return obj

get_builder_cls

get_builder_cls(cls: Type) -> Type

Return the builder class registered for the given class or None if no builder class is registered for that class

Source code in src/momapy/builder.py
def get_builder_cls(cls: typing.Type) -> typing.Type:
    """Return the builder class registered for the given class or `None` if no builder class is registered for that class"""
    return builders.get(cls)

get_or_make_builder_cls

get_or_make_builder_cls(cls: Type, builder_fields: Collection[tuple[str, Type, Field]] | None = None, builder_bases: Collection[Type] | None = None, builder_namespace: dict[str, Any] | None = None) -> Type

Get and return an existing builder class for the given class or make and return a new builder class for it

Source code in src/momapy/builder.py
def get_or_make_builder_cls(
    cls: typing.Type,
    builder_fields: (
        typing.Collection[tuple[str, typing.Type, dataclasses.Field]] | None
    ) = None,
    builder_bases: typing.Collection[typing.Type] | None = None,
    builder_namespace: dict[str, typing.Any] | None = None,
) -> typing.Type:
    """Get and return an existing builder class for the given class or make and return a new builder class for it"""
    builder_cls = get_builder_cls(cls)
    if builder_cls is None:
        if dataclasses.is_dataclass(cls):
            builder_cls = _make_builder_cls(
                cls, builder_fields, builder_bases, builder_namespace
            )
            register_builder_cls(builder_cls)
        else:
            builder_cls = cls
    return builder_cls

has_builder_cls

has_builder_cls(cls: Type) -> bool

Return true if there is a registered builder class for the given class, and false otherwise

Source code in src/momapy/builder.py
def has_builder_cls(cls: typing.Type) -> bool:
    """Return `true` if there is a registered builder class for the given class, and `false` otherwise"""
    return cls in builders

isinstance_or_builder

isinstance_or_builder(obj: Any, type_: Type | tuple[Type]) -> bool

Return true if the object is an istance of the given classes or of their registered builder classes, and false otherwise

Source code in src/momapy/builder.py
def isinstance_or_builder(
    obj: typing.Any, type_: typing.Type | tuple[typing.Type]
) -> bool:
    """Return `true` if the object is an istance of the given classes or of their registered builder classes, and `false` otherwise"""
    if isinstance(type_, type):
        type_ = (type_,)
    type_ += tuple([get_or_make_builder_cls(t) for t in type_])
    return isinstance(obj, type_)

issubclass_or_builder

issubclass_or_builder(cls: Type, type_: Type | tuple[Type]) -> bool

Return true if the class is a subclass of the given classes or of their registered builder classes, and false otherwise

Source code in src/momapy/builder.py
def issubclass_or_builder(
    cls: typing.Type, type_: typing.Type | tuple[typing.Type]
) -> bool:
    """Return `true` if the class is a subclass of the given classes or of their registered builder classes, and `false` otherwise"""
    if isinstance(type_, type):
        type_ = (type_,)
    type_ += tuple([get_or_make_builder_cls(t) for t in type_])
    return issubclass(cls, type_)

new_builder_object

new_builder_object(cls: Type, *args, **kwargs) -> Builder

Create and return a builder object from an object class or a builder class

Source code in src/momapy/builder.py
def new_builder_object(cls: typing.Type, *args, **kwargs) -> Builder:
    """Create and return a builder object from an object class or a builder class"""
    if not issubclass(cls, Builder):
        cls = get_or_make_builder_cls(cls)
    return cls(*args, **kwargs)

object_from_builder

object_from_builder(builder: Builder, inside_collections=True, builder_to_object: dict[int, Any] | None = None)

Create and return an object from a builder object

Source code in src/momapy/builder.py
def object_from_builder(
    builder: Builder,
    inside_collections=True,
    builder_to_object: dict[int, typing.Any] | None = None,
):
    """Create and return an object from a builder object"""
    if builder_to_object is not None:
        if id(builder) in builder_to_object:
            return builder_to_object[id(builder)]
    else:
        builder_to_object = {}
    if isinstance(builder, Builder):
        obj = builder.build(
            inside_collections=inside_collections,
            builder_to_object=builder_to_object,
        )
        builder_to_object[id(builder)] = obj
        return obj
    if inside_collections:
        if isinstance(builder, (list, tuple, set, frozenset)):
            return type(builder)(
                [
                    object_from_builder(
                        builder=e,
                        inside_collections=inside_collections,
                        builder_to_object=builder_to_object,
                    )
                    for e in builder
                ]
            )
        elif isinstance(builder, (dict, frozendict.frozendict)):
            return type(builder)(
                [
                    (
                        object_from_builder(
                            builder=k,
                            inside_collections=inside_collections,
                            builder_to_object=builder_to_object,
                        ),
                        object_from_builder(
                            builder=v,
                            inside_collections=inside_collections,
                            builder_to_object=builder_to_object,
                        ),
                    )
                    for k, v in builder.items()
                ]
            )
    return builder

register_builder_cls

register_builder_cls(builder_cls: Type) -> None

Register a builder class

Source code in src/momapy/builder.py
def register_builder_cls(builder_cls: typing.Type) -> None:
    """Register a builder class"""
    builders[builder_cls._cls_to_build] = builder_cls

super_or_builder

super_or_builder(type_: Type, obj: Any) -> Type

Return the super class for a given class or its builder class and an object

Source code in src/momapy/builder.py
def super_or_builder(type_: typing.Type, obj: typing.Any) -> typing.Type:
    """Return the super class for a given class or its builder class and an object"""
    try:
        s = super(type_, obj)
    except TypeError:
        builder = get_or_make_builder_cls(type_)
        s = super(builder, obj)
    finally:
        return s