Skip to content
ObjectMapper is a class for automatic object mapping in Python
Python Shell Batchfile
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
mapper merge recent changes from upstream branch Jul 14, 2019
tests merge recent changes from upstream branch Jul 14, 2019
.gitignore python 2.7 compatibility fixes Jun 14, 2019
.travis.yml force python 3.7.3 for travis ci Jul 15, 2019
CHANGES.py
CHANGES.txt TRIVIAL: Update of the CHANGES.txt (notes about v1.1.0). Aug 10, 2019
LICENSE Initial commit Feb 14, 2015
LICENSE.txt v1.0.5 Feb 21, 2018
MANIFEST.in
README.md some more minor doc layouut changes Jul 14, 2019
README.rst some more minor doc layouut changes Jul 14, 2019
create_package.bat Added scripts for publishing on Pypi Feb 15, 2015
create_package.sh Update of the Pypi upload scripts Nov 3, 2017
logo.png
myconfig.cfg Travis-ci support. Jun 21, 2019
object-mapper.iml Added first version Feb 14, 2015
pypi_publish.bat Update of the Pypi upload scripts Nov 3, 2017
pypi_publish.sh Trivial changes (Release notes, gitignore) Nov 3, 2017
requirements.txt Travis-ci support. Jun 21, 2019
run.py added unit test to verify partial attribute mapping May 21, 2015
run_tests.bat Travis-ci support. Jun 21, 2019
run_tests.sh Travis-ci support. Jun 21, 2019
setup.cfg Update of the Pypi upload scripts Nov 3, 2017
setup.py support for nested map Jun 10, 2019

README.md

Object Mapper

Version 1.1.0

Author marazt

Copyright marazt

License The MIT License (MIT)

Last updated 13 July 2019

Package Download https://pypi.python.org/pypi/object-mapper

Build Status Build Status


Versions

1.1.0 - 2019/07/13

1.0.7 - 2019/06/19

  • Fix type name inside mapper dict to avoid collision, thanks @renanvieira

1.0.6 - 2018/10/28

  • Added ability to specify excluded fields, thanks @uralov

1.0.5 - 2018/02/21

  • Support for dynamic properties @nijm

1.0.4 - 2017/11/03

  • Migration to new Pypi.org deployment

1.0.3 - 2015/05/15

1.0.2 - 2015/05/06

1.0.1 - 2015/02/19

  • Fix of the package information

1.0.0 - 2015/02/19

  • Initial version

About

ObjectMapper is a class for automatic object mapping inspired by .NET AutoMapper. It helps you to create objects between project layers (data layer, service layer, view) in a simple, transparent way.

Example

  1. Mapping of the properties without mapping definition

In this case are mapped only these properties of the target class which are in target and source classes. Other properties are not mapped. Suppose we have class A with attributes name and last_name and class B with attribute name. Initialization of the ObjectMapper will be:

mapper = ObjectMapper()
mapper.create_map(A, B)
instance_b = mapper.map(A(), B)

In this case, value of A.name will be copied into B.name.

  1. Mapping with defined mapping functions

Suppose we have class A with attributes first_name and last_name , class B with attribute full_name and class C with attribute reverse_name. And want to map it in a way B.full_name = A.first_name + A.last_name and C.reverse_name = A.last_name + A.first_name Initialization of the ObjectMapper will be:

mapper = ObjectMapper()
mapper.create_map(A, B, {'name': lambda a : a.first_name + " " + a.last_name})
mapper.create_map(A, C, {'name': lambda a : a.last_name + " " + a.first_name})

instance_b = mapper.map(A(), B)
instance_c = mapper.map(A(), C)

In this case, to the B.name will be mapped A.first_name + " " + A.last_name In this case, to the C.name will be mapped A.last_name + " " + A.first_name

  1. Mapping suppression

For some purposes, it can be needed to suppress some mapping. Suppose we have class A with attributes name and last_name and class B with attributes name and last_name. And we want to map only the A.name into B.name, but not A.last_name to B.last_name Initialization of the ObjectMapper will be:

mapper = ObjectMapper()
mapper.create_map(A, B, {'last_name': None})

instance_b = mapper.map(A(), B)

In this case, value of A.name will be copied into B.name automatically by the attribute name name. Attribute A.last_name will be not mapped thanks the suppression (lambda function is None).

  1. Case insensitive mapping

Suppose we have class A with attributes Name and Age and class B with attributes name and age and we want to map A to B in a way B.name = A.Name and B.age = A.Age Initialization of the ObjectMapper will be:

mapper = ObjectMapper()
mapper.create_map(A, B)
instance_b = mapper.map(A(), B, ignore_case=True)

In this case, the value of A.Name will be copied into B.name and the value of A.Age will be copied into B.age.

Note: You can find more examples in tests package

Installation

ENJOY IT!

You can’t perform that action at this time.