swig & Python question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • It's me

    #1

    swig & Python question

    I am playing around with SWING building a Python module using the no brainer
    example in http://www.swig.org/tutorial.html. With that first example,


    /* 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);
    }
    and using the swig file of:
    /* example.i */
    %module example
    %{
    /* Put header files here (optional) */
    %}

    extern double My_variable;
    extern int fact(int n);
    extern int my_mod(int x, int y);
    extern char *get_time();
    I was able to execute this Python script:

    [color=blue][color=green][color=darkred]
    >>> import example
    >>> example.fact(5)[/color][/color][/color]
    120[color=blue][color=green][color=darkred]
    >>> example.my_mod( 7,3)[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> example.get_tim e()[/color][/color][/color]
    'Sun Feb 11 23:01:07 1996'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    However, when I try to access the gloabl variable My_variable by doing:

    print example.cvar

    I get a blank (rather then a value of 3.0).

    Why?

    --
    It's me


  • It's me

    #2
    Re: swig &amp; Python question


    "It's me" <itsme@yahoo.co m> wrote in message
    news:oDStd.3107 6$zx1.23489@new ssvr13.news.pro digy.com...[color=blue]
    > I am playing around with SWING building a Python module using the no[/color]
    brainer[color=blue]
    > example in http://www.swig.org/tutorial.html. With that first example,
    >[/color]

    Oops! Soapy fingers. "SWIG" - not "SWING".

    --
    It's me.


    Comment

    • Keith Dart

      #3
      Re: swig &amp; Python question

      It's me wrote:[color=blue]
      > "It's me" <itsme@yahoo.co m> wrote in message
      > news:oDStd.3107 6$zx1.23489@new ssvr13.news.pro digy.com...
      >[color=green]
      >>I am playing around with SWING building a Python module using the no[/color]
      >
      > brainer
      >[color=green]
      >>example in http://www.swig.org/tutorial.html. With that first example,
      >>[/color]
      >
      >
      > Oops! Soapy fingers. "SWIG" - not "SWING".
      >
      > --
      > It's me.[/color]

      I have used SWIG before, and it's not always such a "no-brainer". In
      fact, it rarely is except for trivial examples. But it can work. I think
      it is best suited for wrapping large libraries. For small stuff, it
      would be better to just do it "manually" using the Python C API.


      Good luck.

      --
      It's not me.




      --
      \/ \/
      (O O)
      -- --------------------oOOo~(_)~oOOo----------------------------------------
      Keith Dart <kdart@kdart.co m>
      public key: ID: F3D288E4
      =============== =============== =============== =============== =============== =

      Comment

      • It's me

        #4
        Re: swig &amp; Python question

        Whether SWIG will work in a "no brainer" way or not depends on the original
        code, I think. If the original code uses a very convoluted design, of
        course things will get hairy. If the package uses a very clean structure,
        I think you will find SWIG works out very nicely.

        The intriguing things is, however, once you structure the package to a form
        SWIG would work, it opens up the door to support multiple script languages
        (and they have a long list of supported script languages).

        If you hand crafted it to run the Python-C API, then you can only use Python
        as script.

        --
        It's me


        "Keith Dart" <kdart@kdart.co m> wrote in message
        news:41BAD6F0.6 020509@kdart.co m...[color=blue]
        > It's me wrote:[color=green]
        > > "It's me" <itsme@yahoo.co m> wrote in message
        > > news:oDStd.3107 6$zx1.23489@new ssvr13.news.pro digy.com...
        > >[color=darkred]
        > >>I am playing around with SWING building a Python module using the no[/color]
        > >
        > > brainer
        > >[color=darkred]
        > >>example in http://www.swig.org/tutorial.html. With that first example,
        > >>[/color]
        > >
        > >
        > > Oops! Soapy fingers. "SWIG" - not "SWING".
        > >
        > > --
        > > It's me.[/color]
        >
        > I have used SWIG before, and it's not always such a "no-brainer". In
        > fact, it rarely is except for trivial examples. But it can work. I think
        > it is best suited for wrapping large libraries. For small stuff, it
        > would be better to just do it "manually" using the Python C API.
        >
        >
        > Good luck.
        >
        > --
        > It's not me.
        >
        >
        >
        >
        > --
        > \/ \/
        > (O O)
        > -- --------------------oOOo~(_)~oOOo--------------------------------------[/color]
        --[color=blue]
        > Keith Dart <kdart@kdart.co m>
        > public key: ID: F3D288E4
        >[/color]
        =============== =============== =============== =============== =============== =


        Comment

        Working...