الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

zope.interface

بدأه ahmed_youssef في 2 فبراير 2010 · 4 رد · 881 مشاهدة · في لغة Python
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

هذه الحزمة من Zope وهى احد اساسيات مكونات Zope 3 ولكنها ليست موجهة فقط ل Zope وممكن استخدامها فى اى مشروع اخر -على سبيل المثال twisted -

ال Interface بتوصف سلوك لكائن معين عن طريق

1- التوثيق

2- الصفات

3- الطرق

*هنستخدم كلمة implement بمعنى ينفذ بما انها عقد

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

مثلا لدينا ICompiler

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

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

def implements(*interfaces):
	....
	....

هل انا نفذت بنود العقد كلها ؟

تقدر تتأكد من كدا بإستخدامك ل verifyObject او verifyClass لتختبر هل الكائن او الصف -على الترتيب- قام بتعريف/تنفيذ كل مانص عليه العقد او لأ

    print verifyClass(ICompiler, CCompiler)
    print ICompiler.implementedBy(CCompiler) # is ICompiler implemented by "freakyc" ?
    freakyc=CCompiler()
    print verifyObject(ICompiler, freakyc)
    freakyc.compile('hello.c')

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

    freakypp=CPPCompiler()
    compiler=ICompiler(freakypp)
    print compiler.name
    print compiler.version
    compiler.compile("hello.cpp")

هل انا نفذت بنود العقد كلها ؟

تقدر تتأكد من كدا بإستخدامك ل verifyObject او verifyClass لتختبر هل الكائن او الصف -على الترتيب- قام بتعريف كل مانص عليه العقد او لأ والا هيترفع AttributeError

    print verifyClass(ICompiler, CCompiler)

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

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

نستخدم providedBy لإختبار هل يقوم الكائن freakyc بتقديم الإنترفيس ام لأ

print ICompiler.implementedBy(CCompiler) # is ICompiler implemented by "CCompiler" ?

هل قام الصف CCompiler بتنفيذ بنود العقد او تحقيق الإنترفيس ICompiler ام لا

فى دالتين بردو بنفس الإسم ولكن ستجدهم بإستدعائهم من zope.interface

    print list(implementedBy(CCompiler))

الحصول على كل الواجهات التى وعد الصف CCompiler بتحقيقها او تنفيذ بنودها

    print list(providedBy(freakyc))

نحصل على كل الواجهات اللتى يقدمها الكائن freakyc

السورس

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

تم تعديل هذه المشاركة بواسطة ahmed_youssef في 2 فبراير 2010 في 10:46

1
(map share people)

فضلا لاتقم بمراسلتي من أجل أسئلة لها أقسامها في المنتدى حتى تعم الفائدة على الجميع وللحصول على إجابات أفضل من أعضاء أكثر خبرة.
Weblog
@bitbucket
@xmonader

#2

موضوع متميز يا احمد

قرأت من قبل حول Zope و و تذكرت وقتها ضخامة و تعقيد Web Bealogic

Technical Lead Developer

My LinkedIn Profile

اللهم قنى شر الجهل و الجهلاء

( اقْتَرَبَ لِلنَّاسِ حِسَابُهُمْ وَهُمْ فِي غَفْلَةٍ مَّعْرِضُونَ ) {الأنبياء:1}

#3

الله يخليك ياطارق

هيكليتها معقدة فعلا مع ادعاء ان Zope 3 انها modular لكن كم ال concepts المرتبطة مع zope كتير --يمكن grok تكون افيد

عن نفسى Django هى اختيارى الأول --مع بعض مشاكلها :)

تم تعديل هذه المشاركة بواسطة ahmed_youssef في 2 فبراير 2010 في 12:26

(map share people)

فضلا لاتقم بمراسلتي من أجل أسئلة لها أقسامها في المنتدى حتى تعم الفائدة على الجميع وللحصول على إجابات أفضل من أعضاء أكثر خبرة.
Weblog
@bitbucket
@xmonader

#4

معذرة يا احمد و لكنما هو الاستخدام العملى الذى يمكن تحقيقه باستخدام هذه ال Interface هل هى Interface Contract لبناء Add-on ل zope

Technical Lead Developer

My LinkedIn Profile

اللهم قنى شر الجهل و الجهلاء

( اقْتَرَبَ لِلنَّاسِ حِسَابُهُمْ وَهُمْ فِي غَفْلَةٍ مَّعْرِضُونَ ) {الأنبياء:1}

#5

بايثون مش بتدعم مفهوم ال interface بسبب دعم ال multiple inheritance .. ودى كانت حل وسط بسبب رفض بايثون دعمها

http://zope3.xmu.me/interfaces.html#introduction

مشابهة جدا لاستخدام الإنترفيس فى ال .net وبالتأكيد تقدر تستخدمها بصورة مستقلة وبعيدة تماما عن zope

(map share people)

فضلا لاتقم بمراسلتي من أجل أسئلة لها أقسامها في المنتدى حتى تعم الفائدة على الجميع وللحصول على إجابات أفضل من أعضاء أكثر خبرة.
Weblog
@bitbucket
@xmonader

مواضيع مشابهة