Python SWIG std::map error?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Python SWIG std::map error?

gardnerpomper
Hi,

I have a need for a C++ std::map<std::string,std::string> in my python program, so I download swig. I tried the following as an example to use std::map, but I get errors when I try to compile the generated .cxx file. Here is my example:

/* File : example.i */
%module example

%include "std_string.i"
%include "std_map.i"

/* instantiate the required template specializations */
%template(map_si)  std::map<std::string, std::string>;

I have tried the std_vector example in the python examples, and that worked. The command I run this with is:

swig -c++ -python example.i
make

Here is the beginning of the output I get from that:

make -f ../../Makefile CXXSRCS='' SWIG='../../../preinst-swig' \
SWIGOPT='' TARGET='example' INTERFACE='example.i' python_cpp
make[1]: Entering directory `/home/gardner/downloads/swig-1.3.27/Examples/python/std_map'
../../../preinst-swig -c++ -python  example.i
g++ -c -fpic  example_wrap.cxx    -DHAVE_CONFIG_H -I/usr/local/include/python2.4 -I/usr/local/lib/python2.4/config
example_wrap.cxx: In function `const char* swig::type_name() [with Type =
   std::string]':
example_wrap.cxx:1596:   instantiated from `static swig_type_info* swig::traits_info<Type>::type_info() [with Type = std::string]'

I am running swig 1.3.27 on Quantian Linux (debian base, kernel 2.4.27) using gcc version 3.3.4.

If anyone has an example of using an STL map from python, I would be very grateful!

Thanks,

- Gardner

Reply | Threaded
Open this post in threaded view
|

Re: Python SWIG std::map error?

Marcelo Matus
As the std_map.i file says, std::map requires to instantiate std::pair.
 
So you need to do:

%module example

%include "std_string.i"
%include "std_map.i"

/* instantiate the required template specializations */
%template() std::pair<std::string, std::string>;
%template(map_si)  std::map<std::string, std::string>;


Marcelo


Gardner Pomper wrote:

> Hi,
>
> I have a need for a C++ std::map<std::string,std::string> in my python
> program, so I download swig. I tried the following as an example to
> use std::map, but I get errors when I try to compile the generated
> .cxx file. Here is my example:
>
> /* File : example.i */
> %module example
>
> %include "std_string.i"
> %include "std_map.i"
>
> /* instantiate the required template specializations */
> %template(map_si)  std::map<std::string, std::string>;
>
> I have tried the std_vector example in the python examples, and that
> worked. The command I run this with is:
>
> swig -c++ -python example.i
> make
>
> Here is the beginning of the output I get from that:
>
> make -f ../../Makefile CXXSRCS='' SWIG='../../../preinst-swig' \
> SWIGOPT='' TARGET='example' INTERFACE='example.i' python_cpp
> make[1]: Entering directory
> `/home/gardner/downloads/swig-1.3.27/Examples/python/std_map'
> ../../../preinst-swig -c++ -python  example.i
> g++ -c -fpic  example_wrap.cxx    -DHAVE_CONFIG_H
> -I/usr/local/include/python2.4 -I/usr/local/lib/python2.4/config
> example_wrap.cxx: In function `const char* swig::type_name() [with Type =
>    std::string]':
> example_wrap.cxx:1596:   instantiated from `static swig_type_info*
> swig::traits_info<Type>::type_info() [with Type = std::string]'
>
> I am running swig 1.3.27 on Quantian Linux (debian base, kernel
> 2.4.27) using gcc version 3.3.4.
>
> If anyone has an example of using an STL map from python, I would be
> very grateful!
>
> Thanks,
>
> - Gardner
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: SPAM-LOW: Re: Python SWIG std::map error?

gardnerpomper
Thank you, thank you, thank you!

Ok, I changed example.i to what you have listed below, and tried this runme.py:

# file: runme.py
# test of std::map<std::string, std::string>;
import example

v = example.map_si()
v['hi'] = 'no'
v['bye'] = 'yes'
print v.keys()
print v.values()

Executing "python runme.py" results in this output:

['bye', 'hi']
('yes', 'no')

This is SO COOL! I have an application in python that loads 5M lines from a 171MB file. Putting them into a dictionary with 5M entries uses up 1.5GB of ram. This SWIG will allow me to save a HUGE amount of memory.

Thank you so much!

- Gardner

On 1/7/06, Marcelo Matus <[hidden email]> wrote:
As the std_map.i file says, std::map requires to instantiate std::pair.

So you need to do:

%module example

%include "std_string.i"
%include "std_map.i"

