Transform objects to and from similar structural mappings.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file remapper-1.0.1.tar.gz.
File metadata
- Download URL: remapper-1.0.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.24.1 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
954bf6fa3f4fe3515c3069976c489af53a584e2a6f12fc55df33c139014c1dda
|
|
| MD5 |
5c23f99d55b3add773759f7419265eb6
|
|
| BLAKE2b-256 |
7b578da8dcf65f4513b34df6c4f7d879087536c469bb3abc4e7a29b2513b6476
|
File details
Details for the file remapper-1.0.1-py3-none-any.whl.
File metadata
- Download URL: remapper-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.24.1 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c338934db9956ab8fd4bc4438223f84e5abc99f812a7747a4a38ad40a6aa3c4
|
|
| MD5 |
1b1f94b584e83e772122fa634ff3ea25
|
|
| BLAKE2b-256 |
f26dd46807050cbf9ca945e356f0dabd8611ccb7ccf4d5ca0297538c840490b8
|