remapper
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
remapworks 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.
Metadata
Release files for remapper 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| remapper-1.0.2.tar.gz | 5.6 kB | Without provenance | Details for remapper-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| remapper-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for remapper-1.0.2-py3-none-any.whl |
Total release size: 11.1 kB
Release files / remapper-1.0.2.tar.gz
| Download URL | remapper-1.0.2.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
31abb2794657df4e03a56c7a95b455d436c0158b97c2bc462f13e9725d343498
|
|
BLAKE2b-256 checksum How to use checksums |
496c985b35d69b5165dc79403f4d8ddbe9a58ffab1e5eb7f1b37de1c49cdcc55
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.25.2 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
|
Release files / remapper-1.0.2-py3-none-any.whl
| Download URL | remapper-1.0.2-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cba2e5e54a22da25e6951448ce43bd1d3138cc791d8da92d74dc084de6684ff5
|
|
BLAKE2b-256 checksum How to use checksums |
326a20c49a32f1b27780605dd760bcc96af01e15a90e494ecfe453c9110392fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.25.2 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
|
Security for remapper 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.