Connecting Tech Pros Worldwide Forums | Help | Site Map

Extending Python questions

Eli Daniel
Guest
 
Posts: n/a
#1: Jul 18 '05
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.openconnection('www.yahoo.com',12,2000)

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_UnpackTuple 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.openconnection") ?
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
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Extending Python questions


> myshell.openconnection('www.yahoo.com',12,2000)[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_UnpackTuple 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.openconnection('www.yahoo.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.openconnection") ?[/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.readlines():
command, rest = line.split()[0], " ".join(line.split()[1:])
getattr(myshell, command)(rest)


--
Regards,

Diez B. Roggisch
Eli Daniel
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Extending Python questions


"Diez B. Roggisch" <deetsNOSPAM@web.de> wrote in message news:<cmb5i9$qbh$04$1@news.t-online.com>...[color=blue][color=green]
> > myshell.openconnection('www.yahoo.com',12,2000)
> >
> > 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_UnpackTuple 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.openconnection('www.yahoo.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.openconnection") ?[/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.readlines():
> command, rest = line.split()[0], " ".join(line.split()[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
Diez B. Roggisch
Guest
 
Posts: n/a
#4: Jul 18 '05

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
Closed Thread