473,698 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2207
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.verif y.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*****@Anansi Spaceworks.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
11943
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
2830
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 use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
5
18107
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 who care. I can't seem to get class_initialize to have required arguments in the initializer. Of course I just assumed that the class_initialize was the constructor... Maybe not?
5
17006
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, not an object I am wanting to check. Here's my scenario. I am dynamically loading assemblies (dlls) at runtime. I am getting the list of types from each assembly, and if that type implements a particular interface, then and only then do I want...
3
8084
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 statement to VB.NET and this is crucial: re-implementing ISerializable.GetObjectData() in the DataTable derived class so serializing the datatable derived class will call this method and not the DataTable version (which is private, so overriding is also...
2
1275
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 on that classes members, recursively I designed a Strong type class, inherited from CollectionBase. My class have a member of that type, so it contain lots of references fo objects of that strong type. Is there a general way to figure out that a...
7
13682
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
7212
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 relationships with other persons. If there is a change in the relationship (i.e. status, type etc) then we want the PropertyChanged event to fire and notify the top level Person object of that change. The PropertyChanged event is implemented as follows:...
11
15631
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: Class RobertNestaMarley implements Smoker_Interface the operator instanceof will let me know. like this:
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.