Extending Python questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eli Daniel

    #1

    Extending Python questions

    Hi,
    I am relative new to Python. Please tell me if the following is
    possible.
    I have a command line shell written in C. This shell executes some
    commands which I would like to import to the python shell.
    Here is an example command from my shell:

    openconnection www.yahoo.com 12 2000

    So I've created a dll (myshell_d.dll) according to the manual
    ("Extending Python with C or C++") and it works. The problem is the
    syntax I have to use now is

    myshell.opencon nection('www.ya hoo.com',12,200 0)

    It's not only the problem of comfort - it's a problem of changing
    *some* scripts. And there's more than that - my shell functions
    already know how to handle it's arguments (which's number may vary),
    it doesn't need python to do that. All it needs is a pointer to a
    string representing the arguments (in our case "www.yahoo. com 12
    2000"). I've tried using PyArg_UnpackTup le but without success.

    So I have two questions:
    1. Is it possible to move the calls to a upper level (i.e. call
    "openconnection " and not "myshell.openco nnection") ?
    2. Is is possible to remove the brackets and commas? (i.e.
    "www.yahoo. com 12 2000" instead of ('www.yahoo.com ',12,2000)?

    My final goal is to use the original commands if possible.
    thanks in advance,
    Eli
  • Diez B. Roggisch

    #2
    Re: Extending Python questions

    > myshell.opencon nection('www.ya hoo.com',12,200 0)[color=blue]
    >
    > It's not only the problem of comfort - it's a problem of changing
    > *some* scripts. And there's more than that - my shell functions
    > already know how to handle it's arguments (which's number may vary),
    > it doesn't need python to do that. All it needs is a pointer to a
    > string representing the arguments (in our case "www.yahoo. com 12
    > 2000"). I've tried using PyArg_UnpackTup le but without success.[/color]

    Then don't specify the arguments separately, but instead make it one string
    argument, and pass it to your command:

    myshell.opencon nection('www.ya hoo.com 12 2000')

    [color=blue]
    > So I have two questions:
    > 1. Is it possible to move the calls to a upper level (i.e. call
    > "openconnection " and not "myshell.openco nnection") ?[/color]

    from myshell import *
    [color=blue]
    > 2. Is is possible to remove the brackets and commas? (i.e.
    > "www.yahoo. com 12 2000" instead of ('www.yahoo.com ',12,2000)?[/color]

    No. Thats too much of tinkering with pythons parser - you'd render all other
    python sources useless, which I suspect outnumber your scripts by a degree
    or two... :)
    [color=blue]
    > final goal is to use the original commands if possible.
    > thanks in advance,[/color]

    What do you use python for in the first place, if you want to keep
    everything as it is?

    What you can do is to read your scripts _using_ python instead of _passing_
    them to python - then you can control the syntax of your scripting
    language. The main loop could look like this:

    import myshell
    for line in sys.stdin.readl ines():
    command, rest = line.split()[0], " ".join(line.spl it()[1:])
    getattr(myshell , command)(rest)


    --
    Regards,

    Diez B. Roggisch

    Comment

    • Eli Daniel

      #3
      Re: Extending Python questions

      "Diez B. Roggisch" <deetsNOSPAM@we b.de> wrote in message news:<cmb5i9$qb h$04$1@news.t-online.com>...[color=blue][color=green]
      > > myshell.opencon nection('www.ya hoo.com',12,200 0)
      > >
      > > It's not only the problem of comfort - it's a problem of changing
      > > *some* scripts. And there's more than that - my shell functions
      > > already know how to handle it's arguments (which's number may vary),
      > > it doesn't need python to do that. All it needs is a pointer to a
      > > string representing the arguments (in our case "www.yahoo. com 12
      > > 2000"). I've tried using PyArg_UnpackTup le but without success.[/color]
      >
      > Then don't specify the arguments separately, but instead make it one string
      > argument, and pass it to your command:
      >
      > myshell.opencon nection('www.ya hoo.com 12 2000')
      >
      >[color=green]
      > > So I have two questions:
      > > 1. Is it possible to move the calls to a upper level (i.e. call
      > > "openconnection " and not "myshell.openco nnection") ?[/color]
      >
      > from myshell import *
      >[color=green]
      > > 2. Is is possible to remove the brackets and commas? (i.e.
      > > "www.yahoo. com 12 2000" instead of ('www.yahoo.com ',12,2000)?[/color]
      >
      > No. Thats too much of tinkering with pythons parser - you'd render all other
      > python sources useless, which I suspect outnumber your scripts by a degree
      > or two... :)
      >[color=green]
      > > final goal is to use the original commands if possible.
      > > thanks in advance,[/color]
      >
      > What do you use python for in the first place, if you want to keep
      > everything as it is?
      >
      > What you can do is to read your scripts _using_ python instead of _passing_
      > them to python - then you can control the syntax of your scripting
      > language. The main loop could look like this:
      >
      > import myshell
      > for line in sys.stdin.readl ines():
      > command, rest = line.split()[0], " ".join(line.spl it()[1:])
      > getattr(myshell , command)(rest)[/color]


      Thanks for the reply. I'll try those.
      The reason I'm trying it this way is that a user can add python code
      inside the scripts.

      Eli

      Comment

      • Diez B. Roggisch

        #4
        Re: Extending Python questions

        >[color=blue]
        > Thanks for the reply. I'll try those.
        > The reason I'm trying it this way is that a user can add python code
        > inside the scripts.[/color]

        Sorry to say that, but then you'll have to either write a real parser that
        understands both syntaxes - and I bet thats more effort as converting your
        existing scripts.

        You can't have your cake and eat it - if you want python, you should use its
        syntax.
        --
        Regards,

        Diez B. Roggisch

        Comment

        Working...