pysertive
v0.0.1
Pysertive :合同(DBC)工具包的自信Python设计
Pysertive是一个Python图书馆,为通过合同(DBC)原则实施设计的装饰工。它简化了代码中执行前提,后条件和不变性。 Pysertive旨在成为确保代码行为和约束的强大工具,从而在Python中促进安全,可维护和强大的软件开发。
Pysertive做得很好的事情:
Pysertive的设计是简单性和灵活性,使您可以轻松地将严格的合同检查集成到Python代码中,这有助于调试和维护复杂的系统。
要安装Pysertive,只需使用PIP:
pip install pysertive这是快速开始使用Pysertive的方法:
from pysertive import pre_condition , post_condition , invariant
@ pre_condition ( lambda x : x > 0 , exception_type = ValueError , message = "Input must be positive" )
def sqrt ( x ):
return x ** 0.5
@ post_condition ( lambda result : result != None , exception_type = AssertionError , message = "Result cannot be None" )
def fetch_data ():
return { "data" : "Here is your data" }
@ invariant ( lambda self : self . balance >= 0 , exception_type = RuntimeError , message = "Insufficient funds" )
class BankAccount :
def __init__ ( self , balance ):
self . balance = balance
def deposit ( self , amount ):
self . balance += amount
def withdraw ( self , amount ):
self . balance -= amount # No need to manually check for negative balance 确保您的功能输入有效:
@ pre_condition ( lambda age : age >= 18 , exception_type = ValueError , message = "Must be 18 or older" )
def sign_contract ( age ):
print ( f"Contract signed by individual aged { age } " )执行功能后验证输出:
@ post_condition ( lambda result : result > 0 , exception_type = AssertionError , message = "Profit must be positive" )
def calculate_profit ( revenue , costs ):
return revenue - costs执行类国家保持一致:
@ invariant ( lambda self : self . inventory_count >= 0 , exception_type = RuntimeError , message = "Inventory count cannot be negative" )
class Warehouse :
def __init__ ( self , inventory_count ):
self . inventory_count = inventory_count
def add_stock ( self , number ):
self . inventory_count += number
def remove_stock ( self , number ):
self . inventory_count -= number 有关如何使用Pysertive的更多详细示例,请查看存储库中的examples.py 。该文件包含如何在Python代码中使用前提条件,后条件和不变性的示例。
欢迎捐款!如果您想做出贡献,请查看贡献指南,并随意打开问题或拉动请求。
Pysertive根据MIT许可发布。有关更多详细信息,请参见许可证文件。
去上面