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許可發布。有關更多詳細信息,請參見許可證文件。
去上面