pysertive
v0.0.1
Pysertive :契約による断定的なPythonデザイン(DBC)ツールキット
Pysertiveは、契約によるデザイン(DBC)の原則を実装するためのデコレーターを提供するPythonライブラリです。コードの前提条件、事後条件、および不変性の実施を簡素化します。 Pysertiveは、Pythonで安全で保守可能で堅牢なソフトウェア開発を促進し、コードの動作と制約を確保するための強力なツールになることを目指しています。
pysertiveがうまくいくこと:
Pysertiveは、シンプルさと柔軟性を念頭に置いて設計されており、複雑なシステムのデバッグと維持に役立つ、厳密な契約チェックをPythonコードに簡単に統合できます。
Pysertiveをインストールするには、PIPを使用するだけです。
pip install pysertivePysertiveをすばやく開始する方法は次のとおりです。
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ライセンスの下でリリースされます。詳細については、ライセンスファイルを参照してください。
トップに行きます