Interface Implementation in Python

P

p_shakil

Hi,

I would like to know the interface concept in Python.How the
Interface is defined and implemented in Python?.

How to access the interface fromn Client?

Thanks
PSB
 
M

MonkeeSage

Hi,

I would like to know the interface concept in Python.How the
Interface is defined and implemented in Python?.

How to access the interface fromn Client?

Thanks
PSB

Not sure exactly what you mean, but in python (like most dynamic
languages) an "interface" is simply a behavioral type system. Any
object can implement an interface, based on its signature. For
example; let's say you have a file object which implements read() and
write(). Any other object with a sufficiently similar signature (e.g.,
StringIO), can be said to implement the same interface. An interface
in python is simply a specified behavior, and any object which
implements that behavior can be said to have the same "interface".

HTH,
Jordan
 
D

Dennis Lee Bieber

Hi,

I would like to know the interface concept in Python.How the
Interface is defined and implemented in Python?.

How to access the interface fromn Client?
Insufficient data:

User Interface:
text console
Tkinter
wxPython
one-or-two-other-libraries

System Call Interface:
os module for posix compatible operations
ctypes for generic DLL (or Linux equivalent)
win32 modules for Windows only
Embedding and/or Extending Python reference book

Database Interface:
DB-API compatible modules for various DBMS

Web-based Services:
soapPy (or however it is spelled)
other-things

Client:
web-browser
????
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

Johannes Woolard

'Lo, I think I know what you're asking:
I would like to know the interface concept in Python.How the
Interface is defined and implemented in Python?.

I assume you're talking about the what Java calls an interface - and
the simple answer is that we don't have an equivalent in Python.
This is because in the Python system of classes, interface inheritance
would be unenforceable (because methods can be arbitrarily added at
runtime).
How to access the interface fromn Client?

I'm not completely sure what you mean here... client class, client of
some protocol? Assuming you mean the client class, my answer above
should suffice (and if not then I am completely missing the question).

Regards,

Johannes Woolard
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

Hi,

I would like to know the interface concept in Python.How the
Interface is defined and implemented in Python?.

How to access the interface fromn Client?

You have a class with methods and data. You write many unit tests for
that class which defines the behaviour of your interface. Make sure
your class passes all those tests. When you are done, not only does
your unit tests specify an interface, you also have a concrete class
that implements that interface.

Now replace the original class with another class. If that class also
passes all your tests, then you can conclude that it also implements
the same interface.
 
G

Goldfish

I would like to know the interface concept in Python.How the
One way I have implemented interfaces, is as follows:

class MyInterface(object):
def someMethod(self, argument):
raise NotImplementedError()

If anybody ever uses that class directly, or subclasses it without
implementing a real version of that method, a runtime error can be
expected. This bridges both interfaces and abstract classes.

As others have pointed out, this isn't quite like a Java interface,
but I have found it useful to state my intentions up front, and that
is how I do it. The only way to truly enforce this is by following up
with lots of good test cases, run often.
 
P

p_shakil

One way I have implemented interfaces, is as follows:

class MyInterface(object):
def someMethod(self, argument):
raise NotImplementedError()

If anybody ever uses that class directly, or subclasses it without
implementing a real version of that method, a runtime error can be
expected. This bridges both interfaces and abstract classes.

As others have pointed out, this isn't quite like a Java interface,
but I have found it useful to state my intentions up front, and that
is how I do it. The only way to truly enforce this is by following up
with lots of good test cases, run often.


I have a class by name Point3D in Python.

class Point3D :
def __int(self):
self.__x =0.0
self.__y =0.0
self.__z =0.0

def setPoint(x,y,z):
self.__x =x
self.__y =y
self.__z =z

def getPoint(x,y,z):
x = self.__x
y = self.__y
z = self.__z

I dont want to expose the above Point3D implementation to the user /
client side.To achieve that we can use interface concept.In Python to
use interface concept.

How the interface class looks like for Point3D in python?.

How to implement that Interface?

How to access the implemented interface thru Interface Object in the
client side?

It will be helpful if somebody proivide a piece of sample code for
creating the interface for the above access at the client place

in C++ these can be done in this way

class IPoint : public IUnknown{
virtual void setPoint(int x,int y,int z)=0;
virtual void getPoint(&x,&y,&z)=0;
}

