Giter Site home page Giter Site logo

mapperpy's Introduction

MapperPy

Python object mapper.

Main features

  • Can map both ways
  • Automatically maps matching attributes
  • Allows to define custom mappings or override default ones
  • Allows to suppress automatic mapping for specific attributes
  • Allows to provide custom functions for attributes initialization
  • Supports nested mappers which are used automatically when attribute of specific type found

Usage

Installation

pip install MapperPy

Importing

from mapperpy import ObjectMapper

Initialization

Creating mapper from class:

mapper = ObjectMapper.from_class(ClassA, ClassB)

Note that automatic attribute mapping won't work if class(es) can't be instantiated with default no-arg constructor. Class instance is required to determine instance attributes' names.

To overcome this create mapper from prototype, i.e.:

mapper = ObjectMapper.from_prototype(ClassA("proto"), ClassB(None, None))

Prototypes are stored and then used during mapping to determine instance attributes' names.

Invoking mapper

Simply invoke:

instance_b = mapper.map(instance_a)

or:

instance_a = mapper.map(instance_b)

Mapper determines type of the instance automatically and maps it other type.

Mapper customization

Custom mappings (custom_mappings()):

mapper = mapper.custom_mappings({"some_property": "mapped_property", "my_property": "other_property"})

Suppress automatic mapping:

mapper = mapper.custom_mappings({"some_property": None})

Custom attribute initialization using function:

mapper = mapper.left_initializers({
            "some_property": lambda obj: obj.mapped_property + obj.mapped_property_02,
            "unmapped_property1": lambda obj: "prefix_{}".format(obj.unmapped_property2)})

Those functions will be used during initialization of ClassA (on the "left" side). Similarly right_initializers can be used to define initializers of ClassB (on the "right" side).

Custom value conversion using function:

mapper = mapper.value_converters({
            "some_property": (lambda val: json.dumps(val), lambda val: json.loads(val))})

Those functions will be used during attribute mapping to convert attribute value. First function is used when mapping from "left" to "right" and second function is used in the opposite direction. When setting value_converters it's enough to provide only "left" attribute name, "right" attribute name is derived automatically. This also means that value_converters should be used after custom_mappings so attributes' names can be derived.

Nested mappers

In case of more complex object structure you can use nested mappers.

Let's say you want to map an object which contains another object which you also want to map automatically. You can define nested mapper and then attach it to root mapper:

nested_mapper = ObjectMapper.from_class(NestedClassA, NestedClassB)

mapper = ObjectMapper.from_class(ClassA, ClassB).nested_mapper(nested_mapper)

Mapping attribute name

If you want to determine mapped name of an attribute you can invoke:

mapped_name = mapper.map_attr_name("some_property")

For example for mapper:

mapper = mapper.custom_mappings({"some_property": "mapped_property", "my_property": "other_property"})

result of:

print(mapper.map_attr_name("some_property"))

will be:

mapped_property

mapperpy's People

Contributors

cepe avatar lgrech avatar

Watchers

Tim Treptow avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.