logo       

Re: How to emove prefix from classes?: msg#00192

programming.swig

Subject: Re: How to emove prefix from classes?


On Thu, 30 Sep 2004, Robin Dunn wrote:

> Raghavendra Chandrashekara wrote:
> > Hi Everybody!
> >
> > I have a lot of classes which start with a certain prefix. For example,
> >
> > ftkClass1
> > ftkClass2
> > ...
> >
> > Is there a way of renaming the classes in the wrapper library (I am
> > using Python) so that they become:
> >
> > ftk.Class1
> > ftk.Class2
> > ...
> >
> > I think Robin Dunn has done this for wxPython and I would like to do
> > something similar.
>
> I have done it, but it wasn't easy. I first thought of modifying
> %rename to be able to take a regex or something and doing global renames
> that way, but I never found a way to implement that without major
> surgery on SWIG.
>
> So instead I run SWIG twice on each .i file. The first time it is to
> generate the XML of the parse tree. Then I run some Python code on the
> XML and generate a file full of %rename directives, one for each top
> level name that needs to have the wx removed. Finally, I run SWIG
> again, this time including the file with the %rename's. It's mostly all
> in wxPython's setup.py and config.py if you're interested in looking closer.

I think that you could also accomplish something similar by adding some
Python code that does some renaming for you, something like

%pythoncode %{

for foo in globals().values():
if type(foo) == type:
name = foo.__name__
if name.startswith('ftk'):
exec('%s = foo' % name[3:])
exec('del %s' % name)

%}

at the end of your .i file. The only downside I'm aware of is that the
classes will still print ftkClass1, etc., when you print an instance.

Also, if you go the other route an alternative to generating and parsing
XML is to load your module into Python after the first SWIG run and use a
loop similar to the above to generate your %rename directives.

Josh

--
Joshua L. Cherry, Ph.D.
NCBI/NLM/NIH (Contractor)
jcherry@xxxxxxxxxxxxxxxx

_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise