Skip to content

Commit f1a0dae

Browse files
committed
added doc
1 parent 55eda09 commit f1a0dae

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Table of Contents:
1313
- [About](#about)
1414
- [Contribute](#contribute)
1515
- [Usage](#usage)
16+
- [Installation](#installation)
17+
- [Basic](#basic)
18+
- [Map dictionary source to target object](#map-dictionary-source-to-target-object)
1619
- [Different field names](#different-field-names)
1720
- [Overwrite field value in mapping](#overwrite-field-value-in-mapping)
1821
- [Disable Deepcopy](#disable-deepcopy)
@@ -37,25 +40,27 @@ The major advantage of py-automapper is its extensibility, that allows it to map
3740
Read [CONTRIBUTING.md](/CONTRIBUTING.md) guide.
3841

3942
# Usage
43+
## Installation
4044
Install package:
4145
```bash
4246
pip install py-automapper
4347
```
4448

45-
Let's say we have domain model `UserInfo` and its API representation `PublicUserInfo` with `age` field missing:
49+
## Basic
50+
Let's say we have domain model `UserInfo` and its API representation `PublicUserInfo` without exposing user `age`:
4651
```python
4752
class UserInfo:
48-
def __init__(self, name: str, age: int, profession: str):
53+
def __init__(self, name: str, profession: str, age: int):
4954
self.name = name
50-
self.age = age
5155
self.profession = profession
56+
self.age = age
5257

5358
class PublicUserInfo:
5459
def __init__(self, name: str, profession: str):
5560
self.name = name
5661
self.profession = profession
5762

58-
user_info = UserInfo("John Malkovich", 35, "engineer")
63+
user_info = UserInfo("John Malkovich", "engineer", 35)
5964
```
6065
To create `PublicUserInfo` object:
6166
```python
@@ -77,6 +82,19 @@ print(vars(public_user_info))
7782
# {'name': 'John Malkovich', 'profession': 'engineer'}
7883
```
7984

85+
## Map dictionary source to target object
86+
If source object is dictionary:
87+
```python
88+
source = {
89+
"name": "John Carter",
90+
"profession": "hero"
91+
}
92+
public_info = mapper.to(PublicUserInfo).map(source)
93+
94+
print(vars(public_info))
95+
# {'name': 'John Carter', 'profession': 'hero'}
96+
```
97+
8098
## Different field names
8199
If your target class field name is different from source class.
82100
```python

tests/test_subscriptable_obj_mapping.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
from automapper import mapper
22

3+
34
class PublicUserInfo(object):
45
def __init__(self, name: str, profession: str):
56
self.name = name
67
self.profession = profession
78

89

910
def test_map__dict_to_object():
10-
original = {
11-
"name": "John Carter",
12-
"age": 35,
13-
"profession": "hero"
14-
}
15-
11+
original = {"name": "John Carter", "age": 35, "profession": "hero"}
12+
1613
public_info = mapper.to(PublicUserInfo).map(original)
1714

1815
assert hasattr(public_info, "name") and public_info.name == "John Carter"

0 commit comments

Comments
 (0)