classmapper


Nameclassmapper JSON
Version 0.0.1 PyPI version JSON
download
home_pagehttps://github.com/David-Lor/python-classmapper
SummaryNone
upload_time2024-04-26 18:39:45
maintainerNone
docs_urlNone
authorDavid Lorenzo
requires_pythonNone
licenseISC
keywords classmapper objectmapper class-mapper object-mapper mapping mapper mapstructs
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python ClassMapper

A simple class mapping (conversion) Python library.

## Example

Imagine you have two classes, `OriginCls` and `DestinationCls`:

```python
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.

```python
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:

```python
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:

```python
class OtherCls:
    pass

mapper.map(source, OtherCls)  # raises NoClassMappingError
```

More examples can be found on [tests](tests).



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/David-Lor/python-classmapper",
    "name": "classmapper",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "classmapper, objectmapper, class-mapper, object-mapper, mapping, mapper, mapstructs",
    "author": "David Lorenzo",
    "author_email": "17401854+David-Lor@users.noreply.github.com",
    "download_url": "https://files.pythonhosted.org/packages/4f/df/dab992885c87981e73766233c6fc8159983dbd6d8b5e27fe701f2d885f01/classmapper-0.0.1.tar.gz",
    "platform": null,
    "description": "# Python ClassMapper\n\nA simple class mapping (conversion) Python library.\n\n## Example\n\nImagine you have two classes, `OriginCls` and `DestinationCls`:\n\n```python\nfrom dataclasses import dataclass\n\n@dataclass\nclass OriginCls:\n    id: str\n    name: str\n    age: int\n\n@dataclass\nclass DestinationCls:\n    _id: str\n    data: dict\n```\n\nYou want to be able to convert an object of `OriginCls` to `DestinationCls` and viceversa.\nClassMapper allows you to define \"mappers\" - functions that explicitly convert an object of a class A to a new instance of a class B.\nBeing explicit means that you have to explicitly define how the conversion between classes is performed.\n\n```python\nfrom classmapper import ClassMapper\n\nmapper = ClassMapper()\n\n@mapper.register(OriginCls, DestinationCls)\ndef _mapper_origin_destination(_from: OriginCls) -> DestinationCls:\n    return DestinationCls(\n        _id=_from.id,\n        data={\n            \"name\": _from.name,\n            \"age\": _from.age\n        }\n    )\n\n@mapper.register(DestinationCls, OriginCls)\ndef _mapper_destination_origin(_from: DestinationCls) -> OriginCls:\n    return OriginCls(\n        id=_from._id,\n        name=_from.data[\"name\"],\n        age=_from.data[\"age\"]\n    )\n```\n\nNow you can convert an object of `OriginCls` into `DestinationCls`, and viceversa:\n\n```python\nsource = OriginCls(id=\"1\", name=\"foo\", age=21)\n# mapper.map(source object, target class)\n#   returns object of target class, if a mapper between source object's class and target class is registered\nresult = mapper.map(source, DestinationCls)\nprint(result)\n\n# convert back into the original\nresult = mapper.map(result, OriginCls)\nprint(result)\n```\n\nIf you try to convert between classes not mapped, a NoClassMappingError is raised:\n\n```python\nclass OtherCls:\n    pass\n\nmapper.map(source, OtherCls)  # raises NoClassMappingError\n```\n\nMore examples can be found on [tests](tests).\n\n\n",
    "bugtrack_url": null,
    "license": "ISC",
    "summary": null,
    "version": "0.0.1",
    "project_urls": {
        "Download": "https://github.com/David-Lor/python-classmapper/archive/main.zip",
        "Homepage": "https://github.com/David-Lor/python-classmapper"
    },
    "split_keywords": [
        "classmapper",
        " objectmapper",
        " class-mapper",
        " object-mapper",
        " mapping",
        " mapper",
        " mapstructs"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4fdfdab992885c87981e73766233c6fc8159983dbd6d8b5e27fe701f2d885f01",
                "md5": "dc9d5b2a6f90dfcdcae8545863e71ac9",
                "sha256": "a2e015007d6ffffa205cf8e0633d793037c4caecab922dd9967f6e60cc9ab9a5"
            },
            "downloads": -1,
            "filename": "classmapper-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "dc9d5b2a6f90dfcdcae8545863e71ac9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3308,
            "upload_time": "2024-04-26T18:39:45",
            "upload_time_iso_8601": "2024-04-26T18:39:45.555464Z",
            "url": "https://files.pythonhosted.org/packages/4f/df/dab992885c87981e73766233c6fc8159983dbd6d8b5e27fe701f2d885f01/classmapper-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-26 18:39:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "David-Lor",
    "github_project": "python-classmapper",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "classmapper"
}
        
Elapsed time: 1.11611s