Skip to main content

mapperr - mapping across dictionary and class object, recursively

If you are using python for implementing protocols, cache management, nosql or sql database manupilation with the object oriented concepts in your code, mapperr can handle your object in your way, easily.

Installation

via pip

pip3 install mapperr

via direct setup

pip3 install setuptools
python3 setup.py sdist bdist_wheel
pip3 install dist/mapperr-0.0.1-py3-none-any.whl

Usage

Your classes' attributes are needed to be annotated with their types like int, str, Book, List[Book]. Parameterized constructors are not suitable, you can use it with plain objects which has most trash work. You can also fill your required options with param op_required as string list.

to_obj( dict_data: dict, destination_class: Type ) -> object

to_dict( obj: object ) -> dict

from typing import List
from pprint import pprint
from mapperr import to_dict, to_obj

class Book:
    _id: int
    title: str
    op_required: list = ['_id', 'title']

class BookShelf:
    code: str
    books: List[Book]

class Library:
    name: str
    book_shelfs: List[BookShelf]


def retrieve_library_from_the_source() -> dict:
    return {
        "name" : "Hogwarts Library",
        "book_shelfs" : [
            {
                "code" : "A1",
                "books" : [
                    {
                    "_id" : 0,
                    "title" : "Defence Against the Dark Arts"
                    },
                    {
                    "_id" : 1,
                    "title" : "Potions"
                    },
                ]
            },
            {
                "code" : "A2",
                "books" : [
                    {
                    "_id" : 3,
                    "title" : "Charms"
                    },
                    {
                    "_id" : 4,
                    "title" : "Herbology"
                    },
                ]
            }
        ]
    }

def send_library_to_the_source(data: dict):
    pprint(data)


lib: Library = to_obj(retrieve_library_from_the_source(), Library)

new_book = Book()
new_book._id = 5
new_book.title = "Alchemy"

lib.book_shelfs[0].books.append(new_book)

send_library_to_the_source( to_dict(lib) )

You can add your recursive type definitions into your class by using string variables.

class Person:
    name: str
    identity: int
    mother: 'Person'
    father: 'Person'
    friends: List['Person']

Metadata

Release files for mapperr 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mapperr 0.2.1
File Size Uploaded
mapperr-0.2.1.tar.gz 5.5 kB Details

Release files / mapperr-0.2.1.tar.gz

Download URL mapperr-0.2.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b9a5a7a742e58ece40f9f361ff8adcf3dc93a7c9df99f1fa7a90571b235610bf
BLAKE2b-256 checksum
How to use checksums
fd96e3aab7ee288d06c8b4410ef29c800f1fbddd9a075b200e15c236b194be74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release history Release notifications | RSS feed

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

This release

0.2.1 This release

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page