代码之家  ›  专栏  ›  技术社区  ›  Patrick Trentin

如何强制swig在模块的帮助页中生成适当的参数列表?

  •  0
  • Patrick Trentin  · 技术社区  · 7 年前

    这个 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(&ltime);
         return ctime(&ltime);
     }
    

    文件 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();
    
    0 回复  |  直到 7 年前
        1
  •  1
  •   Mark Tolonen    7 年前

    36.10 Docstring Features 在SWIG文档中。

    尤其是 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
    
        2
  •  1
  •   Jens Munk    7 年前

    另一种选择是 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 *
    

    它概括了类的文档,而且非常灵活。