Quick and Simple Class-to-Dict Mapping for Python using decorators
Project description
QuickMap
Quick and Simple Class-to-Dict Mapping for Python using decorators
Welcome to QuickMap, your go-to library for fast and straightforward mapping between Python classes and dictionaries. QuickMap is designed to make the process of serializing and deserializing data between classes and dictionaries as seamless and efficient as possible.
With QuickMap, you can:
- Quickly Convert Classes to Dictionaries: Serialize your class instances into dictionaries effortlessly, capturing all attributes and their values.
- Easily Recreate Classes from Dictionaries: Deserialize dictionaries back into class instances with ease, ensuring your data maintains its structure and type integrity.
- Customize Your Mappings: Define custom conversion rules and transformations for specific use cases, giving you the flexibility to handle complex scenarios.
- Enhance Your Workflow: Save development time and reduce boilerplate code with QuickMap’s intuitive and easy-to-use API. Built with speed and simplicity in mind, QuickMap integrates seamlessly into any Python project. Whether you're an experienced developer or new to Python, QuickMap's comprehensive documentation and practical examples will help you get started quickly.
Installation
pip install quickmap
or from Github:
git clone https://github.com/roymanigley/quickmap.git
cd quickmap
pip install -r requirements.txt
python setup.py install
Usage
map from object
to dict
from quickmap import mapping
class Dummy:
name: str
# define the mapping
@mapping(target='name', source='dummy.name')
# define the source type using the type hint on the `do_mapping` function
# define the target type using the return type hint
def do_mapping(dummy: Dummy) -> dict:
pass
# call then mapping function
dummy = Dummy()
dummy.name = 'alpha'
dummy = do_mapping(dummy)
print(dummy['name'])
map from dict
to object
from quickmap import mapping
class Dummy:
name: str
# define the mapping
@mapping(target='name', source='dummy.name')
# define the source type using the type hint on the `do_mapping` function
# define the target type using the return type hint
def do_mapping(dummy: dict) -> Dummy:
pass
# call then mapping function
dummy = do_mapping({'name': 'alpha'})
print(dummy.name)
map from multiple arguments
from quickmap import mapping
class Dummy:
name_1: str
name_2: str
# define the mapping
@mapping(target='name_1', source='dummy_1.name')
@mapping(target='name_2', source='dummy_2.name')
# define the source type using the type hint on the `do_mapping` function
# define the target type using the return type hint
def do_mapping(dummy_1: dict, dummy_2: dict) -> Dummy:
pass
# call then mapping function
dummy = do_mapping(dummy_1={'name': 'alpha'}, dummy_2={'name': 'bravo'})
print(dummy.name_1, dummy.name_2)
Example
import unittest
from quickmap import mapping
class MapToComplexTestCase(unittest.TestCase):
def test_from_dict(self):
# GIVEN
source = {
'name': 'order_01',
'customer': {
'name': 'alpha corp',
'address': {
'street': 'Evergreen 22',
'zip': '0815',
'city': 'Springfield'
}
}
}
class Address:
street: str
zip: str
city: str
class Customer:
name: str
address: Address
class Order:
name: str
customer: Customer
customer_name_uppercase: str
@mapping(target='name', source='order.name')
@mapping(target='customer_name_uppercase', source='order.customer.name', function=str.upper)
@mapping(target='customer.name', source='order.customer.name')
@mapping(target='customer.address.street', source='order.customer.address.street')
@mapping(target='customer.address.zip', source='order.customer.address.zip')
@mapping(target='customer.address.city', source='order.customer.address.city')
def do_mapping(order: dict) -> Order:
pass
# WHEN
value: Order = do_mapping(order=source)
# THEN
self.assertEqual(type(value), Order)
self.assertEqual(value.name, 'order_01')
self.assertEqual(value.customer_name_uppercase, 'ALPHA CORP')
self.assertEqual(value.customer.name, 'alpha corp')
self.assertEqual(value.customer.address.street, 'Evergreen 22')
self.assertEqual(value.customer.address.zip, '0815')
self.assertEqual(value.customer.address.city, 'Springfield')
if __name__ == '__main__':
unittest.main()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
quickmap-1.0.1.tar.gz
(4.7 kB
view details)
Built Distribution
File details
Details for the file quickmap-1.0.1.tar.gz
.
File metadata
- Download URL: quickmap-1.0.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca96851f4c4765874a45ac5528bc9455d80e270fb3e625c5482445f881c3614d |
|
MD5 | 78a923904342d566dc6231354415c3a2 |
|
BLAKE2b-256 | 33dffceaf52c2d6e1264e4e7f29972920548ea9ed7cef372c09b4efea172d8f1 |
File details
Details for the file quickmap-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: quickmap-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7550a5311a620fe7e29ec808a35b05bd7dc091e4d6a4296395d92093c94c80c |
|
MD5 | 92d69eaf74c88cb4fb897cedbf738d57 |
|
BLAKE2b-256 | 7466ae2164964999f4c55278ecda5e69c9a6fee867bf997346590dac1bbc86d0 |