View class_invariant.py
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
@icontract.invariant( | |
lambda self: len(self) == 0 or len(self) == 1, | |
"There can be no or only single secret data stored", | |
) | |
class ReadOnce(metaclass=Final): | |
... |
View init_ensure.py
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
@icontract.ensure(lambda self: not self.__secrets and not self.__is_consumed) | |
def __init__(self) -> None: | |
self.__reset_secrets() | |
self.__reset_is_consumed() |
View validate_password.py
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
import re | |
def validate_password(password: str) -> bool: | |
reg = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{6,20}$" | |
pattern = re.compile(reg) | |
return bool(re.search(pattern, password)) |
View password_with_icontract.py
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
import icontract | |
from readonce import ReadOnce | |
def validate_password_length(password: str) -> bool: | |
return len(password) > 7 | |
class Password(ReadOnce): | |
@icontract.ensure(lambda self: len(self) == 1, "Secret is missing") | |
@icontract.require( | |
lambda password: validate_password_length(password), |
View pydantic_validation.py
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
from pydantic import BaseModel, validator | |
class InvalidDBCredentialsModel(BaseModel): | |
comment: str | |
password: Password | |
uri: DBUri | |
port: DBPort | |
host: DBHost | |
@validator("password") |
View can_not_serialize.py
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
>>> credentials.json() | |
... | |
TypeError: Object of type 'Password' is not JSON serializable |
View masked_dict.py
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
credentials.dict() | |
{'comment': 'The Hacked Database', 'password': ReadOnce[secrets=*****], 'uri': ReadOnce[secrets=*****], 'port': ReadOnce[secrets=*****], 'host': ReadOnce[secrets=*****]} |
View pydantic_model_creation.py
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
>>> credentials = DBCredentialsModel(comment="The Hacked Database", password=Password("db-password"), uri=DBUri("mysql://"), port=DBPort(3306), host=DBHost("localhost")) | |
>>> credentials | |
DBCredentialsModel(comment='The Hacked Database', password=ReadOnce[secrets=*****], uri=ReadOnce[secrets=*****], port=ReadOnce[secrets=*****], host=ReadOnce[secrets=*****]) |
View pydantic_model.py
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
from pydantic import BaseModel | |
class DBCredentialsModel(BaseModel): | |
comment: str | |
password: Password | |
uri: DBUri | |
port: DBPort | |
host: DBHost | |
class Config: |
View custom_encoder.py
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
import json | |
class CustomDBCredentialsEncoder(json.JSONEncoder): | |
def default(self, obj): | |
try: | |
# Intentionally omit other fields | |
return {"uri": obj.uri.get_secret()} | |
except AttributeError: | |
return super().default(obj) | |
NewerOlder