c++ - Python Properties & Swig -
I am trying to create Python binding for some C ++ code using swig. I seem to have a problem in trying to create Python properties from some accelerator functions:
class player {public: zero entity (existence * unit); Unit * unit () console; }; I tried to create a property using the Dragon Property function but it seems that the wrapper classes that generate swig are not compatible for at least the setters. How do you make properties using Swig?
Oh, this is hard (and fun) SWIG does not it @property identifies as an opportunity to generate: I think it will be very easy and recognize many false positive if it has not been carefully done though, since SWIG does not create it in C ++ , It is still quite possible that it is in Python using a small metaclaces. So, below, suppose we have a math class that we set and get an integer variable called "Pi" then we can use this code:
Example.h #ifndef EXAMPLE_H # defined EXAMPLE_H class math {public: int pi () const {it-> _pi return; } Zero pie (int pe) {it-> _pi = pi; } Private: int _pi; }; #endif
example.i
% module example% {#define SWIG_FILE_WITH_INIT #include "example.h"%} [essentially example.h Duplicated]
example.cpp
#include "example.h"
use.py
class property Voodoo ("Type"): "" "MetaCLASS starts when * class * is initialized, not an object, so we try to trap around class methods and particular details Are free. " "" Def __init __ (cls, * a): # OK, so the list of C ++ properties is stored on # __properties__ magic variables in the class using O # described style in O. To take advantage of the Cls .__ property: # get accessor Def fget (self): Get a SWIG class using #Super is to use super # because only the information that we are discontinuing is the # class object Self (CLS) is not the most robust way to do this work but works when SWIG # class is the only superclass S = super (CLS, Self) # Now get the C ++ method and call its operator DO (). Return Getetr (s, prop) () # Set accelerator def defaced (self, value): Call your overloaded operator (int value) to set it to # S similar to S = Super (CLS, Self). Return getattr (s, prop) (value) # The properties in python are the descriptors, which in turn have static variables on the # orbit. Therefore, here we create # variable variables and set it in property Setattr (CLS, Prop, Property (fget = fget, fset = fset) # Type () requires extra arguments that we have to do # Inheritance Did not use. (Parental classes are passed as logic in the form of the # part of the MetaCLAS protocol.) Usually a = [& lt; Some swing # classes & gt;] just super (property voodoo, cls) .__ init __ (* a) # another piece of work: SWIG selfishly overrides # __setattr__. Common Python class objects use .__ setattr__, # so that we use it here. It is not really necessary that we use the # __setsetr__ till we leave the SWIG class in the # inheritance series, because SWIG will leave the property created by __setattr__ #. Def __setattr __ (self, name, value): # Do this for properties listed only if the name is in cls .__ properties: object .__ setattr __ (self, name, value) Other: Like the above. S = super (cls, self) s .__ setattr__ (name, value) # Note that __setattr__ is considered as an example method, # so own. Just assign it to the class attribute # will ensure that this is an example method; It is, * * * No * will be magically turned on in a static / classpath. CLS .__ setattr__ = __setattr__
some fileAU
import import Example Import PropertyVoodoo class Math (example.Math): __properties__ = ['pi'] __metaclass__ = PropertyVoodoo m = Math () Print MPI MPI = 1024 print M.P.I. M.P.I. = 10000 print mpg
So the end result is that you have to create a wrapper class for every SWIG Python class and then type two lines: marking one For the methods which should be converted into properties and one to bring Metaclos.
Comments
Post a Comment