Take a look at this message thread. I think it would be interesting/nice if our constants and enums also returned a nice name when inspected. Thoughts? Roy -------- Original Message -------- Subject: Re: [Swig-user] wrapping enums for python Date: Wed, 13 Sep 2006 17:14:50 +0200 From: Nitro <nitro@dr-code.org> To: K M <intra611@gmail.com>, swig-user@lists.sourceforge.net References: <194b4da90609130632h66c62ed8o8035b4bc041fdb79@mail.gmail.com> Am 13.09.2006, 15:32 Uhr, schrieb K M <intra611@gmail.com>:> Hi, > > When printing enum wrapped to python I would like to get symbol instead > of > number. For example > > in C++: > > enum min_max > { > SS_MIN = 0, > SS_MAX = 100 > }; > > > I get: > > print swigtest.SS_MAX > 100 > > and I would like to get: > > print swigtest.SS_MAX > SS_MAX > > So the idea was to use something like: > > %pythoncode %{ > > class Enum(int): > def __new__(cls, arg, desc ): > self = int.__new__(cls, arg) > self.desc = desc > return self > def __repr__( self ): > return self.desc > > %} > > and then somehow in the .py file > > SS_MIN = Enum(_swigtest.SS_MIN, "SS_MIN") > instead of > SS_MIN = _swigtest.SS_MIN > > But the problem is that I do not know how to do > SS_MIN = Enum(_swigtest.SS_MIN, "SS_MIN") > for each define (would it be possible to use something like > %feature("pythonprepend")?) . > > Also there are C++ methods that return enums so it might be necessary to > create Enum python class in wrapper cxx file instead of py file. > (And then change SWIG_Python_SetConstant() method to create equivalent of > SS_MIN = Enum(_swigtest.SS_MIN, "SS_MIN") ). > > As I am new to SWIG (and Python as well) I wander if there is better and > simpler way how to do this. > > ThanksMaybe you can take a look here http://www.swig.org/Doc1.3/Typemaps.html#Typemaps_nn7 under point "Return value handling". You could then write a typemap which converts the returned enum to a Enum class. You could probably use %pythoncode to insert your python statement into the .py file. Just make sure you put it at the very end of your interface file. You probably even don''t have to specify any of the enums by hand, just use swig''s xml output and parse it to generate some code that applies your enum typemap to all enums in your code. How to do this xml/parse thing was several times on the mailing lists, search for xml and robin dunn probably. The last post was not more than a month ago. All in all I really like your idea, I thought about something similar some way back. You could extend your Enum class to cover operations such as or''ing together some elements (like const1 | const2). Personally I''d like to see this approach here as standard swig behaviour. What do you (other language maintainers for example) think? -Matthias ------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 _______________________________________________ Swig-user mailing list Swig-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/swig-user
Alex Fenton
2006-Sep-15 15:48 UTC
[Wxruby-users] [Fwd: Re: [Swig-user] wrapping enums for python]
Roy Sutton wrote:> Take a look at this message thread. I think it would be > interesting/nice if our constants and enums also returned a nice name > when inspected. >Yes. A string or symbol would be much more useful. The use of integer constant ids throughout wxRuby is a bane. e.g. When describing a menu item you have to mention the thing three times (declare constant; add menu item; hook event). I tried ameliorating this by using Ruby symbols for menu ids (more readable, literal and have a guaranteed unique conversion to integer), but the integers choked Wx... alex