Author Topic: zope.interface  (Read 99 times)

Ahmed Youssef

  • Helping Freak
  • Administrator
  • Active Member
  • *****
  • Posts: 242
    • View Profile
    • WWW
    • Email
zope.interface
« on: February 02, 2010, 01:12:29 AM »
هذه الحزمة من Zope وهى احد اساسيات مكونات Zope 3 ولكنها ليست موجهة فقط ل Zope وممكن استخدامها فى اى مشروع اخر -على سبيل المثال twisted -

ال Interface بتوصف سلوك لكائن معين عن طريق
1- التوثيق
2- الصفات
3- الطرق

*هنستخدم كلمة implement بمعنى ينفذ بما انها عقد
اى صف سيقوم بعمل implement او تنفيذ للواجهة ملزم بتنفيذ بنودها من صفات وطرق

مثلا لدينا ICompiler
Code: [Select]
class ICompiler(Interface):
    """This is the main doc-string for the interface"""
   
    name=Attribute("Compiler's name")
    version=Attribute("The version")
   
    def compile(source):
        '''Compiling the source'''


وهى بتنص على ان اى كلاس هيستخدمها لازم يعرف الصفات name, version
ويعرف الطرق (compile)
*لاحظ عدم وجود self


لدينا صف بإسم CCompiler واخر بإسم CPPCompiler وواضح ان الإتنين ليهم انترفيس واحدة وهى ICompiler

Code: [Select]
class CCompiler(object):
    implements(ICompiler)
   
    def __init__(self):
        self.name="Freaky C Compiler"
        self.version="3.2"
       
    def compile(self, source): #
        print "Running 'Freaky C Compiler' on %s"%source
   


class CPPCompiler(object):
    implements(ICompiler)
   
    def __init__(self):
        self.name="Freaky++ Compiler"
        self.version="3.6"
   
    def compile(self, source):
        print "Running 'Freaky++' on %s"%source
   
الدالة implements بتاخد عدد متغير من ال interfaces فتقدر تضيفهم كلهم كمعاملات ليها
هتلاقيها فى ملف declarations.py
Code: [Select]
def implements(*interfaces):
....
....


هل انا نفذت بنود العقد كلها ؟
تقدر تتأكد من كدا بإستخدامك ل verifyObject او verifyClass لتختبر هل الكائن او الصف -على الترتيب-  قام بتعريف/تنفيذ كل مانص عليه العقد او لأ
Code: [Select]
    print verifyClass(ICompiler, CCompiler)
    print ICompiler.implementedBy(CCompiler) # is ICompiler implemented by "freakyc" ?
    freakyc=CCompiler()
    print verifyObject(ICompiler, freakyc)
    freakyc.compile('hello.c')


لاحظ للحصول على البنود المنفذة اللتى تم تحقيقها من العقد عن طريق كائن معين هو تمرير الكائن لمشيد الإنترفيس

Code: [Select]
    freakypp=CPPCompiler()
    compiler=ICompiler(freakypp)
    print compiler.name
    print compiler.version
    compiler.compile("hello.cpp")

هل انا نفذت بنود العقد كلها ؟
تقدر تتأكد من كدا بإستخدامك ل verifyObject او verifyClass لتختبر هل الكائن او الصف -على الترتيب-  قام بتعريف كل مانص عليه العقد او لأ والا هيترفع AttributeError
Code: [Select]
    print verifyClass(ICompiler, CCompiler)

    freakyc=CCompiler()
    print verifyObject(ICompiler, freakyc)
    freakyc.compile('hello.c')

Code: [Select]
print ICompiler.providedBy(freakyc) # is ICompiler provided by "freakyc" ?
نستخدم providedBy لإختبار هل يقوم الكائن freakyc بتقديم الإنترفيس ام لأ

Code: [Select]
print ICompiler.implementedBy(CCompiler) # is ICompiler implemented by "CCompiler" ?
هل قام الصف CCompiler بتنفيذ بنود العقد او تحقيق الإنترفيس ICompiler ام لا

فى دالتين بردو بنفس الإسم ولكن ستجدهم بإستدعائهم من zope.interface
Code: [Select]
    print list(implementedBy(CCompiler))
الحصول على كل الواجهات التى وعد الصف CCompiler بتحقيقها او تنفيذ بنودها

Code: [Select]
    print list(providedBy(freakyc))
نحصل على كل الواجهات اللتى يقدمها الكائن freakyc


السورس
Code: [Select]
from zope.interface import Interface, Attribute, implements, implementedBy, providedBy
from zope.interface.verify import verifyClass, verifyObject

class ICompiler(Interface):
    """This is the main doc-string for the interface"""
   
    name=Attribute("Compiler's name")
    version=Attribute("The version")
   
    def compile(source):
        '''Compiling the source'''


class CCompiler(object):
    implements(ICompiler)
   
    def __init__(self):
        self.name="Freaky C Compiler"
        self.version="3.2"
       
    def compile(self, source): #
        print "Running 'Freaky C Compiler' on %s"%source
   


class CPPCompiler(object):
    implements(ICompiler)
   
    def __init__(self):
        self.name="Freaky++ Compiler"
        self.version="3.6"
   
    def compile(self, source):
        print "Running 'Freaky++' on %s"%source
   

if __name__=="__main__":
    freakyc=CCompiler()
    compiler=ICompiler(freakyc)
    compiler.compile("hello.c")

    freakypp=CPPCompiler()
    compiler=ICompiler(freakypp)
    print compiler.name
    print compiler.version
    compiler.compile("hello.cpp")
   
    print list(implementedBy(CCompiler))
    print list(providedBy(freakyc))
    #print verifyClass(ICompiler, CCompiler)
    #print ICompiler.implementedBy(CCompiler) # is ICompiler implemented by "CCompiler" ?
    #freakyc=CCompiler()
    #print verifyObject(ICompiler, freakyc)
    #freakyc.compile('hello.c')

   
    #print ICompiler.providedBy(freakyc) # is ICompiler provided by "freakyc" ?

    #freakypp=CPPCompiler()
    #freakypp.compile('hello.cpp')
   
   

http://www.zope.org/Products/ZopeInterface
   

« Last Edit: February 02, 2010, 01:45:49 AM by Ahmed Youssef »
Logged

Life is just a chance to grow a soul. - A. Powell
Weblog: http://ahmedyoussef.wordpress.com/