Skip to main content

A declarative data mapper

Project description

data-mapper

A declarative data mapper

PyPI Travis (.org) branch

Description

Most projects work with different representations of the same data. The code that is written every time to morph data between its representations is mostly very repetitive. More over, it is always a deposit of bugs and issues which requires a developer or tester to unit-test it.

This package is an attempt to solve these problems... well, at least the most common ones.

And to make a developer job easier it is primarily designed to be used in declarative fashion: describe what you want and get it right after.

Use Cases and Features

Here are examples of the most common use-cases and features:

Different Naming Schemes

This mapper looks for properties first_name and last_name in the data. For property first_name it tries to resolve it by the first key 'first_name', if not found it tries the second key 'name'. The similar process goes for property last_name.

from data_mapper.mappers import Mapper
from data_mapper.properties import Property

class PersonMapper(Mapper):
    first_name = Property('first_name', 'name')
    last_name = Property('last_name', 'surname')

mapper = PersonMapper()

assert mapper.get({
    'first_name': 'Ivan', 
    'surname': 'Bogush',
}) == {
    'first_name': 'Ivan', 
    'last_name': 'Bogush',
}

assert mapper.get({
    'name': 'Ivan', 
    'surname': 'Bogush',
}) == {
    'first_name': 'Ivan', 
    'last_name': 'Bogush',
}

This use-case has a story :)

It was the first issue I wanted to solve in my other project. I had different naming schemes in different data sources, and in my databases. All of them used different names for product categories: 'categories', 'category', 'categoryId'. I found it very boring to write repeatable code to convert the same data.

Arbitrary functions on resolved values

Full Name String Construction

This one resolves properties first_name, middle_name [optionally] and last_name and combines them into a single string — full_name.

from data_mapper.shortcuts import F, Str, L

full_name = F(
    ' '.join,
    L(
        Str('first_name'),
        Str('middle_name', required=False),
        Str('last_name'),
        skip_none=True,
    ),
)

assert 'Anton Pavlovich Chekhov' == full_name.get(dict(
    first_name='Anton',
    middle_name='Pavlovich',
    last_name='Chekhov',
))

assert 'Anton Chekhov' == full_name.get(dict(
    first_name='Anton',
    last_name='Chekhov',
))

Object mapping

Dict to Object

Let's assume we have a class Person:

class Person:
    def __init__(
            self,
            id_: int,
            first_name: str,
            last_name: str,
            middle_name: str = None,
    ):
        self.id = id_
        self.first_name = first_name
        self.last_name = last_name
        self.middle_name = middle_name

A mapper from dict with corresponding keys to an instance of class Person could be defined by subclassing ObjectMapper:

from data_mapper.mappers.object import ObjectMapper
from data_mapper.properties import (
    CompoundProperty, CompoundListProperty, IntegerProperty, StringProperty,
)


class PersonMapper(ObjectMapper):
    init = Person
    args = CompoundListProperty(
        IntegerProperty('id'),
        StringProperty('first_name'),
        StringProperty('last_name'),
    )
    kwargs = CompoundProperty(
        middle_name=StringProperty(required=False),
    )

first, middle, last = 'Iosif Aleksandrovich Brodsky'.split()
person = PersonMapper().get(dict(
    id=1940,
    first_name=first,
    middle_name=middle,
    last_name=last,
))

assert isinstance(person, Person)
assert person.id == 1940
assert person.first_name == first
assert person.middle_name == middle
assert person.last_name == last

Exactly the same can be done by instantiating the ObjectMapper:

from data_mapper.mappers.object import ObjectMapper
from data_mapper.properties import (
    CompoundProperty, CompoundListProperty, IntegerProperty, StringProperty,
)


mapper = ObjectMapper(
    init=Person,
    args=CompoundListProperty(
        IntegerProperty('id'),
        StringProperty('first_name'),
        StringProperty('last_name'),
    ),
    kwargs=CompoundProperty(
        middle_name=StringProperty(required=False),
    ),
)

first, middle, last = 'Iosif Aleksandrovich Brodsky'.split()
person = mapper.get(dict(
    id=1940,
    first_name=first,
    middle_name=middle,
    last_name=last,
))

assert isinstance(person, Person)
assert person.id == 1940
assert person.first_name == first
assert person.middle_name == middle
assert person.last_name == last

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

data-mapper-4.2.3.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

data_mapper-4.2.3-py3-none-any.whl (30.6 kB view details)

Uploaded Python 3

File details

Details for the file data-mapper-4.2.3.tar.gz.

File metadata

  • Download URL: data-mapper-4.2.3.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.7

File hashes

Hashes for data-mapper-4.2.3.tar.gz
Algorithm Hash digest
SHA256 b07d18297c20f5457bb792ce5af0c9097e39eb3ebe4c46f7a735e6856b9a90d4
MD5 39617266397be87a90a35cd2ce4269c6
BLAKE2b-256 182a47e17fddff73b3d7d8ebaae7da3c47020ff6d9b4218c519c084bbc2c493f

See more details on using hashes here.

File details

Details for the file data_mapper-4.2.3-py3-none-any.whl.

File metadata

  • Download URL: data_mapper-4.2.3-py3-none-any.whl
  • Upload date:
  • Size: 30.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.7

File hashes

Hashes for data_mapper-4.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d2b361120fd00439f7a4b24bf1662cd3b42edc4af7eb53aa177cb908b70f0a87
MD5 3decf184b7350aa394f620974d810818
BLAKE2b-256 441abe6646748b1ba0710163fedc574b6f0f6c1e49909046aaf5beabafee4c7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page