Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add advanced exclude and include support for dict, json and copy (#648)
* Add advanced exclude support for dict, json and copy * Add advanced exclude support for dict, json and copy Add new version section (v0.31) * Add advanced include support, add more tests, improve code style Rename ValueExclude to ValueItems and move it to utils Use old logic to calculate keys, but still exclude it in _iter * Add more tests for ValueItems * Removed update arg check in _calculate_keys for return None This will increase speed when no include or exclude given and skip_defaults is False * Fix formatting, remove duplicate imports * Add # pragma: no cover to 'if TYPE_CHECKING:' block * tweaks and coverage * fix history * Add docs * tweak docs
- Loading branch information
1 parent
bc60014
commit 74768c1
Showing
9 changed files
with
527 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from pydantic import BaseModel, SecretStr | ||
|
||
class User(BaseModel): | ||
id: int | ||
username: str | ||
password: SecretStr | ||
|
||
class Transaction(BaseModel): | ||
id: str | ||
user: User | ||
value: int | ||
|
||
transaction = Transaction( | ||
id="1234567890", | ||
user=User( | ||
id=42, | ||
username="JohnDoe", | ||
password="hashedpassword" | ||
), | ||
value=9876543210 | ||
) | ||
|
||
# using a set: | ||
print(transaction.dict(exclude={'user', 'value'})) | ||
#> {'id': '1234567890'} | ||
|
||
# using a dict: | ||
print(transaction.dict(exclude={'user': {'username', 'password'}, 'value': ...})) | ||
#> {'id': '1234567890', 'user': {'id': 42}} | ||
|
||
print(transaction.dict(include={'id': ..., 'user': {'id'}})) | ||
#> {'id': '1234567890', 'user': {'id': 42}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import datetime | ||
from typing import List | ||
|
||
from pydantic import BaseModel, SecretStr | ||
|
||
class Country(BaseModel): | ||
name: str | ||
phone_code: int | ||
|
||
class Address(BaseModel): | ||
post_code: int | ||
country: Country | ||
|
||
class CardDetails(BaseModel): | ||
number: SecretStr | ||
expires: datetime.date | ||
|
||
class Hobby(BaseModel): | ||
name: str | ||
info: str | ||
|
||
class User(BaseModel): | ||
first_name: str | ||
second_name: str | ||
address: Address | ||
card_details: CardDetails | ||
hobbies: List[Hobby] | ||
|
||
user = User( | ||
first_name='John', | ||
second_name='Doe', | ||
address=Address( | ||
post_code=123456, | ||
country=Country( | ||
name='USA', | ||
phone_code=1 | ||
) | ||
), | ||
card_details=CardDetails( | ||
number=4212934504460000, | ||
expires=datetime.date(2020, 5, 1) | ||
), | ||
hobbies=[ | ||
Hobby(name='Programming', info='Writing code and stuff'), | ||
Hobby(name='Gaming', info='Hell Yeah!!!') | ||
] | ||
|
||
) | ||
|
||
exclude_keys = { | ||
'second_name': ..., | ||
'address': {'post_code': ..., 'country': {'phone_code'}}, | ||
'card_details': ..., | ||
'hobbies': {-1: {'info'}}, # You can exclude values from tuples and lists by indexes | ||
} | ||
|
||
include_keys = { | ||
'first_name': ..., | ||
'address': {'country': {'name'}}, | ||
'hobbies': {0: ..., -1: {'name'}} | ||
} | ||
|
||
print( | ||
user.dict(include=include_keys) == user.dict(exclude=exclude_keys) == { | ||
'first_name': 'John', | ||
'address': {'country': {'name': 'USA'}}, | ||
'hobbies': [ | ||
{'name': 'Programming', 'info': 'Writing code and stuff'}, | ||
{'name': 'Gaming'} | ||
] | ||
} | ||
) | ||
# True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.