From 8b1e1c104bdff504b3e775b450432e6462b8d09b Mon Sep 17 00:00:00 2001 From: root Date: Wed, 26 Apr 2006 10:48:09 +0000 Subject: forget the past. forget the past. --- interface.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 interface.py (limited to 'interface.py') diff --git a/interface.py b/interface.py new file mode 100644 index 0000000..f4800f0 --- /dev/null +++ b/interface.py @@ -0,0 +1,47 @@ +# Copyright (c) 2000-2004 LOGILAB S.A. (Paris, FRANCE). +# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., +# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +""" + bases class for interfaces + + TODO: + _ implements a check method which check that an object implements the + interface + _ Attribute objects + + This module requires at least python 2.2 +""" + +__revision__ = "$Id: interface.py,v 1.6 2004-05-10 14:40:50 syt Exp $" + +from types import ListType, TupleType + +class Interface: + """base class for interfaces""" + def is_implemented_by(cls, instance): + return implements(instance, cls) + is_implemented_by = classmethod(is_implemented_by) + +def implements(instance, interface): + """return true if the instance implements the interface + """ + if hasattr(instance, "__implements__") and \ + (interface is instance.__implements__ or + (type(instance.__implements__) in (ListType, TupleType) and \ + interface in instance.__implements__)): + return 1 + return 0 + + -- cgit v1.2.1