Skip to main content

Transform objects to and from similar structural mappings.

Project description

remapper

GitHub Workflow Status PyPI - Downloads GitHub

Transform objects to and from similar structural mappings. Useful for translating between sources of truth and presentational models of data.

Supports Python 3.8+. No dependencies.

Installation

$ pdm add remapper
# or
$ python -m pip install --user remapper

Usage

The examples below use dataclasses because they're easy, but remap works with any destination type that exposes attributes via its __init__.

A trivial example for remap is converting one dataclass into another without having to manually pass attributes:

from dataclasses import dataclass

from remapper import remap


@dataclass
class Source:
    a: int
    b: int
    c: int


@dataclass
class Destination:
    a: int
    b: int
    c: int


dest = remap(Source(a=1, b=2, c=3), Destination)
# >> Destination(a=1, b=2, c=3)

A more useful example would be in mapping an internal database model to an externally-facing dataclass:

from dataclasses import dataclass

from remapper import remap
from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, mapped_column


class Base(DeclarativeBase, MappedAsDataclass):
    pass


class Source(Base):
    id: Mapped[int] = mapped_column(init=False, primary_key=True)

    a: Mapped[int]
    b: Mapped[int]
    c: Mapped[int]


@dataclass
class Destination:
    a: int
    b: int
    c: int

    @property
    def d(self) -> int:
        return self.a + self.b + self.c


source = Source(a=1, b=2, c=3)
await session.commit(source)

dest = remap(source, Destination)
dest.d
# >> 6

Overrides

There's a non-zero chance that a destination may require more data than available on a source. In those cases, you can manually provide values via the overrides keyword argument. Values in overrides will take precedence over values on the source:

from dataclasses import dataclass

from remapper import remap


@dataclass
class Source:
    a: int
    b: int


@dataclass
class Destination:
    a: int
    b: int
    c: int


dest = remap(Source(a=1, b=2), Destination, overrides={"b": 0, "c": 3})
# >> Destination(a=1, b=0, c=3)

Defaults

If a destination defines more attributes than available on a source, but the destination has default values for those additional attributes, you don't need to supply overrides:

from dataclasses import dataclass

from remapper import remap


@dataclass
class Source:
    a: int
    b: int


@dataclass
class Destination:
    a: int
    b: int
    c: int = 3


dest = remap(Source(a=1, b=2), Destination)
# >> Destination(a=1, b=2, c=3)

Nested Types

Some complex sources may have attributes that themselves need to be converted. Rather than remapping in several steps, you can use the nested_types keyword argument to specify inner types of the destination attributes:

from dataclasses import dataclass

from remapper import remap


@dataclass
class Source:
    b: int


@dataclass
class Destination:
    b: int


@dataclass
class ParentSource:
    a: int
    child: Source


@dataclass
class ParentDestination:
    a: int
    child: Destination


## this works but is kind of clunky
source = ParentSource(a=1, child=Source(b=1))
dest_child = remap(source.child, Destination)
dest = remap(source, ParentDestination, overrides={"child": dest_child})
# >> ParentDestination(a=1,child=Destination(b=1))

## so use this instead
dest = remap(
    ParentSource(a=1, child=Source(b=1)),
    ParentDestination,
    nested_types={"child": Destination},
)
# >> ParentDestination(a=1,child=Destination(b=1))

License

This software is licensed under the BSD 3-Clause Clear License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

remapper-1.0.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

remapper-1.0.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file remapper-1.0.2.tar.gz.

File metadata

  • Download URL: remapper-1.0.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.25.2 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for remapper-1.0.2.tar.gz
Algorithm Hash digest
SHA256 31abb2794657df4e03a56c7a95b455d436c0158b97c2bc462f13e9725d343498
MD5 cfac6cd8368078ed890b572ed025f224
BLAKE2b-256 496c985b35d69b5165dc79403f4d8ddbe9a58ffab1e5eb7f1b37de1c49cdcc55

See more details on using hashes here.

File details

Details for the file remapper-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: remapper-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.25.2 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for remapper-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cba2e5e54a22da25e6951448ce43bd1d3138cc791d8da92d74dc084de6684ff5
MD5 172511c705f8994143bfa4059dd06f67
BLAKE2b-256 326a20c49a32f1b27780605dd760bcc96af01e15a90e494ecfe453c9110392fc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page