Pymertive : مجموعة أدوات Python الحازمة حسب العقد (DBC)
Pysertive هي مكتبة Python التي توفر ديكورات لتنفيذ مبادئ التصميم حسب العقد (DBC). إنه يبسط فرض الشروط المسبقة ، والشرافات البريدية ، والثاني في الكود الخاص بك. يهدف Pysertive إلى أن تكون أداة قوية لضمان سلوك وقيود الكود ، وتعزيز تطوير البرمجيات الآمنة والقابلة للصيانة والقوية في Python.
الأشياء التي تعمل بشكل جيد:
تم تصميم Pysertive مع مراعاة البساطة والمرونة ، مما يتيح لك دمج العقد الصارم بسهولة في رمز Python الخاص بك ، مما يساعد في تصحيح أنظمة معقدة والحفاظ عليها.
لتثبيت Pystertive ، ما عليك سوى استخدام 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 للحصول على أمثلة أكثر تفصيلاً حول كيفية استخدام pystertive ، تحقق من ملف examples.py بي في المستودع. يحتوي هذا الملف على أمثلة على كيفية استخدام الشروط المسبقة ، والشروط البريدية ، والثاني في رمز Python الخاص بك.
المساهمات مرحب بها! إذا كنت ترغب في المساهمة ، فيرجى مراجعة دليل المساهمة ، ولا تتردد في فتح مشكلة أو طلب سحب.
يتم إصدار Pystertive تحت رخصة معهد ماساتشوستس للتكنولوجيا. انظر ملف الترخيص لمزيد من التفاصيل.
اذهب إلى الأعلى