/* instantiate the required template specializations */
%template() std::pair<std::string, std::string>;
%template(map_si)  std::map<std::string, std::string>;


Marcelo


Gardner Pomper wrote:

> Hi,
>
> I have a need for a C++ std::map<std::string,std::string> in my python
> program, so I download swig. I tried the following as an example to
> use std::map, but I get errors when I try to compile the generated
> .cxx file. Here is my example:
>
> /* File : example.i */
> %module example
>
> %include "std_string.i"
> %include "std_map.i"
>
> /* instantiate the required template specializations */
> %template(map_si)  std::map<std::string, std::string>;
>
> I have tried the std_vector example in the python examples, and that
> worked. The command I run this with is:
>
> swig -c++ -python example.i
> make
>
> Here is the beginning of the output I get from that:
>
> make -f ../../Makefile CXXSRCS='' SWIG='../../../preinst-swig' \
> SWIGOPT='' TARGET='example' INTERFACE='example.i' python_cpp
> make[1]: Entering directory
> `/home/gardner/downloads/swig- 1.3.27/Examples/python/std_map'
> ../../../preinst-swig -c++ -python  example.i
> g++ -c -fpic  example_wrap.cxx    -DHAVE_CONFIG_H
> -I/usr/local/include/python2.4 -I/usr/local/lib/python2.4/config
> example_wrap.cxx: In function `const char* swig::type_name() [with Type =
>    std::string]':
> example_wrap.cxx:1596:   instantiated from `static swig_type_info*
> swig::traits_info<Type>::type_info() [with Type = std::string]'
>
> I am running swig 1.3.27 on Quantian Linux (debian base, kernel
> 2.4.27) using gcc version 3.3.4.
>
> If anyone has an example of using an STL map from python, I would be
> very grateful!
>
> Thanks,
>
> - Gardner
>



Reply | Threaded
Open this post in threaded view
|

Re: SPAM-LOW: Re: Python SWIG std::map error?

Marcelo Matus
Good!, when you have some numbers of what memory std::map requires for
the same file, please share them with us. It would be interesting to
know that.

Marcelo


Gardner Pomper wrote:

> Thank you, thank you, thank you!
>
> Ok, I changed example.i to what you have listed below, and tried this
> runme.py:
>
> # file: runme.py
> # test of std::map<std::string, std::string>;
> import example
>
> v = example.map_si()
> v['hi'] = 'no'
> v['bye'] = 'yes'
> print v.keys()
> print v.values()
>
> Executing "python runme.py" results in this output:
>
> ['bye', 'hi']
> ('yes', 'no')
>
> This is SO COOL! I have an application in python that loads 5M lines
> from a 171MB file. Putting them into a dictionary with 5M entries uses
> up 1.5GB of ram. This SWIG will allow me to save a HUGE amount of memory.
>
> Thank you so much!
>
> - Gardner
>
> On 1/7/06, *Marcelo Matus* < [hidden email]
> <mailto:[hidden email]>> wrote:
>
>     As the std_map.i file says, std::map requires to instantiate
>     std::pair.
>
>     So you need to do:
>
>     %module example
>
>     %include "std_string.i"
>     %include "std_map.i"
>
>     /* instantiate the required template specializations */
>     %template() std::pair<std::string, std::string>;
>     %template(map_si)  std::map<std::string, std::string>;
>
>
>     Marcelo
>
>
>     Gardner Pomper wrote:
>
>     > Hi,
>     >
>     > I have a need for a C++ std::map<std::string,std::string> in my
>     python
>     > program, so I download swig. I tried the following as an example to
>     > use std::map, but I get errors when I try to compile the generated
>     > .cxx file. Here is my example:
>     >
>     > /* File : example.i */
>     > %module example
>     >
>     > %include "std_string.i"
>     > %include "std_map.i"
>     >
>     > /* instantiate the required template specializations */
>     > %template(map_si)  std::map<std::string, std::string>;
>     >
>     > I have tried the std_vector example in the python examples, and that
>     > worked. The command I run this with is:
>     >
>     > swig -c++ -python example.i
>     > make
>     >
>     > Here is the beginning of the output I get from that:
>     >
>     > make -f ../../Makefile CXXSRCS='' SWIG='../../../preinst-swig' \
>     > SWIGOPT='' TARGET='example' INTERFACE='example.i' python_cpp
>     > make[1]: Entering directory
>     > `/home/gardner/downloads/swig- 1.3.27/Examples/python/std_map'
>     > ../../../preinst-swig -c++ -python  example.i
>     > g++ -c -fpic  example_wrap.cxx    -DHAVE_CONFIG_H
>     > -I/usr/local/include/python2.4 -I/usr/local/lib/python2.4/config
>     > example_wrap.cxx: In function `const char* swig::type_name()
>     [with Type =
>     >    std::string]':
>     > example_wrap.cxx:1596:   instantiated from `static swig_type_info*
>     > swig::traits_info<Type>::type_info() [with Type = std::string]'
>     >
>     > I am running swig 1.3.27 on Quantian Linux (debian base, kernel
>     > 2.4.27) using gcc version 3.3.4.
>     >
>     > If anyone has an example of using an STL map from python, I would be
>     > very grateful!
>     >
>     > Thanks,
>     >
>     > - Gardner
>     >
>
>
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Python SWIG std::map error?

