473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

check if class really implements api?

Hello,
with the zope.interface module I can query if my class claims to
implement my API. This
is what the method implementedBy is for.

However, I find myself constantly changing the API and I am looking for
a more reliable way
to check my implementation classes. Is there a way to check if at least
all function
names of the API are defined by the class and if their parameter lists
fit the API? A way
to test this would save me a lot of time..

Kind regards,
Karsten.

Jan 3 '06 #1
3 2196
Karsten W. wrote:
However, I find myself constantly changing the API and I am looking for
a more reliable way
to check my implementation classes. Is there a way to check if at least
all function
names of the API are defined by the class and if their parameter lists
fit the API? A way
to test this would save me a lot of time..


To make it short: no, there is no reliable way to test this.

To make it longer: yes, you can, with several kludges (and limitations),
test this sort of thing. For example, start by running a dir() on the
interface definition class, and for each name you get returned check
whether it is a callable/function, then in case it is, have a look at the
corresponding class claiming to implement the interface, check if the name
exists, if it is a callable, and whether the method signature is
appopriately similar.

Most of the latter can be done using the introspection support present in
Python, which is nicely wrapped up for you in a module and whose
documentation you can find at:

http://www.python.org/doc/2.4.2/lib/module-inspect.html

Be aware that this does not check for methods which might be added at
runtime to a class, that you might have to create more elaborate tests to
check whether a class that might be callable has the correct method
signature, and several other quirks (such as a function in the interface
prototype taking five arguments, whereas in the class definition taking
only a single catch-all argument).

See how much of a can of worm this is? That's why you should rely on
duck-typing: if it quacks like a duck and walks like a duck, chances are
you really have a duck. And the only reliable way to test that is to let it
run.

--- Heiko.
Jan 4 '06 #2
On 3 Jan 2006 12:56:58 -0800
"Karsten W." <Do*******************@gmx.net> wrote:
with the zope.interface module I can query if my class
claims to implement my API. This
is what the method implementedBy is for.

However, I find myself constantly changing the API and I
am looking for a more reliable way
to check my implementation classes. Is there a way to
check if at least all function
names of the API are defined by the class and if their
parameter lists fit the API? A way
to test this would save me a lot of time..


You want:

Zope 2.5.1:
Interface.verify.verify_class_implementation

or

Zope 3.x:
zope.interface.verify

You'll want to read the source modules for documentation and
usage, as I don't think these are particularly well
documented anywhere else.

They are somewhat different in API between the two interface
implementations, but both do essentially the same thing --
they check the class against the interface object. This
will only tell you whether the methods are there and have
the same calling profile -- it obviously can't tell you
whether they do what you expect (or indeed do anything at
all).

But I had the same problem you're having, and I found them
useful.

Cheers,
Terry

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Jan 4 '06 #3

Terry Hancock schrieb:
You want:
[...] Zope 3.x:
zope.interface.verify


This is exactly what I was looking for! There is a function verifyClass
and there is
a TestCase for verify.py which teaches how to use it.

Thanks a lot!
Karsten.

Jan 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: trenchmouth | last post by:
Does anybody know how I can loop through all the properties in a class I have created? Can I use Me to refer to the properties? I'm using VB6. Many thanks.
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
5
by: randomblink | last post by:
Alright... Does anyone know if you can create a class that has a constructor? I would like to create a BUTTONMANAGER class that handles my buttons for me... I am doing this in Access for those...
5
by: Joe Rattz | last post by:
Ok, I am trying to figure out how to tell if a class (not object) implements a particular interface. Using the "is" operator does not seem to work for me and I assume its because I have a class,...
3
by: Frans Bouma | last post by:
Hi, I have a serious problem with VB.NET and a DataTable derived class and I can't figure out how to solve it. I have implemented it in C# where it works perfectly, but I can't port one...
2
by: Crirus | last post by:
I have some question related to the other post about iteration on a class members. I had found a way to figure out when a member of my class is array so I can pull out it's elements to iterate...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
2
by: Dinsdale | last post by:
We have created a object library that implements the INotifyPropertyChanged.PropertyChanged to bubble changes up to higher level classes. For instance, we have a person class that can have...
11
bilibytes
by: bilibytes | last post by:
Hello, I have a little question. I would like to know how to check if an object is compliant with an interface. I know that if the Class implements explicitly the Interface, like this:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.