Python ClassMapper
A simple class mapping (conversion) Python library.
Example
Imagine you have two classes, OriginCls and DestinationCls:
from dataclasses import dataclass
@dataclass
class OriginCls:
id: str
name: str
age: int
@dataclass
class DestinationCls:
_id: str
data: dict
You want to be able to convert an object of OriginCls to DestinationCls and viceversa.
ClassMapper allows you to define "mappers" - functions that explicitly convert an object of a class A to a new instance of a class B.
Being explicit means that you have to explicitly define how the conversion between classes is performed.
from classmapper import ClassMapper
mapper = ClassMapper()
@mapper.register(OriginCls, DestinationCls)
def _mapper_origin_destination(_from: OriginCls) -> DestinationCls:
return DestinationCls(
_id=_from.id,
data={
"name": _from.name,
"age": _from.age
}
)
@mapper.register(DestinationCls, OriginCls)
def _mapper_destination_origin(_from: DestinationCls) -> OriginCls:
return OriginCls(
id=_from._id,
name=_from.data["name"],
age=_from.data["age"]
)
Now you can convert an object of OriginCls into DestinationCls, and viceversa:
source = OriginCls(id="1", name="foo", age=21)
# mapper.map(source object, target class)
# returns object of target class, if a mapper between source object's class and target class is registered
result = mapper.map(source, DestinationCls)
print(result)
# convert back into the original
result = mapper.map(result, OriginCls)
print(result)
If you try to convert between classes not mapped, a NoClassMappingError is raised:
class OtherCls:
pass
mapper.map(source, OtherCls) # raises NoClassMappingError
More examples can be found on tests.
Metadata
Release files for classmapper 0.0.1
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 |
|---|---|---|---|---|
| classmapper-0.0.1.tar.gz | 3.3 kB | Without provenance | Details for classmapper-0.0.1.tar.gz |
Release files / classmapper-0.0.1.tar.gz
| Download URL | classmapper-0.0.1.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a2e015007d6ffffa205cf8e0633d793037c4caecab922dd9967f6e60cc9ab9a5
|
|
BLAKE2b-256 checksum How to use checksums |
4fdfdab992885c87981e73766233c6fc8159983dbd6d8b5e27fe701f2d885f01
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Security for classmapper 0.0.1
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.