Marcelo Matus
In reply to this post by gardnerpomper
If your maps are very large, probably you would like to try
the swig CVS version, which avoid creating temporal tuples or lists.

Of course, if you call

v = example.map_si()
...
print v.keys()
print v.values()

that will create very large lists to contain the keys and values,
but if you use iterators, such as:

for key in v.iterkeys():
  print "key", key

for val in v.itervalues():
  print "value", val

for key,val in v.iteritems():
  print "item", key,val

for key in v:
  print "item", key, v[key]

swig will use native C++ iterators and no copies of the map, or auxiliar
python
tuples/lists, will be created.

Marcelo


Gardner Pomper wrote:

> Thank you, thank you, thank you!
>
> Ok, I changed example.i to what you have listed below, and tried this
> runme.py:
>
> # file: runme.py
> # test of std::map<std::string, std::string>;
> import example
>
> v = example.map_si()
> v['hi'] = 'no'
> v['bye'] = 'yes'
> print v.keys()
> print v.values()
>
> Executing "python runme.py" results in this output:
>
> ['bye', 'hi']
> ('yes', 'no')
>
> This is SO COOL! I have an application in python that loads 5M lines
> from a 171MB file. Putting them into a dictionary with 5M entries uses
> up 1.5GB of ram. This SWIG will allow me to save a HUGE amount of memory.
>
> Thank you so much!
>
> - Gardner
>
> On 1/7/06, *Marcelo Matus* < [hidden email]
> <mailto:[hidden email]>> wrote:
>
>     As the std_map.i file says, std::map requires to instantiate
>     std::pair.
>
>     So you need to do:
>
>     %module example
>
>     %include "std_string.i"
>     %include "std_map.i"
>
>     /* instantiate the required template specializations */
>     %template() std::pair<std::string, std::string>;
>     %template(map_si)  std::map<std::string, std::string>;
>
>
>     Marcelo
>
>
>     Gardner Pomper wrote:
>
>     > Hi,
>     >
>     > I have a need for a C++ std::map<std::string,std::string> in my
>     python
>     > program, so I download swig. I tried the following as an example to
>     > use std::map, but I get errors when I try to compile the generated
>     > .cxx file. Here is my example:
>     >
>     > /* File : example.i */
>     > %module example
>     >
>     > %include "std_string.i"
>     > %include "std_map.i"
>     >
>     > /* instantiate the required template specializations */
>     > %template(map_si)  std::map<std::string, std::string>;
>     >
>     > I have tried the std_vector example in the python examples, and that
>     > worked. The command I run this with is:
>     >
>     > swig -c++ -python example.i
>     > make
>     >
>     > Here is the beginning of the output I get from that:
>     >
>     > make -f ../../Makefile CXXSRCS='' SWIG='../../../preinst-swig' \
>     > SWIGOPT='' TARGET='example' INTERFACE='example.i' python_cpp
>     > make[1]: Entering directory
>     > `/home/gardner/downloads/swig- 1.3.27/Examples/python/std_map'
>     > ../../../preinst-swig -c++ -python  example.i
>     > g++ -c -fpic  example_wrap.cxx    -DHAVE_CONFIG_H
>     > -I/usr/local/include/python2.4 -I/usr/local/lib/python2.4/config
>     > example_wrap.cxx: In function `const char* swig::type_name()
>     [with Type =
>     >    std::string]':
>     > example_wrap.cxx:1596:   instantiated from `static swig_type_info*
>     > swig::traits_info<Type>::type_info() [with Type = std::string]'
>     >
>     > I am running swig 1.3.27 on Quantian Linux (debian base, kernel
>     > 2.4.27) using gcc version 3.3.4.
>     >
>     > If anyone has an example of using an STL map from python, I would be
>     > very grateful!
>     >
>     > Thanks,
>     >
>     > - Gardner
>     >
>
>
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user