class Point3D: public IPoint {
private :
float _x ,_y,_z;
public:
void Point3D() {
_x=_y=_z =0
}
void setPoint(int x,int y,int z){

_x =x
_y =y
_z =z


}
void getPoint(&x,&y,&z){
x = _x
y = _y
z = _z
}
}

Access the Point data in the client side

IPoint *pIPoint = NULL;
pISampleInterface = CoCreateInstance(..);
................
pISampleInterface ->QueryInstance(IID_IPoint ,(void*&)pIPoint )

// We have the pIPoint interafce
float dX =0,dY=0,dZ=0;

pIPoint->setPoint(10,20,30);

pIPoint->getPoint(&dX ,&dY,&dZ);

printf ( "%f,%f%f",dX ,dY,dZ);

10.0 20.0 30.0


Thanks
PSB

--------------------------------------------------------------------------------
 
M

MonkeeSage

I dont want to expose the above Point3D implementation to the user /
client side.To achieve that we can use interface concept.In Python to
use interface concept.

In python you would use name mangling to hide parts of the interface
from the public.

class Point3D:
def __init__(self):
self.__x = 0.0
self.__y = 0.0
self.__z = 0.0

def __setPoint(x,y,z):
self.__x = x
self.__y = y
self.__z = z

def __getPoint(x,y,z):
x = self.__x
y = self.__y
z = self.__z

p = Point3D()
p.__setPoint(1,2,3)

# ...
# AttributeError: Point3D instance has no attribute '__setPoint'

Regards,
Jordan
 
D

Dennis Lee Bieber

I have a class by name Point3D in Python.

class Point3D :
def __int(self):
self.__x =0.0
self.__y =0.0
self.__z =0.0

def setPoint(x,y,z):
self.__x =x
self.__y =y
self.__z =z

def getPoint(x,y,z):
x = self.__x
y = self.__y
z = self.__z
The above is not Python (I'm going to presume the news client ate
the indentation) -- in particular, your getPoint() is non functional; x,
y, z are input arguments to getPoint, and act as local variables within
the function.

class Point3D:
def __init__(self, x = 0.0, y = 0.0, z = 0.0):
self.setPoint(x, y, z)

def setPoint(x, y, z):
self._x = x
self._y = y
self._z = z

def getPoint():
return (self._x, self._y, self._z)

.... is functional Python
I dont want to expose the above Point3D implementation to the user /
client side.To achieve that we can use interface concept.In Python to
use interface concept.
At this point it still isn't clear what you consider a "user" or
"client".
How the interface class looks like for Point3D in python?.

How to implement that Interface?

How to access the implemented interface thru Interface Object in the
client side?
So far, all buzz words with no context...
It will be helpful if somebody proivide a piece of sample code for
creating the interface for the above access at the client place

said:
Access the Point data in the client side

IPoint *pIPoint = NULL;
pISampleInterface = CoCreateInstance(..);
...............
pISampleInterface ->QueryInstance(IID_IPoint ,(void*&)pIPoint )
NOW you have given CONTEXT...

CoCreateInstance is Windows specific for COM/OLE...

I suggest you obtain (if you don't already have it) the PyWin32 (aka
win32all) module package, and then study the documentation for the
pythoncom module within that package. Granted, the documentation is
somewhat sparse -- for example (I do hope this will pass for "fair use"
to illustrate what I mean by sparse).

-=-=-=-=-
pythoncom.WrapObject

PyIUnknown = WrapObject(ob, gatewayIID , interfaceIID )

Wraps a Python instance in a gateway object.

Parameters
ob : object

The object to wrap.

gatewayIID=IID_IDispatch : PyIID

The IID of the gateway object to create (ie, the interface of the server
object wrapped by the return value)

interfaceIID=IID_IDispatch : PyIID

The IID of the interface object to create (ie, the interface of the
returned object)

Return Value
Note that there are 2 objects created by this call - a gateway (server)
object, suitable for use by other external COM clients/hosts, as well as
the returned Python interface (client) object, which maps to the new
gateway.
There are some unusual cases where the 2 IID parameters will not be
identical. If you need to do this, you should know exactly what you are
doing, and why!

-=-=-=-=-=-=-

As for making things available to other programs via COM... That
probably requires creating and registering a COM server with the OS, so
other programs can find it. I have no experience with this level of COM
(give me an Amiga application with an ARexx port any day -- those were
relatively easy to code)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top