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 operations 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.properties.operations import Operation
from data_mapper.properties.string import StringProperty

full_name = Operation(
    StringProperty('first_name'),
    StringProperty('middle_name', required=False),
    StringProperty('last_name'),
    func=lambda *args: ' '.join(filter(None, args)),
)

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',
))

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-1.1.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

data_mapper-1.1.0-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: data-mapper-1.1.0.tar.gz
  • Upload date:
  • Size: 11.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.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.7

File hashes

Hashes for data-mapper-1.1.0.tar.gz
Algorithm Hash digest
SHA256 77d351d688ffba4c25d27cbf783b416c6ecf8209d1a1baddb6c1303fc2c4a1cd
MD5 dcc956f154a2ffe279347cb1e88c6afc
BLAKE2b-256 d58b1df758ad15ed94c4685a30329486d16b1b40dabcaedcff69825620a7c6a1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: data_mapper-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.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.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.7

File hashes

Hashes for data_mapper-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d2b704bf4945ca49aea9757eb9945c36dfb3a98bb9186327bd713606cf0871e
MD5 e87344c7c58a86c2be2da0fde7d6077d
BLAKE2b-256 9f8aed7f2d6d83473d9293884803a720979603557f990e2e55d9c912f537c2d0

See more details on using hashes here.

Supported by

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