Builder - momapy

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`

    19
    20
    21
    22
    23
    24
    25
    26
    @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`

    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    @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`

    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    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`

    353
    354
    355
    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`

    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    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`

    348
    349
    350
    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`

    363
    364
    365
    366
    367
    368
    369
    370
    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`

    373
    374
    375
    376
    377
    378
    379
    380
    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`

    320
    321
    322
    323
    324
    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`

    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    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`

    358
    359
    360
    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`

    383
    384
    385
    386
    387
    388
    389
    390
    391
    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