使用help
生成的模块的SWIG
页面并不是很有用。实际上,它甚至没有列出每个函数的参数。
Help on module example:
NAME
example
FILE
/home/anon/example.py
DESCRIPTION
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 3.0.12
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.
FUNCTIONS
fact(...)
get_time(...)
my_mod(...)
DATA
cvar = <Swig global variables>
(END)
问题:有没有办法告诉swig
- 有一些选项 - 至少包括每个函数的命名参数的确切列表?
我想至少得到如下内容:
...
fact(n)
...
my_mod(x, y)
...
一般来说,更高质量的文件也是受欢迎的。
我知道如果我将原始函数foo
重命名为_foo
然后我手动定义一个新的foo()
,我可以获得此结果。但是,是否有一些其他的,系统的和内置的方法达到了同样的目的?
这是我执行的命令列表,取自这个tutorial:
~$ swig -python example.i
~$ gcc -fPIC -c example.c example_wrap.c \
-I/usr/include/python2.7
~$ ld -shared example.o example_wrap.o -o _example.so
文件example.c
:
/* File : example.c */
#include <time.h>
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
int my_mod(int x, int y) {
return (x%y);
}
char *get_time()
{
time_t ltime;
time(<ime);
return ctime(<ime);
}
文件example.i
:
/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
请参阅SWIG文档中的36.10 Docstring Features。
特别是,autodoc
功能适用于您的示例。只需使用:
swig -python -features autodoc example.i
样本输出:
>>> import example
>>> help(example)
Help on module example:
NAME
example
DESCRIPTION
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 3.0.12
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.
FUNCTIONS
fact(n)
fact(int n) -> int
get_time()
get_time() -> char *
my_mod(x, y)
my_mod(int x, int y) -> int
DATA
cvar = <Swig global variables>
FILE
c:\example\example.py
另一种选择是使用doxy2swig.py
,参见例如http://github.com/m7thon/doxy2swig
使用doxygen example.h
的标头
#pragma once
extern double My_variable; ///< My variable for something
/**
* Factorial function
*
* @param n
*
* @return n!
*/
extern int fact(int n);
/**
* Module function
*
* @param x
* @param y
*
* @return
*/
extern int my_mod(int x, int y);
/**
* Get the current time
*
*
* @return string representation of time
*/
extern char *get_time();
接口文件example.i
%module example
%{
/* Put header files here or function declarations like below */
#include "example.h"
%}
%include "documentation.i"
%include "example.h"
要进行SWIG并编译,请执行以下操作。当然,如果有人愿意,可以使用automake或CMake很好地设置。
doxygen -g
sed -i 's/GENERATE_XML = NO/GENERATE_XML = YES/g' Doxyfile
python doxy2swig.py -c -a ./xml/index.xml documentation.i
swig -python example.i
gcc -fPIC -c example.c example_wrap.c -I/usr/include/python2.7
ld -shared example.o example_wrap.o -o _example.so
在Python中,文档看起来像
In [1]: import example
In [2]: help(example.get_time)
Help on function get_time in module example:
get_time()
Get the current time
Returns
-------
string representation of time
get_time() -> char *
它概括为类的文档,它非常灵活。