Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14f055e
commit 0f85ff4
Showing
8 changed files
with
165 additions
and
3 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,36 @@ | ||
from functools import cached_property | ||
from random import randint | ||
|
||
from pydantic import BaseModel, field as computed_field | ||
|
||
|
||
class Square(BaseModel): | ||
side: float | ||
|
||
@computed_field(title='The area') | ||
def area(self) -> float: | ||
return self.side ** 2 | ||
|
||
@area.setter | ||
def area(self, area: float) -> None: | ||
self.side = area ** .5 | ||
|
||
@area.deleter | ||
def area(self) -> None: | ||
self.side = 0 | ||
|
||
@computed_field(alias="The random number") | ||
@cached_property | ||
def random_n(self) -> int: | ||
return randint(0, 1_000) | ||
|
||
|
||
sq = Square(side=10) | ||
the_random_n = sq.random_n | ||
assert sq.dict(by_alias=True) == {'side': 10, 'area': 100, 'The random number': the_random_n} | ||
sq.area = 49 | ||
assert sq.dict() == {'side': 7, 'area': 49, 'random_n': the_random_n} | ||
del sq.area | ||
assert sq.dict() == {'side': 0, 'area': 0, 'random_n': the_random_n} | ||
assert sq.dict(exclude_computed=True) == {'side': 0} | ||
assert sq.dict(exclude={'random_n'}) == {'side': 0, 'area': 0} |
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
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
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ isort==5.8.0 | |
mypy==0.812 | ||
pycodestyle==2.7.0 | ||
pyflakes==2.3.1 | ||
twine==3.4.1 | ||
twine==3.4.1 | ||
typingx==0.5.3 |
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